IOS SQLite基本操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS SQLite基本操作相关的知识,希望对你有一定的参考价值。

再所有操作之前先添加libsqlite这个库以及引入sqlite3.h头文件

一,创建数据库打开数据库,关闭数据库

    NSString *sqlPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    sqlPath = [_sqlPath stringByAppendingPathComponent:@"students.sqlite"];
    sqlite3 *sql;
    int result =  sqlite3_open(self.sqlPath.UTF8String, &sql);
    if(result == SQLITE_OK)
    {
        NSLog(@"打开数据库成功");
        self.sql = sql;
        return YES;
    }
    NSLog(@"打开数据库失败");
    sqlite3 *sql = self.sql;
    if(sqlite3_close(sql) == SQLITE_OK){
        NSLog(@"关闭数据库成功");
        return YES;
    }

二、插入数据

        拼接语句

    [self openDataBase];
    NSString *sqlString = [NSString stringWithFormat:@"insert into t_student(name) values(‘毛小东‘)"];
    sqlite3 *sql = self.sql;
    char *error;
    int result = sqlite3_exec(sql, sqlString.UTF8String,NULL, NULL, &error);
    if(result == SQLITE_OK)
        NSLog(@"插入成功");
    [self closeDataBase];

  暂位符方式

     

    NSString *sqlString = @"insert into t_student(name) values(?)";
    sqlite3_stmt *stmt;
    int result  = sqlite3_prepare_v2(self.sql, sqlString.UTF8String, -1, &stmt, NULL);
    if(result == SQLITE_OK){
        sqlite3_bind_text(stmt, 1, [NSString stringWithFormat:@"叶衍宏"].UTF8String, -1, NULL);
    }
    int resultInsert = sqlite3_step(stmt);
    if(resultInsert == SQLITE_DONE)
        NSLog(@"插入成功");
    
    [self closeDataBase];

二,查询

   

    [self openDataBase];
    NSString *sqlString = [NSString stringWithFormat:@"select id,name from t_student"];
    sqlite3_stmt *stmt;
    int result = sqlite3_prepare_v2(self.sql, sqlString.UTF8String, -1, &stmt, NULL);
    if(result == SQLITE_OK){
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int studentId = sqlite3_column_int(stmt, 0);
            NSString *studentName =  [NSString stringWithCString:(const char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
            NSLog(@"ID = %d  姓名 = %@",studentId,studentName);
        }
    }
    sqlite3_finalize(stmt);
    [self closeDataBase];

三,删除

   

    [self openDataBase];
    NSString *sqlString = [NSString stringWithFormat:@"delete from t_student where id = 5"];
    char *error;
    int result = sqlite3_exec(self.sql, sqlString.UTF8String, NULL, NULL, &error);
    if(result == SQLITE_OK)
        NSLog(@"成功");
    [self closeDataBase];

四,更改

  

    [self openDataBase];
    NSString *sqlString = [NSString stringWithFormat:@"update t_student set name = ‘毛茸茸‘ where id = 3"];
    char *error;
    int result = sqlite3_exec(self.sql, sqlString.UTF8String, NULL, NULL, &error);
    if(result == SQLITE_OK)
        NSLog(@"修改成功");
    [self closeDataBase];

  

五,常用方法

 

//打开数据库函数,

SQLITE_API int SQLITE_STDCALL sqlite3_open(

  const char *filename,   /* Database filename (UTF-8) *///数据库文件路径

  sqlite3 **ppDb          /* OUT: SQLite db handle */ //数据库句柄

);

//关闭数据库

SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*);

 

//执行SQL语句,可以执行增删查改等操作,不过一般不拿来执行查操作,因为返回不是所查询的数据      

SQLITE_API int SQLITE_STDCALL sqlite3_exec(

  sqlite3*,                                  /* An open database *///数据库句柄

  const char *sql,                           /* SQL to be evaluated *///SQL语句

  int (*callback)(void*,int,char**,char**),  /* Callback function *///执行完毕回调

  void *,                                    /* 1st argument to callback *///回调block第一个参数

  char **errmsg                              /* Error msg written here *///错误信息

);

//检查SQL语句

SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(

  sqlite3 *db,            /* Database handle */  //数据库句柄

  const char *zSql,       /* SQL statement, UTF-8 encoded */ // SQL语句

  int nByte,              /* Maximum length of zSql in bytes. */  SQL语句最大长度,-1为不检查

  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */  sqlite3_stmt 实例,可以获取数据

  const char **pzTail     /* OUT: Pointer to unused portion of zSql */

);

DEMO:http://pan.baidu.com/s/1mhdPQp6

以上是关于IOS SQLite基本操作的主要内容,如果未能解决你的问题,请参考以下文章

使用片段从数据库 SQLite 获取数据时出错

iOS之Sqlite3封装

java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段

如何增加 Sqlite FTS4 片段大小

SQLite 片段函数实现不会在 TextView 中将文本格式化为 HTML

当我从用户获取数据并将其保存到 SQLite 数据库中时,我应该怎么做才能使列表视图在片段中工作