FMDB的简单使用

//创建数据库,并连接

-(void)createdata

{

NSArray*doucumentDirectory=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMasknormal; word-break:normal; line-height:21px; color:rgb(0,YES);

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 = '%@'"normal; word-break:normal; line-height:21px; color:rgb(0,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@"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=? "normal; word-break:normal; line-height:21px; color:rgb(0,name,interest,uid];

//删除数据

void)deletedata

:@"deletefrom user where name=?"normal; word-break:normal; line-height:21px; color:rgb(0,[:@"%@"normal; word-break:normal; line-height:21px; color:rgb(0,@"eee" 数据库基本就是更新和查询

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

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能