2014-06-30 341 views
1

我插入数据到sqlite数据库,我已经看到我的控制台结果NSLog(@“完成”),但我没有看到我的sqlite数据库文件中的数据更新。请帮助我 !!!插入数据到sqlite数据库

这里我的代码来保存数据:

- (void)saveData:(NSString *)_Id 
{ 
    sqlite3_stmt *statement; 
    NSString *_databasePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"data.db"]; 

    const char *dbpath = [_databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &db) == SQLITE_OK) 
    { 

     NSString *insertSQL = [NSString stringWithFormat: 
           @"INSERT INTO MyTable (id) VALUES (\"%@\")", _Id]; 

     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(db, insert_stmt, 
          -1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      NSLog(@"DONE"); 
     } else { 
      NSLog(@"Failed to add contact"); 
     } 
     sqlite3_finalize(statement); 
     sqlite3_close(db); 
    } 
} 
+0

是的,回去并确保你检查所有的错误代码。并且要明白,100个症状中有99个是由于SQL文件不在您认为的位置所致。 –

+0

(使用命令行中的sqlite3来检查您的数据库。) –

+0

@KhoaNN您看到“完成”但未看到结果的原因可能是因为该软件包是只读的。您必须将数据库复制到Documents文件夹(如果尚未存在),然后使用该数据库。 – Rob

回答

1

你在你的包打开数据库。该软件包在设备上是只读的。您必须将数据库复制到您的Documents文件夹(如果它不存在那里)并从那里打开它。

另请注意,您正在处理数据库的多个副本(项目中的一个,捆绑中的一个,现在位于设备/模拟器的Documents文件夹中)。确保您检查在正确的数据库插入的记录(一个在Documents

顺便说一句,你也应该检查,看看sqlite3_prepare_v2返回SQLITE_OK,如果没有,请登录sqlite3_errmsg

您还应该使用使用sqlite3_bind_text在SQL ?占位符和绑定值(或者,如果它是可能nilsqlite_bind_null):

- (void)saveData:(NSString *)_Id 
{ 
    sqlite3_stmt *statement; 
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath ] stringByAppendingPathComponent:@"data.db"]; 
    NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
    NSString *documentsPath = [documentsFolder stringByAppendingPathComponent:@"data.db"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath:documentsPath isDirectory:NO]) { 
     NSError *error; 
     if (![fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error]) { 
      NSLog(@"database copy failed: %@", error); 
     } 
    } 

    const char *dbpath = [documentsPath UTF8String]; 

    if (sqlite3_open(dbpath, &db) == SQLITE_OK) { 
     const char *insertSql = "INSERT INTO MyTable (id) VALUES (?)"; 
     if (sqlite3_prepare_v2(db, insertSql, -1, &statement, NULL) != SQLITE_OK) { 
      NSLog(@"prepare error: %s", sqlite3_errmsg(db)); 
     } 

     // bind a value to the ? placeholder in the SQL 

     if (_Id) { 
      if (sqlite3_bind_text(statement, 1, [_Id UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK) { 
       NSLog(@"bind text error: %s", sqlite3_errmsg(db)); 
      } 
     } else { 
      if (sqlite3_bind_null(statement, 1) != SQLITE_OK) { 
       NSLog(@"bind null error: %s", sqlite3_errmsg(db)); 
      } 
     } 

     if (sqlite3_step(statement) == SQLITE_DONE) { 
      NSLog(@"DONE"); 
     } else { 
      NSLog(@"Failed to add contact: %s", sqlite3_errmsg(db)); 
     } 

     sqlite3_finalize(statement); 
     sqlite3_close(db); 
    } 
} 

大多数人动,“如果数据库中没有在文件存在,然后从包中复制它并从那里打开“专用openDatabase方法内的逻辑,但希望这可以说明这个想法。

+0

谢谢你的回复。我这样做,但我的数据库没有更新,我仍然看到“DONE”显示:( – KhoaNN

+0

@KhoaNN你在看设备/模拟器的Documents文件夹中的数据库(而不是捆绑中的或在你的项目)? – Rob

+0

我正在查看数据库“/ Volumes/DATA/KhoaNN/Library/Application Support/iPhone Simulator/7.1/Applications/485F9F41-683F-44C7-9A63-A16FE5609853/MyApplication.app/data.db” – KhoaNN