ios sqlite insert 和 update的区别
1个回答
展开全部
首先xcode 新建的工程需要加载sqlite3 。看图点击添加这个就ok
首先创建数据库
//0获取沙盒中的数据库名
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"student.sqlite"];
NSLog(@"%@",filename);
//2.创建数据库
int result = sqlite3_open(filename.UTF8String, &_db);
if (result == SQLITE_OK) {
NSLog(@"成功打开");
//3.创表
const char *sql = "create TABLE if not EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text, age integer);";
char *errorMesg = NULL;
int result = sqlite3_exec(_db, sql, NULL,NULL, &errorMesg);
if (result == SQLITE_OK) {
NSLog(@"成功创建表XXX");
}else{
NSLog(@"创表失败:%s",errorMesg);
}
}else{
NSLog(@"打开数据库失败");
}
添加表 及数据
const char *sql = "insert into t_student (name, age) values('jack', 20);";
char *errorMesg = NULL;
int result = sqlite3_exec(_db,sql,NULL, NULL, &errorMesg);
if (result == SQLITE_OK) {
NSLog(@"成功添加数据");
}else {
NSLog(@"添加数据失败:%s",errorMesg);
}
查看是否添加
进入打印的 路径查看即可
首先创建数据库
//0获取沙盒中的数据库名
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"student.sqlite"];
NSLog(@"%@",filename);
//2.创建数据库
int result = sqlite3_open(filename.UTF8String, &_db);
if (result == SQLITE_OK) {
NSLog(@"成功打开");
//3.创表
const char *sql = "create TABLE if not EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text, age integer);";
char *errorMesg = NULL;
int result = sqlite3_exec(_db, sql, NULL,NULL, &errorMesg);
if (result == SQLITE_OK) {
NSLog(@"成功创建表XXX");
}else{
NSLog(@"创表失败:%s",errorMesg);
}
}else{
NSLog(@"打开数据库失败");
}
添加表 及数据
const char *sql = "insert into t_student (name, age) values('jack', 20);";
char *errorMesg = NULL;
int result = sqlite3_exec(_db,sql,NULL, NULL, &errorMesg);
if (result == SQLITE_OK) {
NSLog(@"成功添加数据");
}else {
NSLog(@"添加数据失败:%s",errorMesg);
}
查看是否添加
进入打印的 路径查看即可
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询