NSString *sql = @"CREATE TABLE student(_id Integer primary key, name text, password text, email text)";
NSString *sqlInsert = @"INSERT INTO student (name, password, email) values ('寶貝', '1232', '3343243')";
NSLog(@"%d", isInsertOK );
3.批量的創(chuàng)建表和插入數(shù)據(jù)
NSString *sql1 = @"create table bulktest1 (id integer primary key autoincrement, x text);" "create table bulktest2 (id integer primary key autoincrement, y text);" "create table bulktest3 (id integer primary key autoincrement, z text);" "insert into bulktest1 (x) values ('XXX');" "insert into bulktest2 (y) values ('YYY');" "insert into bulktest3 (z) values ('ZZZ');"; [dataBase executeStatements:sql1]; |
當然也可以通過數(shù)組的方式插入數(shù)據(jù)
NSArray *stuentInfo = [NSArray arrayWithObjects:@"大傻瓜",@"213",@"123456@qq.com", nil nil];
[dataBase executeUpdate:@"insert into student (name, password, email) values (?,?,?)" withArgumentsInArray:stuentInfo];
4.匹配的方式插入數(shù)據(jù)
NSString *sqlInsert = @"INSERT INTO student (name, password, email) values (?,?,?)";
BOOL flag = [dataBase executeUpdate:sqlInsert, @"大寶貝", @"123", @"654321@qq.com"];
NSLog(@"%d", flag);
5.1.查詢所有數(shù)據(jù)
FMResultSet *result = [dataBase executeQuery:@"SELECT * FROM student"];
[self showInfo:result];
[result close];
- (void)showInfo:(FMResultSet *)result { while ([result next]) { int _id = [result intForColumnIndex:0]; NSLog(@"id : %d", _id); NSString *name = [result stringForColumnIndex:1]; NSLog(@"name : %@", name); NSString *password = [result stringForColumnIndex:2]; NSLog(@"password : %@", password); NSString *email = [result stringForColumnIndex:3]; NSLog(@"email : %@", email); } } |
5.2.查詢指定信息
NSString *sqlInsert = @"SELECT * from student where name = %@";
FMResultSet *result = [dataBase executeQueryWithFormat:sqlInsert, @"寶貝"];
NSLog(@"%@", result);
[self showInfo:result];
6.執(zhí)行刪除操作
NSString *sql = @"delete from student where name = ?";
[dataBase executeUpdate:sql,@"寶貝"];
執(zhí)行更新操作的步驟和刪除操作一樣,只不過sql語句不同而已
重要注意事項是:1 使用完數(shù)據(jù)庫后記得關閉
2 查詢結果完場后result也得記得關閉