1 打开数据库
1) 需要制定数据库的路径
NSString
*filePath = [
NSHomeDirectory
()
stringByAppendingFormat
:
@"/Documents/data.sqlite"
];
2)创建数据库的对象
sqlite3
*qingyunDb =
NULL
;
3)打开命令:
sqlite3_open
([dbFilePath
UTF8String
], &qingyunDb);
2 操作数据 DML
//
数据库表的建立
char *error = nil ;
NSString *sql = @"create table pop (id int,name text,city text)" ;
sqlite3_exec (sqlite, [sql UTF8String ], NULL , NULL , &error);
// 数据插入操作
// NSString *insertSql = @"insert into pop values(400,'three',22)";
// sqlite3_exec(sqlite, [insertSql UTF8String], NULL, NULL, NULL);
// 数据的更新
//NSString *updateSql = @"update pop set id = 600 where city = 22";
//result = sqlite3_exec(sqlite , [updateSql UTF8String], NULL, NULL, NULL);
// 删除数据
NSString *deleteSql = @"delete from pop where name = 'apple'" ;
char *error = nil ;
NSString *sql = @"create table pop (id int,name text,city text)" ;
sqlite3_exec (sqlite, [sql UTF8String ], NULL , NULL , &error);
// 数据插入操作
// NSString *insertSql = @"insert into pop values(400,'three',22)";
// sqlite3_exec(sqlite, [insertSql UTF8String], NULL, NULL, NULL);
// 数据的更新
//NSString *updateSql = @"update pop set id = 600 where city = 22";
//result = sqlite3_exec(sqlite , [updateSql UTF8String], NULL, NULL, NULL);
// 删除数据
NSString *deleteSql = @"delete from pop where name = 'apple'" ;
result = sqlite3_exec(sqlite , [deleteSql UTF8String], NULL, NULL, NULL);
//
查询数据库里面的数据
NSString *SelectSql = @"select * from pop where id = ? or name = ?" ;
// 设置句柄
sqlite3_stmt * stmt = NULL ;
// 需要先编译
result = sqlite3_prepare (sqlite , [SelectSql UTF8String ], - 1 , &stmt, NULL );
// 绑定?号指的内容
sqlite3_bind_int (stmt, 1 , 300 );
sqlite3_bind_text (stmt, 2 , "one" , - 1 , NULL );
// 浮标方式,开始查询
result = sqlite3_step (stmt);
while (result == SQLITE_ROW ) {
NSString *iid = [ NSString stringWithFormat : @"%d" , sqlite3_column_int (stmt, 0 )];
NSString *name = [ NSString stringWithFormat : @"%s" , sqlite3_column_text (stmt, 1 )];
NSString *city = [ NSString stringWithFormat : @"%s" , sqlite3_column_text (stmt, 2 )];
NSLog ( @"%@,%@,%@" ,iid,name,city);
result = sqlite3_step (stmt);
NSString *SelectSql = @"select * from pop where id = ? or name = ?" ;
// 设置句柄
sqlite3_stmt * stmt = NULL ;
// 需要先编译
result = sqlite3_prepare (sqlite , [SelectSql UTF8String ], - 1 , &stmt, NULL );
// 绑定?号指的内容
sqlite3_bind_int (stmt, 1 , 300 );
sqlite3_bind_text (stmt, 2 , "one" , - 1 , NULL );
// 浮标方式,开始查询
result = sqlite3_step (stmt);
while (result == SQLITE_ROW ) {
NSString *iid = [ NSString stringWithFormat : @"%d" , sqlite3_column_int (stmt, 0 )];
NSString *name = [ NSString stringWithFormat : @"%s" , sqlite3_column_text (stmt, 1 )];
NSString *city = [ NSString stringWithFormat : @"%s" , sqlite3_column_text (stmt, 2 )];
NSLog ( @"%@,%@,%@" ,iid,name,city);
result = sqlite3_step (stmt);
}
//
关闭数据库句柄
sqlite3_finalize
(stmt);
3
/
关闭数据库
sqlite3_close(sqlite);