SQLite-OC

SQLite-OC,第1张

概述打开 -(void)openSqlite:(NSString *)sqliteName{ if (!sqliteName || sqliteName.length <= 0) { NSAssert(sqliteName, @"sqliteName is null"); } if (!db) { NSString *basePath = [NS 打开
-(voID)opensqlite:(Nsstring *)sqlite@R_301_6889@{    if (!sqlite@R_301_6889@ || sqlite@R_301_6889@.length <= 0) {        NSAssert(sqlite@R_301_6889@,@"sqlite@R_301_6889@ is null");    }    if (!db) {        Nsstring *basePath = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES) firstObject];        Nsstring *path = [basePath stringByAppendingPathComponent:sqlite@R_301_6889@];        NSLog(@"sqlite 存放位置:%@",path);        //打开数据库,若存在则打开;反之不存在就创建一个数据库再打开        int result = sqlite3_open([path UTF8String],&db);        if (result == sqlITE_OK) {            NSLog(@"数据库打开成功");            return;        } else {            NSLog(@"数据库打开失败");        }    }}

创建表 & 删除表(表结构)

- (BOol)createtable{    Nsstring *sql = @"create table if not exists 'student' ('number' integer primary key autoincrement not null,'@R_301_6889@' text,'sex' text,'age' integer,'score' integer default 0);";    char *error = NulL;    int result = sqlite3_exec(db,[sql UTF8String],nil,&error);    return (result == sqlITE_OK);}-(voID)droptable{    Nsstring *sql = @"DROP table if EXISTS student;";    char *error = NulL;    int ret = sqlite3_exec(db,NulL,&error);    if (ret == sqlITE_OK) {        NSLog(@"删除表格成功");    } else {        NSLog(@"删除失败:%s",error);    }}

表的增删改查

- (voID)insertData{    Nsstring *sql = @"INSERT INTO student (@R_301_6889@,sex,age,score) VALUES ('yy','1',28,99);";    char *error = NulL;    int ret = sqlite3_exec(db,NulL,&error);    if (ret == sqlITE_OK) {        NSLog(@"插入成功");    } else {        NSLog(@"插入失败:%s",error);    }}- (voID)deleteData{    Nsstring *sql = @"DELETE FROM student WHRER number = 1;";    int ret = sqlite3_exec(db,NulL);    if (ret == sqlITE_OK) {        NSLog(@"删除成功");    } else {        NSLog(@"删除失败");    }}- (voID) selectData{    Nsstring *sql = @"SELECT @R_301_6889@,score FROM student;";    sqlite3_stmt *stmt;    int ret = sqlite3_prepare_v2(db,-1,&stmt,NulL);    if (ret == sqlITE_OK) {        // 查询成功 遍历结果集        while (sqlite3_step(stmt) == sqlITE_ROW) {             const unsigned char *@R_301_6889@ = sqlite3_column_text(stmt,0);            NSInteger score = sqlite3_column_int(stmt,1);            NSLog(@"%@ 分数 %ld",[Nsstring stringWithUTF8String:(const char *)@R_301_6889@],(long)score);        }    } else {        NSLog(@"查询失败");    }}

解决打印乱码问题
Sending 'const unsigned char *' to parameter of type 'const char * _Nonnull' converts between pointer

强制转化

const unsigned char *@R_301_6889@ = sqlite3_column_text(stmt,0); NSInteger score = sqlite3_column_int(stmt,1); NSLog(@"%@ 分数 %ld",[Nsstring stringWithUTF8String:(const char *)@R_301_6889@],(long)score);

总结

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

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

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

原文地址: http://outofmemory.cn/sjk/1161607.html

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

发表评论

登录后才能评论

评论列表(0条)

保存