FMDB的简单使用

前端之家收集整理的这篇文章主要介绍了FMDB的简单使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//创建数据库,并连接

-(void)createdata

{

NSArray*doucumentDirectory=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMaskYES);

NSString*file=[doucumentDirectoryobjectAtIndex:0];

NSString*dafile=[filestringByAppendingPathComponent:@"db.sqlite"];

if([[NSFileManagerdefaultManager]fileExistsAtPath:dafile])

{

NSLog(@"数据库已存在");

}


db=[FMDatabasedatabaseWithPath:dafile];

if(!db)

数据库创建不成功 }

else

{if([dbopen])

{

NSLog(@"数据库创建成功并打开");

FMResultSet* set = [executeQuery:[NSStringstringWithFormat:@"select count(*) from sqlite_master where type ='table' and name = '%@'"kUserTableName]];

[setnext];

NSIntegercount = [setintForColumnIndex:0];

BOOLexistTable = count;

if(existTable)

{

(@"数据表已存在,请重新添加" return;}

else

*createtable=@"CREATE TABLE user (uid INTEGER PRIMARY KEY AUTOINCREMENTNOT NULL,name text,interest text)";

[executeUpdate:createtable];}

}

//插入数据

void)insertdata:(NSArray*)arguments

[executeUpdate:@"INSERT INTO user(name,interest) VALUES(?,?) "withArgumentsInArray:arguments];

}

}

//查询所有数据

-(NSMutableArray*)selectall

NSMutableArray*array=[[NSMutableArrayalloc]initWithCapacity:100

*sql=@"select * from user where name=? or interest=?" FMResultSet*qureyresult=[executeQuery:sql,@"333",27)">@"111"];

while([qureyresultnext])

{

NSIntegeruid=[qureyresultintForColumn:@"uid"];

NSString*name=[qureyresultstringForColumn:@"name"];

NSString*inserest=[qureyresult@H_859_404@@"interest"];

NSMutableDictionary*dic=[[NSMutableDictionaryinit

[dicsetValue:[stringWithFormat:@"%d",uid]forKey:@"keyuid"];

setValue:name@"keyname"];

setValue:inserest@"keyinserest"];

[arrayaddObject:dic];

}

returnarray;

//修改数据

void)mergerwithname:(NSString*)name withinterest:(NSString*)interestanduid:(NSNumber*)uid

:@"UPDATE user SET name=?,interest=? WHERE uid=? " //删除数据

void)deletedata

:@"deletefrom user where name=?":@"%@" 数据库基本就是更新和查询

除了select,dbexecuteQuery:;其他都是更新executeUpdate:传参数时可用dbexecuteUpdate:withArgumentsInArray:

猜你在找的Sqlite相关文章