SQLite3数据库

SQLite3数据库,第1张

概述SQLite3采用SQLite数据库存储数据 支持SQL语句,方便查询 插件:MesaSQLite //// ViewController.m// SQLiteTest//// Created by jerehedu on 15/2/2.// Copyright (c) 2015年 jereh. All rights reserved.//#import "
sqlite3采用sqlite数据库存储数据
支持SQL语句,方便查询
 
插件:Mesasqlite
 
////  VIEwController.m//  sqliteTest////  Created by jerehedu on 15/2/2.//  copyright (c) 2015年 jereh. All rights reserved.//#import "VIEwController.h"//1.导入sqlite3头文件#import <sqlite3.h>
@interface VIEwController (){    //2.声明sqlite3对象    sqlite3 *db;}@end

#pragma mark 获得沙盒路径- (Nsstring *)getUserdocumentPath{    //获得沙盒路径    NSArray *path = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);    Nsstring *documentPath = [path lastObject];    return documentPath;}

#pragma mark 追加路径- (Nsstring *)appendingPathComponent:(Nsstring *)documentPath andfilename:(Nsstring *)filename{    Nsstring *sqlitePath = [documentPath stringByAppendingPathComponent:filename];    return sqlitePath;}

#pragma mark 创建或打开数据库-(BOol)openorCreatesqliteWithDBPath:(Nsstring *)dbpath{    const char *p = [dbpath UTF8String];    int res = sqlite3_open(p,&db);    if (res == sqlITE_OK) {        return YES;    }else{        return NO;    }}

#pragma mark 执行SQL语句-(BOol)execsqlNoqueryWithsql:(Nsstring *)sql{    int res = sqlite3_exec(db,[sql UTF8String],NulL,NulL);    if (res == sqlITE_OK){        return YES;    }else{        return NO;    }}

#pragma mark 返回stmt,无参数-(sqlite3_stmt *)execqueryWithsql:(Nsstring *)sql{    sqlite3_stmt *stmt;    int pre_res = sqlite3_prepare_v2(db,-1,&stmt,NulL);    if (pre_res == sqlITE_OK){        return stmt;    }    return NulL;}

#pragma mark 有参数-(sqlite3_stmt *)execqueryWithsql:(Nsstring *)sql anDWithParams:(NSArray *)params{    sqlite3_stmt *stmt;    int pre_res = sqlite3_prepare_v2(db,NulL);    if (pre_res == sqlITE_OK){        //绑定参数        //判断参数列表是否为空        if (params != nil) {            //循环绑定参数            for (int i = 0; i < params.count; i++) {                //                sqlite3_bind_int(stmt,i+1,[params[i] intValue]);                //要判断参数类型                ID obj = params[i];                if (obj == nil) {                    sqlite3_bind_null(stmt,i+1);                }                else if ([obj respondsToSelector:@selector(objCType)]){                    //respondsToSelector判断对象是否包含objCType方法(数字有)                    //strstr(char *,char *)判断是否在char中出现过                    if (strstr("ilsILS",[obj objCType])) {                        sqlite3_bind_int(stmt,[obj intValue]);                    }else if (strstr("fd",[obj objCType])) {                        sqlite3_bind_double(stmt,[obj doubleValue]);                    }else{                        stmt = NulL;                    }                }else if ([obj respondsToSelector:@selector(UTF8String)]){                    sqlite3_bind_text(stmt,[obj UTF8String],NulL);                }else{                    stmt = NulL;                }            }        }                return stmt;    }    return NulL;}

@implementation VIEwController- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];//获得沙盒路径    Nsstring *dbPath;    dbPath = [self getUserdocumentPath];        Nsstring *sqlitePath;    sqlitePath = [self appendingPathComponent:dbPath andfilename:@"test.sqlite"];//追加路径(实际不存在)    NSLog(@"%@",dbPath);//5.判断是否打开成功    if ([self openorCreatesqliteWithDBPath:sqlitePath]) {        NSLog(@"db is open");        //构造SQL语句        Nsstring *sql = @"create table if not exists demo(d_ID integer primary key autoincrement,d_name varchar(20))";        int exec_res = sqlite3_exec(db,NulL);        if (exec_res == sqlITE_OK) {            NSLog(@"table is created");        }        /*         参数:         1.sqlite3 对象         2.SQL语句         3.回调函数         4.回调函数的参数         5.错误信息         */                //插入记录,构造SQL语句        Nsstring *insert_sql = @"insert into demo(d_name) values ('zhangziyao')";if ([self execsqlNoqueryWithsql:insert_sql]) {            NSLog(@"one recoder is inserted");        }                //修改        Nsstring *update_sql = @"update demo set d_name = 'chenyi' where d_ID = 1";if ([self execsqlNoqueryWithsql:update_sql]) {            NSLog(@"one recoder is updated");        }                //删除        Nsstring *delete_sql = @"delete from demo where d_name='wuyuqiu'";if ([self execsqlNoqueryWithsql:delete_sql]) {            NSLog(@"one recoder is deleted");        }                //查询int search_d_ID = 3;        Nsstring *search_name = @"_h%";        Nsstring *search_sql = @"select * from demo where d_ID>? and d_name like ?";sqlite3_stmt *stmt;if ([self execqueryWithsql:search_sql]) {            stmt = [self execqueryWithsql:search_sql anDWithParams:@[[NSNumber numberWithInt: search_d_ID],search_name]];            while (sqlite3_step(stmt) == sqlITE_ROW) {                int d_ID = sqlite3_column_int(stmt,0);                const unsigned char *d_name = sqlite3_column_text(stmt,1);                Nsstring *name = [Nsstring stringWithUTF8String:(char*)d_name];                NSLog(@"ID=%d,name=%@",d_ID,name);                _IDLabel.text = [Nsstring stringWithFormat:@"ID=%d",d_ID];                _nameLabel.text = [Nsstring stringWithFormat:@"name=%@",name]; }        }        //释放stmt        sqlite3_finalize(stmt);                //关闭数据库        sqlite3_close(db);                NSNumber *n = [NSNumber numberWithDouble:1.0];        NSLog(@"%s",[n objCType]);    }}- (voID)dIDReceiveMemoryWarning{    [super dIDReceiveMemoryWarning];    // dispose of any resources that can be recreated.}@end
总结

以上是内存溢出为你收集整理的SQLite3数据库全部内容,希望文章能够帮你解决SQLite3数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1170901.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存