首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

ios下的数据库操作实例

2014-07-25 
ios上的数据库操作实例NSLog(@"onButtonClicked");NSArray *paths = NSSearchPathForDirectoriesInDomains

ios上的数据库操作实例

NSLog(@"onButtonClicked");NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  NSString *documentDirectory = [paths objectAtIndex:0];  //dbPath: 数据库路径,在Document中。  NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"test.db"];FMDatabase *db= [FMDatabase databaseWithPath:dbPath];  if (![db open]) {  NSLog(@"Could not open db."); return;}NSLog(@"db opened");BOOL b1 =  [db executeUpdate:@"create table if not exists user (id integer primary key, name varchar, age integer);"];if(!b1){NSLog(@"create table failed");}/////插入数据NSString *sqlaa = nil;sqlaa = @"insert into user (name,age) values ('samyou',18);";sqlaa = [NSString stringWithFormat:@"insert into user (name,age) values ('%@',%d);",@"sam",22];NSLog(sqlaa);BOOL b2 =  [db executeUpdate:sqlaa];if(!b2){NSLog(@"insert failed");}/////修改数据NSString *sqlbb = [NSString stringWithFormat:@"UPDATE user SET name = '%@',age = %d WHERE age = %d ",@"咪咪",10,18]; NSLog(sqlbb);BOOL b3 = [db executeUpdate:sqlbb];if(!b3){NSLog(@"update failed");}///查询数据FMResultSet *rs = [db executeQuery:@"select * from user"];if(rs){NSLog(@"get rs");}while ([rs next]) {NSLog(@"%@%d",[rs stringForColumn:@"name"],[rs intForColumn:@"age"]);}//close[rs close];[db close];

参考了http://blog.csdn.net/f520131480315/article/details/6444297的文章,有些细节的东西请看原文

上传了一个源代码工程,可以去我的资源页下载

http://download.csdn.net/detail/samguoyi/4433645



热点排行