iOS之sqlite3数据库介绍(附代码)
---恢复内容开始---
// path为:~/Documents/person.db
sqlite3 *db = NULL;
int result = sqlite3_open([path UTF8String], &db);
•关闭数据库:sqlite3_close(db);
char *errorMsg = NULL; // 用来存储错误信息
char *sql = "create table if not exists t_person(id integer primary key autoincrement, name text, age integer);";
int result = sqlite3_exec(db, sql, NULL, NULL, &errorMsg);
•代码解析:
char *sql = "insert into t_person(name, age) values(?, ?);";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, "母鸡", -1, NULL);
sqlite3_bind_int(stmt, 2, 27);
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
NSLog(@"插入数据错误");
}
sqlite3_finalize(stmt);
•代码解析:
char *sql = "select id,name,age from t_person;";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
int _id = sqlite3_column_int(stmt, 0);
char *_name = (char *)sqlite3_column_text(stmt, 1);
NSString *name = [NSString stringWithUTF8String:_name];
int _age = sqlite3_column_int(stmt, 2);
NSLog(@"id=%i, name=%@, age=%i", _id, name, _age);
}
}
sqlite3_finalize(stmt);
#import "ViewController.h" #import <sqlite3.h> @interface ViewController () { sqlite3 *_db; // db代表着整个数据库,db是数据库实例 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 0.获得沙盒中的数据库文件名 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"student.sqlite"]; // 1.创建(打开)数据库(如果数据库文件不存在,会自动创建) int result = sqlite3_open(path.UTF8String, &_db); if (result == SQLITE_OK) { NSLog(@"成功打开数据库"); // 2.创表 const char *sql = "create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"; char *errmsg = NULL; int result = sqlite3_exec(_db, sql, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"创表成功"); }else{ NSLog(@"创表失败"); } }else{ NSLog(@"打开数据库失败"); } } //插入数据 - (IBAction)addData { //插入单个数据 /* const char *sql = "insert into t_student(name, age) values('jack',10);"; char *errmsg = NULL; int result = sqlite3_exec(_db, sql, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"插入数据成功"); }else{ NSLog(@"插入数据失败"); } */ //插入多条数据 for (int i = 0; i < 20; i++) { NSString *name = [NSString stringWithFormat:@"jack--%d",i]; int age = i; NSString *sql = [NSString stringWithFormat:@"insert into t_student(name, age) values('%@',%d);",name,age]; char *errmsg = NULL; int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"插入数据成功"); }else{ NSLog(@"插入数据失败"); } } } //删除数据 - (IBAction)deleteData { //删除表中所有数据 /* const char *sql = "delete from t_student;"; char *errmsg = NULL; int result = sqlite3_exec(_db, sql, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"删除表中所有数据成功"); }else{ NSLog(@"删除表中所有数据失败"); } */ //删除表中某个数据 const char *sql = "delete from t_student where name = 'jack--10';"; char *errmsg = NULL; int result = sqlite3_exec(_db, sql, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"删除特定数据成功"); }else{ NSLog(@"删除特定数据失败"); } } //更新数据 - (IBAction)updata { //通过where条件更新特定数据 const char *sql = "update t_student set name = 'chixuedong' where age > 10;"; char *errmsg = NULL; int result = sqlite3_exec(_db, sql, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"更新数据成功"); }else{ NSLog(@"更新数据失败"); } } //查询数据 - (IBAction)selectData { // 1.定义sql语句 const char *sql = "select id, name, age from t_student where age = ?;"; // 2.定义一个stmt存放结果集 sqlite3_stmt *stmt = NULL; // 3.检测SQL语句的合法性 int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL); if (result == SQLITE_OK) { NSLog(@"查询语句合法"); // 设置占位符的内容,防止SQL注入漏洞 sqlite3_bind_text(stmt, 1, "8", -1, NULL); // 4.执行SQL语句,从结果集中取出数据 while (sqlite3_step(stmt) == SQLITE_ROW) {// 真的查询到一行数据 // 获得第0列的id int t_id = sqlite3_column_int(stmt, 0); // 获得第1列的name const unsigned char *t_name = sqlite3_column_text(stmt, 1); // 获得第2列的age int t_age = sqlite3_column_int(stmt, 2); NSLog(@"%d,%s,%d",t_id,t_name,t_age); } }else{ NSLog(@"查询语句非法"); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
---恢复内容结束---