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://download.csdn.net/detail/samguoyi/4433645