加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

FMDB的简单使用

发布时间:2020-12-12 19:42:13 所属栏目:百科 来源:网络整理
导读:// 创建数据库,并连接 -( void )createdata { NSArray *doucumentDirectory= NSSearchPathForDirectori esInDomains ( NSDocumentDirectory , NSUserDomainMask YES ); NSString *file=[doucumentDirectory objectAtIndex : 0 ]; NSString *dafile=[file st

//创建数据库,并连接

-(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@"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:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读