1

I tried to select data from a table:

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");
    }

I do this select and in the time i insert data to another Table in the database with this :

    -(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);
    }
}

This code work fine if i don't insert a data and select in the same time, and the insert and the select is on different tables.

Edit

when i try to make a select i get "Error" output from : NSLog(@"Error");

Edit 2

When i add sqlite3_errmsg(database) to the select method i get in the console:

reason: 'Error while inserting data. 'database is locked'
2
  • have you open database before fire query on that (sqlite3_open([/*database path*/ UTF8String], &database)!= SQLITE_OK) Commented Apr 19, 2012 at 13:10
  • You can't access the database, even if it is two different tables at the same time from two different threads. Commented Apr 19, 2012 at 13:13

1 Answer 1

1

after the select query please finalize the sql statement

 sqlite3_finalize(statement);

and solve your problem

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.