Sqlite的使用

(1)创建数据库,先创建数据库文件
NSString *pathString = [ NSHomeDirectory () stringByAppendingString : @"/Documents/Note.sqlite" ];
打开数据库
sqlite3_open()函数有两个参数第一个参数是路径的名字装换成utf8编码,第二个为数据库对象。此步骤为打开数据库,如果打开成功函数返回SQLITE_OK.调用打开数据库函数需用 sqlite3_close ( db );关闭函数关闭.
if ( sqlite3_open ([pathString UTF8String ], & db )!= SQLITE_OK ) {
       
NSLog ( @" 打开数据库失败! " );
       
sqlite3_close ( db );
    }
else {
       char *err;
创建SQL语句,此语句为创建表并指明表中包含的字段.
        NSString *createSQL = [ NSString stringWithFormat : @"CREATE TABLE USER (name TEXT NOT NULL, age INTEGER, sex TEXT, image BLOB, id INTEGER PRIMARY KEY AUTOINCREMENT)" ];
通过调用sqlite3_exec()函数来执行SQL语句,此函数第一个参数为数据库第二个参数为SQL语句,第五格参数为错误信息。
        if ( sqlite3_exec ( db , [createSQL UTF8String ], NULL , NULL , &err)!= SQLITE_OK ) {
           
NSLog ( @" 创建表失败! " );
        }
else {
           
sqlite3_close ( db );
        }
       
sqlite3_close ( db );
    }
(2)查询数据库,查询数据库先打开数据库同上
定义SQL查询语句 NSString *qSQL = @"SELECT * FROM USER where name =?" ;
sqlite3_stmt *statement;//占位符绑定参数的宿主语句
调用sqlite3_prepare_v2()函数编译SQL语句,编译后会使查询效率提高(个人理解),第一个参数为数据库文件,第二个为SQL语句,第三个为-1,第四个为SQL语句中的占位符,此函数编译成功会返回SQLITE_OK.
        if ( sqlite3_prepare_v2 ( db , [qSQL UTF8String ], - 1 , &statement, NULL ) == SQLITE_OK ) {
           // 绑定参数开始
通过 sqlite3_bind_text()函数给占位符绑定参数,第一个参数为绑定参数的对象,第二个表示是第几个占位符,第三个参数为占位符字符。
            sqlite3_bind_text (statement, 1 , [name UTF8String ], - 1 , NULL );
调用 sqlite3_step 一次或多次来执行这个 sql, 这个过程用于执行有前面 sqlite3_prepare 创建的准备语句。这个语句执行到结果的第一行可用的位置。继续前进到结果的第二行的话,只需再次调用 sqlite3_setp() 。继续调用 sqlite3_setp() 知道这个语句完成,那些不返回结果的语句(如: INSERT UPDATE ,或 DELETE ), sqlite3_step() 只执行一次就返回。
            if ( sqlite3_step (statement)== SQLITE_ROW ) {
调用sqlite3_column_text (statement, 0 )函数返回第0列的值
                char * name = ( char *) sqlite3_column_text (statement, 0 );
               
NSString *nsname = [[ NSString alloc ] initWithUTF8String :name];
               
int age = ( int ) sqlite3_column_int (statement, 1 );
               
char * sex = ( char *) sqlite3_column_text (statement, 2 );
               
NSString *nssex = [[ NSString alloc ] initWithUTF8String :sex];
               
Note *note = [[ Note alloc ] init ];
                note.
name = nsname;
                note.
age = age;
                note.sex = nssex;
调用sqlite3_finalize (statement)函数释放资源
                sqlite3_finalize (statement);
关闭数据库
                sqlite3_close ( db );
               
return note;
            }
        }
       
sqlite3_finalize (statement);
       sqlite3_close(db);
(3)插入数据
- ( int )create:( Note *)model{
   
NSString *pathString = [ NSHomeDirectory () stringByAppendingString : @"/Documents/Note.sqlite" ];
   
NSLog ( @"%@" ,pathString);
   
if ( sqlite3_open ([pathString UTF8String ], & db )!= SQLITE_OK ) {
       
NSLog ( @" 打开数据库失败! " );
       
sqlite3_close ( db );
    }else{
设置插入语句
        NSString *sql = @"INSERT OR REPLACE INTO USER (name,age,sex) VALUES (?,?,?)" ;
       
sqlite3_stmt *statement;
       
if ( sqlite3_prepare_v2 ( db , [sql UTF8String ], - 1 , &statement, NULL ) == SQLITE_OK ) {
           
sqlite3_bind_text (statement, 1 , [model. name UTF8String ], - 1 , NULL );
           
sqlite3_bind_int64 (statement, 2 , model. age );
           sqlite3_bind_text(statement, 3, [model.sex UTF8String], -1, NULL);
执行插入语句
            if ( sqlite3_step (statement)!= SQLITE_DONE ){
               
NSLog ( @" 插入数据失败! " );
            }
        }
       
sqlite3_finalize (statement);
       
sqlite3_close ( db );
    }
   
return 0 ;
}
(4)修改数据操作跟插入一样只是sql语句变了一下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值