iOS 使用sqlite3 建表,删除表,添加记录, 查询记录

1.打开数据库

NSString *DBPath = @"/Users/administrator/Movies/LanGu.sqlite3";

    sqlite3  *database = NULL;

    char           *errMsg;

    //1  创建或打开数据库

    if(sqlite3_open([DBPath UTF8String], &database)!=SQLITE_OK){

        NSLog(@"open error");

    }else{

        NSLog(@"open success");

    }

2.建表

const char *createTable1 = "CREATE TABLE IF NOT EXISTS LGRB(TITLE Text,CONTEXT Text,IMAGE BLOB)";

    if(sqlite3_exec(database, createTable1, NULL, NULL, &errMsg)!=SQLITE_OK){

        NSLog(@"Failed to create table1");

    }else{

        NSLog(@"succeed to create table1");

    }

3.   删除表 

NSString *dropTable1 = @"DROP TABLE JYDT";

    const char *drop_stmt = [dropTable1 UTF8String];

    if(sqlite3_exec(database, drop_stmt, NULL, NULL, &errMsg)==SQLITE_OK)

    {

        NSLog(@"删除成功");

    }else{

        NSLog(@"删除不成功");

    }

4. 查询结果集

NSString *title;

    const char *selectSql1 = "SELECT TITLE FROM LGGG WHERE rowid=1";

    sqlite3_stmt *statement;

    if(sqlite3_prepare_v2(database, selectSql1, -1, &statement, nil)==SQLITE_OK){

        NSLog(@"select ok");

    }

    while (sqlite3_step(statement)==SQLITE_ROW) {

        title = [[NSString alloc]initWithCString:(char *)sqlite3_column_text(statement, 0) encoding:NSUTF8StringEncoding];

        NSLog(@"%@",title);

    }

    sqlite3_finalize(statement);




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值