数据库类库FMDB

发表于:2014-5-13 10:36

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:激情为梦想而生    来源:51Testing软件测试网采编

分享:
  3.批量的创建表和插入数据
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];
  当然也可以通过数组的方式插入数据
  NSArray *stuentInfo = [NSArray arrayWithObjects:@"大傻瓜",@"213",@"123456@qq.com", nil nil];
  [dataBase executeUpdate:@"insert into student (name, password, email) values (?,?,?)" withArgumentsInArray:stuentInfo];
  4.匹配的方式插入数据
  NSString *sqlInsert = @"INSERT INTO student (name, password, email) values (?,?,?)";
  BOOL flag = [dataBase executeUpdate:sqlInsert, @"大宝贝", @"123", @"654321@qq.com"];
  NSLog(@"%d", flag);
  5.1.查询所有数据
  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.执行删除操作
  NSString *sql = @"delete from student where name = ?";
  [dataBase executeUpdate:sql,@"宝贝"];
  执行更新操作的步骤和删除操作一样,只不过sql语句不同而已
  重要注意事项是:1 使用完数据库后记得关闭
  2 查询结果完场后result也得记得关闭
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号