iOS 原生sqlite3的使用方法
程序员文章站
2023-12-17 10:40:52
本文介绍了ios 原生sqlite3的使用方法,分享给大家,具体如下:
sqlite?
sqlit是一个开源、轻型嵌入式关系数据库,诞生于2000年5月...
本文介绍了ios 原生sqlite3的使用方法,分享给大家,具体如下:
sqlite?
- sqlit是一个开源、轻型嵌入式关系数据库,诞生于2000年5月
- 占用资源非常的低,在嵌入式设备中,可能只需要几百k的内存就够了
- 能够支持windows/linux/unix等等主流的操作系统
- 比起mysql、postgresql这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快
sql语句常用操作
增、删、改、查,crud,create[新建], retrieve[检索], update[更新], delete[删除]。
sql语法写法特点
1、不区分大小写(create = create)
2、每条语句以分号(;)结尾
3、关键字建议大写
sql语句常用关键字
select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等
- 数据定义语句(ddl):包括create和drop等操作,在数据库中创建新表或删除表(create table或 drop table)
- 数据操作语句(dml):包括insert、update、delete等操作,分别用于添加、修改、删除表中的数据
- 数据查询语句(dql):可以用于查询获得表中的数据,关键字select是dql(也是所有sql)用得最多的操作,其他dql常用的关键字有where,order by,group by和having
// viewcontroller.m // sqlite3 // // created by 周玉 on 2017/12/13. // copyright © 2017年 guidekj. all rights reserved. // #import "viewcontroller.h" #import <sqlite3.h> #define kuiscreenwidth [uiscreen mainscreen].bounds.size.width #define kuiscreenheight [uiscreen mainscreen].bounds.size.height #define file_name @"saas.sqlite" static sqlite3 *db = nil; @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.title = @"sqlite3"; //ddb // [self opendb]; // [self closedb]; //ddl // [self createtable]; // [self droptable]; //dml // [self insertdata]; // [self updatedata]; // [self deletedata]; //dql [self querydata]; } #pragma mark 查询 - (void)querydata{ sqlite3 *newdb = [self opendb]; sqlite3_stmt *statement = nil; nsstring *sqlstr = @"select * from saas_person"; int result = sqlite3_prepare_v2(newdb, sqlstr.utf8string, -1, &statement, null); if (result == sqlite_ok) { //遍历查询结果 if (!(sqlite3_step(statement) == sqlite_done)) { while (sqlite3_step(statement) == sqlite_row) { int id = sqlite3_column_int(statement, 0); const unsigned char *name = sqlite3_column_text(statement, 1); const unsigned char *sex = sqlite3_column_text(statement, 2); int age = sqlite3_column_int(statement, 3); const unsigned char *description = sqlite3_column_text(statement, 4); nslog(@"id = %d , name = %@ , sex = %@ , age = %d , description = %@",id,[nsstring stringwithutf8string:(const char *)name],[nsstring stringwithutf8string:(const char *)sex],age,[nsstring stringwithutf8string:(const char *)description]); } } else { nslog(@"查询语句完成"); } } else { nslog(@"查询语句不合法"); } sqlite3_finalize(statement); [self closedb]; } #pragma mark 删除数据记录 - (void)deletedata{ sqlite3 *newdb = [self opendb]; sqlite3_stmt *statement = nil; nsstring *sqlstr = @"delete from saas_person where name = '王鹏飞'"; int result = sqlite3_prepare_v2(newdb, sqlstr.utf8string, -1, &statement, null); if (result == sqlite_ok) { if (sqlite3_step(statement) == sqlite_done) { nslog(@"删除操作完成"); } } else { nslog(@"删除操作不合法"); } sqlite3_finalize(statement); [self closedb]; } #pragma mark 更新数据记录 - (void)updatedata{ sqlite3 *newdb = [self opendb]; sqlite3_stmt *statement = nil; nsstring *sqlstr = @"update saas_person set description = '喜欢运动,旅游' where name = '周玉'"; int result = sqlite3_prepare_v2(newdb, sqlstr.utf8string, -1, &statement, null); if (result == sqlite_ok) { if (sqlite3_step(statement) == sqlite_done) { nslog(@"更新信息完成"); } } else { //[logging] no such column: descript (key拼写错误) --- 更新信息不合法 nslog(@"更新信息不合法"); } sqlite3_finalize(statement); [self closedb]; } #pragma mark 新增数据记录 - (void)insertdata{ sqlite3 *newdb = [self opendb]; sqlite3_stmt *statement = nil; // nsstring *sqlstr = @"insert into saas_person (name , sex , age , description) values('周玉','男',28,'开朗乐观')"; // nsstring *sqlstr = @"insert into saas_person (name , sex , age , description) values('王鹏飞','女',27,'开朗乐观')"; // nsstring *sqlstr = @"insert into saas_person (name , sex , age , description) values('李梅','女',20,'年轻可爱��')"; nsstring *sqlstr = @"insert into saas_person (name , sex , age , description) values('sumit','男',15,'小朋友的年纪')"; //检验合法性 int result = sqlite3_prepare_v2(newdb, sqlstr.utf8string, -1, &statement, null); if (result == sqlite_ok) { //判断语句执行完毕 if (sqlite3_step(statement) == sqlite_done) { nslog(@"插入的信息完成"); } } else { nslog(@"插入的信息不合法"); } sqlite3_finalize(statement); [self closedb]; } #pragma mark 创建表 - (void)createtable{ sqlite3 *newdb = [self opendb]; // char *sql = "create table if not exists t_person (id integer primary key autoincrement not null, name text , sex text,age integer,description text);"; nsstring *sqlstr = @"create table if not exists saas_person (id integer primary key autoincrement not null, name text , sex text,age integer,description text);"; char *error = null; int result = sqlite3_exec(newdb, sqlstr.utf8string, null, null, &error); if (result == sqlite_ok) { nslog(@"创建表成功"); } else { nslog(@"创建表失败 = %s",error); } [self closedb]; } #pragma mark 删除表 - (void)droptable{ sqlite3 *newdb = [self opendb]; nsstring *sqlstr = @"drop table t_person"; char *error = null; int result = sqlite3_exec(newdb, sqlstr.utf8string, null, null, &error); if (result == sqlite_ok) { nslog(@"删除表成功"); } else { nslog(@"删除表失败 = %s",error); } [self closedb]; } #pragma mark 打开或者创建数据库 - (sqlite3 *)opendb { if (!db) { //1.获取document文件夹的路径 //参数1:文件夹的名字 参数2:查找域 参数3:是否使用绝对路径 nsstring *documentpath = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)[0]; //获取数据库文件的路径 nsstring *dbpath = [documentpath stringbyappendingpathcomponent:@"saas.sqlite"]; nslog(@"%@",dbpath); //判断document中是否有sqlite文件 int result = sqlite3_open([dbpath utf8string], &db); if (result == sqlite_ok) { nslog(@"打开数据库"); }else{ [self closedb]; nslog(@"打开数据库失败"); } } return db; } #pragma mark 关闭数据库 - (void)closedb{ int result = sqlite3_close(db); if (result == sqlite_ok) { nslog(@"数据库关闭成功"); db = nil; } else { nslog(@"数据库关闭失败"); } } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。