简体   繁体   English

iPhone:SQLite存储数据不起作用

[英]iPhone: SQLite store data is not working

I am trying to store data in created SQLite database. 我正在尝试将数据存储在创建的SQLite数据库中。 Here is the code below, i am storing urlStr into a database. 这是下面的代码,我将urlStr存储到数据库中。 Later, i'm planning to store multiple URL strings into the database. 稍后,我打算将多个URL字符串存储到数据库中。 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" VARCHAR,"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? 当我调试代码时,没有错误,直到sqlite3_bind_text并执行该行。.有人可以纠正我在这里做错的事情吗,为什么它没有将文本绑定到数据库中?

Thank you! 谢谢!

I think you are not actually executing the query. 我认为您实际上并没有执行查询。 Call sqlite3_step(sqlite3_stmt*); 调用sqlite3_step(sqlite3_stmt*); between binding values and finalizing. 在绑定值和完成之间。

(but really, you want to use Core Data for this!) (但实际上,您想为此使用Core Data!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM