标签:
// //打开数据库,如果没有这个数据库就会自动创建 //首先要找到数据库的路径与文件名 NSString *path = NSHomeDirectory(); path = [path stringByAppendingPathComponent:@"db_student.sqlite"]; NSLog(@"%@",path); //因为sqlite3_open有int返回值,所以可以做判断 sqlite3 *db; if (sqlite3_open([path UTF8String], &db) != SQLITE_OK) { NSLog(@"打开数据库失败"); }else{ //对数据库进行操作 //创建表 char *sql = "create table if not exists student(name text not null primary key,age integer not null);"; char *err; //OC中用nil,C用NULL if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) { NSLog(@"创建表失败 error=%s",err); } /* //静态增加 char *sqlInsert = "insert into student(name,age) values(‘xiaoming‘,12)"; if (sqlite3_exec(db, sqlInsert, NULL, NULL, &err) != SQLITE_OK) { NSLog(@"增加数据失败 error=%s",err); } //动态增加 NSString *sqlInsert_action = [NSString stringWithFormat:@"insert into student(name,age) values(‘%@‘,%@)",self.nameText,self.ageNum]; const char *sqlChar = [sqlInsert_action UTF8String]; if (sqlite3_exec(db, sqlChar, NULL, NULL, &err) != SQLITE_OK) { NSLog(@"name = %@ age = %@ error = %s",self.nameText,self.ageNum,err); } */ /* //删 char *sqlDelete = "delete from student where name = ‘lo‘"; if (sqlite3_exec(db, sqlDelete, NULL, NULL, &err) != SQLITE_OK) { NSLog(@"删除数据失败 error=%s",err); } */ /* //改 char *sqlUpdate = "Update student set age = 33 where name = ‘xiaoming‘"; if (sqlite3_exec(db, sqlUpdate, NULL, NULL, &err)) { NSLog(@"更新数据失败 error=%s",err); } */ } //关闭数据库 sqlite3_close(db); // //
标签:
原文地址:http://my.oschina.net/u/2346786/blog/467654