我尝试从表格中选择数据:
NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
const char *sql = "SELECT name FROM Artists ORDER BY name";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *)sqlite3_column_text(statement, 0);
NSString *name = [NSString stringWithUTF8String: nameChars];
[tmpArray addObject:name];
}
}else {
NSLog(@"Error");
}
}else {
NSLog(@"Error1");
}
我这样选择并在我将数据插入数据库中的另一个表时使用:
-(void)insertAutpPlaylist:(NSString*)playlistName withPlaylist:(NSMutableArray*)songsArray{
if (sqlite3_open([dataPath UTF8String], &database) == SQLITE_OK) {
for (int i = 0 ; i < [songsArray count]; i++) {
SongItem *song = [songsArray objectAtIndex:i];
sqlite3_stmt *insertStmt = nil;
NSString *name = song.name;
if(insertStmt == nil)
{
NSString *statement = [NSString stringWithFormat:@"INSERT INTO %@ (name) VALUES (?)",playlistName];
const char *insertSql = [statement UTF8String];
if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK){
NSLog(@"Error while creating insert statement.");
insertStmt = nil;
continue;
}
sqlite3_bind_text(insertStmt, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insertStmt)){
//NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
NSLog(@"Error while inserting data.");
insertStmt = nil;
continue;
}
else{}
sqlite3_reset(insertStmt);
insertStmt = nil;
}
}
sqlite3_close(database);
}
}
如果我不插入数据并在同一时间选择,并且插入和选择在不同的表上,则此代码可以正常工作。
当我尝试进行选择时,我从“NSLog(@”错误“)获得”错误“输出;
当我将sqlite3_errmsg(database)
添加到我在控制台中的select方法时:
reason: 'Error while inserting data. 'database is locked'
答案 0 :(得分:1)
在select查询之后请完成sql语句
sqlite3_finalize(statement);
并解决您的问题