开发者

iPhone: SQLite store data is not working

I am trying to store data in created SQLite database. Here is the code below, i am storing urlStr into a database. Later, i'm planning to store multiple URL strings into the database. I'm trying this as sample now. The problem is, it is not binding any text into the database after this code executed.

-(void) readMediaListFromDatabase {

NSString * urlStr = @"assets-library://asset/asset.JPG?id=1000000001&ext=JPG";

// Setup the database object
sqlite3 *database;

// Setup some globals
databaseName = @"MediaList.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    // Table name is Medialist -> CREATE TABLE "Medialist" ("id" VAR开发者_Go百科CHAR,"pathURL" VARCHAR)
    const char *sqlStatement = "select * from Medialist";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            //NSString *aMediaData = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            //NSLog(@"aMediaData: %@", aMediaData);

        }

        //int success = sqlite3_step(compiledStatement);
        //NSLog(@"sqlite3_step: %d", success);

        // Write the URL path into the database 
        const char *sql = "insert into Medialist(pathURL) values(?)";

        if ( sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) !=SQLITE_OK )
        {                
            NSLog(@"SQLite prepare error before binding data: %s", sqlite3_errmsg(database));
        }

        sqlite3_bind_text(compiledStatement, 1, [urlStr UTF8String], -1, SQLITE_TRANSIENT);

    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

}

When i debug the code, there are no errors, it is coming until sqlite3_bind_text and executing that line too.. Could someone please correct me what i'm doing wrong here, why it is not binding the text into the database?

Thank you!


I think you are not actually executing the query. Call sqlite3_step(sqlite3_stmt*); between binding values and finalizing.

(but really, you want to use Core Data for this!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜