iphone开发中sqlite3常用操作

一使用原生库

1新建或插入

sqlite3 *db;
	if (sqlite3_open(@"xx.db", &db) != SQLITE_OK ) {
		return;
	}	
	sqlite3_stmt *dbps;
        int dbrc;
        NSString *sql = @"";
	const char *sqlChar = [sql UTF8String];	
	dbrc = sqlite3_prepare_v2(db, sqlChar, -1, &dbps, NULL);
	dbrc = sqlite3_step(dbps);
	if(dbrc != SQLITE_DONE){
		NSLog(@"exec sql error!,sql:%@,error code:%d.",sql,dbrc);
	}
	sqlite3_finalize(dbps);
	sqlite3_close(db);

2查询

sqlite3 *db;
	int dbrc;
	sqlite3_stmt *dbps;
	if (sqlite3_open(@"xx.db", &db) != SQLITE_OK ) {
		NSLog(@"couldnet't open db:");
		return;
	}
	NSString *sql = @"select * from sqlite3_master ";
	const char *sqlChar = [sql UTF8String]; 

	dbrc = sqlite3_prepare_v2 (db, sqlChar, -1, &dbps, NULL);
	while ((dbrc = sqlite3_step (dbps)) == SQLITE_ROW) {
		NSLog([[NSString alloc] initWithUTF8String:(char*) sqlite3_column_text(dbps, 0)]);
	}
	sqlite3_finalize(dbps);
	sqlite3_close(db);

二使用CSqliteDatabase

待续

相关推荐