sqlite 简用

#import "ViewController.h" #import <sqlite3.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Nsstring *dbPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"sqlite"];//获取主包中的文件路径 Nsstring *docPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/data.sqlite"];//获取Documens下的文件路径 NSLog(@"%@",dbPath); NSLog(@"%@",docPath); NSFileManager *fileManager = [NSFileManager defaultManager];//获取文件管理器的对象 //判断Documents目录下是否有Data.sqlite文件 if (![fileManager fileExistsAtPath:docPath]) { [fileManager copyItemAtPath:dbPath toPath:dbPath error:nil]; } sqlite3 *database = NULL; //如果db不存在,创建并打开 int ret = sqlite3_open([dbPath UTF8String],&database);//sqlite3_open():打开数据库,如果sqlite数据库被成功打开(或创建),将会返回sqlITE_OK,否则将会返回错误码 if ((ret != sqlITE_OK)) { NSLog(@"Open faild."); return; } Nsstring *createTable = @"CREATE TABLE IF NOT EXISTS Parents(pID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,Name VARHAR(20) NOT NULL,Age INTEGER,Desc TEXT)"; char *error = NULL; ret = sqlite3_exec(database,[createTable UTF8String],NULL,&error); if ((ret != sqlITE_OK)) { NSLog(@"Create faild."); sqlite3_close(database); return; } sqlite3_stmt *stmt = NULL; Nsstring *insert = @"INSERT INTO PARENTS(Name,Age) VALUES('萧远山',40)"; ret = sqlite3_prepare(database,[insert UTF8String],-1,&stmt,NULL);//这个函数sql文本转换成一个准备语句(prepared statement)对象,同时返回这个对象的指针。 if ((ret != sqlITE_OK)) { NSLog(@"Prepare insert data faild."); sqlite3_close(database); return; } ret = sqlite3_step(stmt);//这个过程用于执行有前面sqlite3_prepare创建的准备语句。 if ((ret != sqlITE_DONE)) { NSLog(@"Insert data faild."); sqlite3_finalize(stmt);//这个过程销毁前面被sqlite3_prepare创建的准备语句,每个准备语句都必须使用这个函数去销毁以防止内存泄露。 sqlite3_close(database); return; } sqlite3_finalize(stmt); Nsstring *select = @"SELECT Age,Name FROM TEACHER WHERE NAME=?"; ret = sqlite3_prepare(database,[select UTF8String],NULL); if ((ret != sqlITE_OK)) { NSLog(@"Prepare select data faild."); sqlite3_close(database); return; } Nsstring *name = @"玄苦"; //s对sqlite3_bind_text进行一下说明,sqlite3_bind_text的第二个参数为序号(从1开始),第三个参数为字符串值,第四个参数为字符串长度。sqlite3_bind_text的第五个参数为一个函数指针,sqlite3执行完操作后回调此函数,通常用于释放字符串占用的内存。此参数有两个常数,sqlITE_STATIC告诉sqlite3_bind_text函数字符串为常量,可以放心使用;而sqlITE_TRANSIENT会使得sqlite3_bind_text函数对字符串做一份拷贝。一般使用这两个常量参数来调用sqlite3_bind_text。 sqlite3_bind_text(stmt,1,[name UTF8String],NULL); ret = sqlite3_step(stmt); NSLog(@"%i",ret); while (ret == sqlITE_ROW) { //取到一条数据 //取数据的时候,下标从0开始 const unsigned char *cStr = sqlite3_column_text(stmt,1);//得到数据行中某个列的数据 Nsstring *str = [Nsstring stringWithUTF8String:(const char *)cStr]; int age = sqlite3_column_int(stmt,2); NSLog(@"%@:%d",str,age); ret = sqlite3_step(stmt); sqlite3_finalize(stmt); } sqlite3_close(database);//这个过程关闭前面使用sqlite3_open打开的数据库连接,任何与这个连接相关的准备语句必须在调用这个关闭函数之前被释放 // Do any additional setup after loading the view,typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // dispose of any resources that can be recreated. } @end

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能