zoukankan      html  css  js  c++  java
  • Ios开发之sqlite

      Sqlite是ios数据存储的一个重要手段,今天我们就一块来看一下,怎样使用sqlite将数据存储到沙盒中去。

      第一步:导入一个框架libsqlite3.0.dylib

      选中TARGETS在General中的Linked Frameworks and Libraries选项中点击‘+’添加

    第二步:代码部分

        1.找到沙盒中的路径

    NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [path objectAtIndex:0];
     NSString *dbPath = [documentPath stringByAppendingPathComponent:@"sqlite.db"];

        2.创建sqlite对象,并执行打开操作

    sqlite3 *db;
        //打开数据库
    int res = sqlite3_open(dbPath.UTF8String, &db);

    3.创建表

    NSString *sql = @"create table if not exists 'student' ('id' integer primary key autoincrement,'studentname' varchar)";
    int sqlRes = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
     if (sqlRes == SQLITE_OK) {
         NSLog(@"create table ok");
     }

    4.向表中插入数据

    //增 NSString *sql1 = @"insert into student (studentname) values ('小帐')";
    int sql1Res = sqlite3_exec(db, sql1.UTF8String, NULL, NULL, NULL);
    if (sql1Res == SQLITE_OK) {
        NSLog(@"insert ok");
    }

        5.从表中删除数据

    NSString *deletesql  = @"delete from student where id = 3";
    int deleres = sqlite3_exec(db, deletesql.UTF8String, NULL, NULL, NULL);
    if (deleres == SQLITE_OK) {
         NSLog(@"delete is ok");
    }

    6.修改表中的数据

    //修改
    NSString *updateSql = @"update student set studentname = 'wangling' where id = 4";
    int updateres = sqlite3_exec(db, updateSql.UTF8String, NULL, NULL, NULL);
    if (updateres == SQLITE_OK) {
          NSLog(@"update is ok");
    }

            7.查询数据

      查询数据和,增删改都有所不同,我们需要首先创建sqlite_stmt对象,用来存放我们查询的数据流(二进制),然后使用sqlite3_prepare_v2函数准备,通过sqlite3_bind_添加查询参数最后获取数据,最后关闭sqlite_stmt对象。

    //
            sqlite3_stmt *stmt;
            NSString *selectSql = @"select * from student where id = ?";
    
            if (sqlite3_prepare_v2(db, selectSql.UTF8String, -1, &stmt, nil) == SQLITE_OK) {
                sqlite3_bind_int(stmt, 1, 4);
                while (sqlite3_step(stmt) == SQLITE_ROW) {
                    int identity = sqlite3_column_int(stmt, 0);
                    NSLog(@"%d",identity);
                    char * name= (char *)sqlite3_column_text(stmt, 1);
                    NSLog(@"%@",[NSString stringWithUTF8String:name]);
                }
                if (stmt) {
                    sqlite3_finalize(stmt);
    }

    8.关闭数据库sqlite对象

    if (db) {
            sqlite3_close(db);
        }
    作者:杰瑞教育
    出处:http://www.cnblogs.com/jerehedu/ 
    版权声明:本文版权归杰瑞教育技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    技术咨询:JRedu技术交流
     
  • 相关阅读:
    How to alter department in PMS system
    Can't create new folder in windows7
    calculate fraction by oracle
    Long Wei information technology development Limited by Share Ltd interview summary.
    ORACLE BACKUP AND RECOVERY
    DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
    ORACLE_TO_CHAR Function
    电脑BOIS设置
    JSP点击表头排序
    jsp+js实现可排序表格
  • 原文地址:https://www.cnblogs.com/jerehedu/p/5181431.html
Copyright © 2011-2022 走看看