创建数据表
NSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString*filename = [doc stringByAppingdingPathComponent:@"students.sqlte"];
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filename];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL"];
if(rsult) {
NSLog(@"succeeded");
}else{
NSLog(@"failed");
}
}];
插入数据
FMDB中,除查询意外的所有操作,都称为“更新”。
create, drop, insert, update, delete等
[slef.queue inDatabase:^(FMDatabase *db){
[db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);", "lucy", @(arc4random_uniform(40))];
}];
[slef.queue inDatabase:^(FMDatabase *db){
[db excuteUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES (%@, %d);", name, arc4random_uniform(40)];
];
查询
[slef.queue inDatabase:^(FMDatabase *db){
FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM t_student"];
while([resultSet next]) {
int ID= [resultSet intForColumn:@"id"];
NSString*name = [resultSet stringForColumn:@"name"];
int age = [resultSet intForColumn:@"age"];
}
];
删除表格
[db executeUpdate:@"DROP TABLE IF EXISTS t_student"];
网友评论