加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite 简用

发布时间:2020-12-12 19:54:39 所属栏目:百科 来源:网络整理
导读:#import "ViewController.h" #import sqlite3.h @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读