1

我试图从表中选择数据:

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(@"Error");

编辑 2

当我添加sqlite3_errmsg(database)到选择方法时,我进入控制台:

reason: 'Error while inserting data. 'database is locked'
4

1 回答 1

1

选择查询后请敲定sql语句

 sqlite3_finalize(statement);

并解决您的问题

于 2012-04-19T13:34:40.017 回答