2019独角兽企业重金招聘Python工程师标准>>>
FMDB方法的介绍
1.首先我们需要创建一个FMDatabase实例:
+(FMDatabase*)DataBaseSigonInstance
{
//数据库初始化
NSString *homeDir =NSHomeDirectory();
//NSLog(@"%@",homeDir);
NSString *dbPath = [homeDirstringByAppendingPathComponent:@"Library/Caches/FMDatabaseDemo.db"];
FMDatabase *database = [FMDatabasedatabaseWithPath:dbPath];
return database;
}
2.接下来就是需要创建一个数据表了:
//打开数据库
FMDatabase *db = [FMDataBasePathDataBaseSigonInstance];
if (!db.open)
{
NSLog(@"fail to open database");
return;
}
db.logsErrors =YES;//开启错误日志
[db executeUpdate:@"CREATE TABLE IF NOT EXISTS studentTable(name varchar,age integer,totalScore float)"];
[dbclose];
3.添加数据:
FMDatabase *db = [FMDataBasePathDataBaseSigonInstance];
if (!db.open)
{
NSLog(@"fail to open database");
return;
}
//获得数据
student.name = ((UITextField*)[alertViewviewWithTag:TEXTFIELD_TAG]).text;
student.age = [((UITextField*)[alertViewviewWithTag:TEXTFIELD_TAG + 1]).textintValue];
student.totalScore = [((UITextField*)[alertViewviewWithTag:TEXTFIELD_TAG + 2]).textfloatValue];
db.logsErrors =YES;
[db executeUpdate:@"insert into studentTable(name,age,totalScore)values(?,?,?)",student.name,[NSNumbernumberWithInt:student.age],[NSNumbernumberWithFloat:student.totalScore]];//注意参数必须是对象
[dbclose];
4.删除数据:
FMDatabase *db = [FMDataBasePathDataBaseSigonInstance];
if (!db.open)
{
NSLog(@"fail to open!");
return;
}
db.logsErrors =YES;
NSString *delDataSqlStr =@"delete from studentTable";
[dbexecuteUpdate:delDataSqlStr];
[dbclose];
5.修改数据:
FMDatabase *db = [FMDataBasePathDataBaseSigonInstance];
if (!db.open)
{
NSLog(@"fail to open!");
return;
}
db.logsErrors =YES;
[db executeUpdate:@"update studentTable set totalScore = 100"];
[dbclose];
查询数据:
FMDatabase *db = [FMDataBasePathDataBaseSigonInstance];
if (!db.open)
{
NSLog(@"fail to open!");
return;
}
db.logsErrors =YES;
FMResultSet *rs = [dbexecuteQuery:@"select * from studentTable"];
NSMutableArray *studentArr = [[NSMutableArrayalloc]init];
while ([rsnext])
{
NSMutableDictionary *dic = [[NSMutableDictionaryalloc]init];
[dic setValue:[rsstringForColumn:@"name"]forKey:@"name"];
[dic setValue:[NSNumbernumberWithInt:[rsintForColumn:@"age"]]forKey:@"age"];
[dic setValue:[NSNumbernumberWithDouble:[rsdoubleForColumn:@"totalScore"]]forKey:@"totalScore"];
[studentArraddObject:dic];
[dicrelease];
}
for (int count = 0; count< [studentArrcount]; count++)
{
NSLog(@"sudent:%@ age:%@ totalScore:%@",[[studentArrobjectAtIndex:count]objectForKey:@"name"],[[studentArrobjectAtIndex:count]objectForKey:@"age"],[[studentArrobjectAtIndex:count]objectForKey:@"totalScore"]);
}
[studentArrrelease];
[dbclose];
上面的都是一些增删改查的用法
下面我们再来看看FMDatabase 和 FMResultSet
FMDatabase:(主要用到这两个)
- (BOOL)executeUpdate:(NSString*)sql, ...; 这个是用于执行无结果的sql语句的
- (FMResultSet *)executeQuery:(NSString*)sql, ...; 这个是用于执行有结果的sql语句的
FMResultSet:
- (BOOL)next; 结果集的下一个
- (int)columnCount; 结果集的字段个数
- (int)intForColumn:(NSString*)columnName; 获取整形字段的信息(下面的都是同理)
- (int)intForColumnIndex:(int)columnIdx;
- (long)longForColumn:(NSString*)columnName;
- (long)longForColumnIndex:(int)columnIdx;
- (longlongint)longLongIntForColumn:(NSString*)columnName;
- (longlongint)longLongIntForColumnIndex:(int)columnIdx;
- (unsignedlonglong int)unsignedLongLongIntForColumn:(NSString*)columnName;
- (unsignedlonglong int)unsignedLongLongIntForColumnIndex:(int)columnIdx;
- (BOOL)boolForColumn:(NSString*)columnName;
- (BOOL)boolForColumnIndex:(int)columnIdx;
- (double)doubleForColumn:(NSString*)columnName;
- (double)doubleForColumnIndex:(int)columnIdx;
- (NSString*)stringForColumn:(NSString*)columnName;
- (NSString*)stringForColumnIndex:(int)columnIdx;
- (NSDate*)dateForColumn:(NSString*)columnName;
- (NSDate*)dateForColumnIndex:(int)columnIdx;
- (NSData*)dataForColumn:(NSString*)columnName; 获取二进制数据(图片可以使用这个)
- (NSData*)dataForColumnIndex:(int)columnIdx;