zoukankan      html  css  js  c++  java
  • 数据存储SQLite数据库操作

    SQLite,是一款轻型的数据库,它设计目的是为了嵌入式程序的使用。Sqlite是由C实现,占用内存小,而且运行效率很高。

    所以现在也被广泛用于包括浏览器(支持html5的大部分浏览器,ie除外)、ios、Android、window phone等移动设备以及

    一些便携需求的小型web应用系统。

    由于sqlite的使用广泛,不能一一涉及,因此在这里就讨论一下SQLite数据库在ios开发中的一些使用方法。

    在使用sqlite前需要首先引入sqlite的库,由于sqlite是用C写的,objc可以直接使用C代码。但在使用sqlite前,一般都会使用Cocoatouch框架,这样可以与objc保持一致,有利于开发的方便。

    这里引用的Frameworks就是libsqlite3.dylib或libsqlite3.0.dylib。

    在完成Frameworks引用之后,需要在操作sqlite数据库的.m文件中引入相应的头文件。

    #import "sqlite3.h",

    这样这能使用sqlite数据库了。

    而数据库中最常用的操作就是创建数据库、创建表、对数据的增删改查、删除数据库和删除表。下面对这些操作进行一一示范。

    在使用数据库之前需要建立相应数据库文件,而数据库文件是保存在文件系统。因此在创建或打开数据库之前,需要清楚数据库所在位置。这里我们把数据库文件放到Documents目录下。

    在.h文件中也加入#import“sqlite3.h”,并建立一个成员变量,

    sqlite3*database;

    在.m文件中的代码如下:

        //找到Documents目录

        NSArray *docArr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSAllDomainsMask, YES);

        //选用数组中的第一个文件路径

        NSString *docPaths = [docArr objectAtIndex:0];

        //为自己的数据库文件建立相应的文件路径

        NSString *sqlitePaths = [docPaths stringByAppendingPathComponent:@"testdb.sqlite"];

        //判断数据库文件是否存在,存在既打开,不存在就创建,sqlite3_open的第一个参数是指数据库文件名称,

        //第二个参数是指数据库句柄

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

            NSLog(@"open sqlite db ok!");

    }

    在数据库打开后,如果没有相应的数据表,需要建立相应的数据表。下面就是建表的代码:

    //在数据库打开后,如果没有数据表,需要建立相应的数据表

    - (void)createTable

    {

        //收集错误信息字符

        char *errorMsg;

        constchar *createSql = "create table if not exists persons(idinteger primary key autoincrement,name kext)";

        if (sqlite3_exec(database, createSql,NULL, NULL, &errorMsg) ==SQLITE_OK) {

            NSLog(@"create ok.");}

    }

    这里要特别注意errorMsg传的是地址,因为该函数要通过地址引用来写报错字符信息。

    建立过数据表之后,需要向表中插入数据。

    //向表中插入数据

    - (void)insertValueIntoTable

    {

        char *errorMsg;

        constchar *insertSql = "insert into persons(name) values('张三')";

        if (sqlite3_exec(database, insertSql,NULL, NULL, &errorMsg) ==SQLITE_OK) {

            NSLog(@"insert ok.");

        }

    }

        //收集错误信息字符

    在插入数据之后,为进行数据集的查询,需要用到statement。代码如下:

    //数据集的查询

    - (void)queryDataSet

    {

        //查询语句

        constchar *selectSql = "select id,name from persons";

        //sqlite3_stmt接收经过sqlite解析后的语句

        sqlite3_stmt *statement;

        //进行sqlite语句查询

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

            NSLog(@"select ok.");

        }

        //把结果集输出

        while (sqlite3_step(statement) ==SQLITE_ROW) {

            //对每一行数据的解析,sqlite3_column_int中的第一个字段是解析的语句,第二个字段是去表明表中的第几个字段

            int _id = sqlite3_column_int(statement,0);

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

            NSLog(@"row>>>id %i,name %@",_id,name);

        }

        //销毁一个使用过的解析语句对象

        sqlite3_finalize(statement);

    }

    对数据库中已有数据进行更新操作。代码如下:

    //对于表中已有数据的更新

    - (void)updateDataInTable

    {

        //收集错误信息字符

        char *errorMsg;

        //更新数据语句

        constchar *updateSql = "update persons set name = '李四'where name = '张三'";

        if (sqlite3_exec(database, updateSql,NULL, NULL, &errorMsg) ==SQLITE_OK) {

            NSLog(@"update ok.");

        }

    }

    删除已有的数据。代码如下:

    //删除表中的数据

    - (void)deleteDataInTable

    {

        //收集错误信息字符

        char *errorMsg;

        //删除数据语句

        constchar *deleteSql = "delete from persons where name = '张三'";

        if (sqlite3_exec(database, deleteSql,NULL, NULL, &errorMsg) ==SQLITE_OK) {

            NSLog(@"delete ok.");

        }

    }

    上面只是简单的介绍了一些在ios开发中简单的对sqlite数据库的操作。还有好多深入的操作,待后面慢慢讲来。

    Demo地址:

    免费下载地址在 http://linux.linuxidc.com/

    用户名与密码都是www.linuxidc.com

    具体下载目录在 /2013年资料/1月/20日/数据存储之SQLite数据库操作

  • 相关阅读:
    nginx-1.8.1的安装
    ElasticSearch 在3节点集群的启动
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
    sqoop导入导出对mysql再带数据库test能跑通用户自己建立的数据库则不行
    LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
    LeetCode 437. Path Sum III (路径之和之三)
    LeetCode 404. Sum of Left Leaves (左子叶之和)
    LeetCode 257. Binary Tree Paths (二叉树路径)
    LeetCode Questions List (LeetCode 问题列表)- Java Solutions
    LeetCode 561. Array Partition I (数组分隔之一)
  • 原文地址:https://www.cnblogs.com/jiangshiyong/p/2975795.html
Copyright © 2011-2022 走看看