2011-03-27 95 views
0

我想将数据存储在创建的SQLite数据库中。这里是下面的代码,我将urlStr存储到数据库中。后来,我打算将多个URL字符串存储到数据库中。我现在正在尝试这个样本。问题是,执行此代码后,它没有将任何文本绑定到数据库中。iPhone:SQLite存储数据不起作用

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

}

当我调试的代码,没有错误,它是未来,直到sqlite3_bind_text和执行这行太.. 可能有人请纠正我,我在做什么错在这里,为什么它没有将文本绑定到数据库中?

谢谢!

回答

1

我想你并没有真正执行查询。在绑定 值和最终确定之间调用sqlite3_step(sqlite3_stmt*);

(但是真的,你想用这个核心数据!)

+0

嗨,谢谢。我试着添加sqlite3_step(compiledStatement);绑定sqlite3_bind_text和sqlite3_finalize之间,但没有成功。它不会将数据存储到数据库中。 – Getsy 2011-03-28 03:10:26

+0

嗨,我想出了另一个问题,即,我从表中删除了“”id“VARCHAR”,现在相同的代码可以存储数据。 – Getsy 2011-03-28 07:36:19