2013-03-14 58 views
0

我是新來使用Mac 10.7.3 iphone programming.Iam在我不能夠通過textfield.Can存儲在數據庫中,我存儲在數據庫裏面的數據資料的任何身體告訴我爲什麼它的database.What裏面不添加的數據是錯誤在此代碼無法在數據庫中的數據存儲在iPhone

NSString *docsDir; 
    NSArray *dirPaths; 
// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
// Build the path to the database file 
    databasePath = [docsDir stringByAppendingPathComponent: @"test.db"]; 
    NSLog(@"%@",databasePath); 
    NSFileManager *fn=[NSFileManager defaultManager]; 
    NSError *error; 
    BOOL success=[fn fileExistsAtPath:databasePath]; 

    if(!success) { 

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.db"]; 
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error]; 
    } 
    //NSLog(@"sdasdassd%@",databasePath); 
    NSLog(@"hai"); 
    sqlite3_stmt *statement; 

    const char *dbpath = [databasePath UTF8String]; 
      if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
     { 

      NSLog(@"hai2"); 
NSString *insertSQL = [NSString stringWithFormat: @"insert into path(imagepath,audiopath) values (\"%@\",\"%@\")",name.text,email.text]; 

      const char *insert_stmt = [insertSQL UTF8String]; 

      sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); 
      if (sqlite3_step(statement) == SQLITE_DONE) 
      { 
        NSLog(@"details added");  

      } else { 
        NSLog(@"details not added");  
      } 
      sqlite3_finalize(statement); 
      sqlite3_close(contactDB); 

     } 
    } 

感謝 阿斯拉姆

+0

你在庫中檢查 - >模擬器 - >應用程序 - >應用程序 - >文檔 - > sqlite數據庫嗎?檢查是否創建表和字段。? – 2013-03-14 09:57:24

+0

很可能你沒有報告任何錯誤。 – 2013-03-14 11:23:34

+0

錯誤或不coming.In控制檯其顯示該信息不是給重播添加 – Mohammed 2013-03-14 11:33:04

回答

0

首先,當應用負載,然後複製到數據庫應用程序文件夾

應用程序代理文件的代碼::

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

     NSString *docsDir; 
     NSArray *dirPaths; 

     // Get the documents directory 
     dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     docsDir = [dirPaths objectAtIndex:0]; 

     //Build the path to the database file 
     databasePath = [docsDir stringByAppendingPathComponent: @"test.db"]; 
     NSLog(@"%@",databasePath); 

     NSFileManager *fn=[NSFileManager defaultManager]; 
     NSError *error; 

     BOOL success=[fn fileExistsAtPath:databasePath]; 

     if(!success) { 

      NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.db"]; 
      success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error]; 
     } 
} 

然後,在保存按鈕點擊,
這裏,[app dbpath]是從NSString的委託文件訪問。
代碼::

sqlite3_stmt *statement; 

const char *dbpath = [databasePath UTF8String]; 

if (sqlite3_open([[app dbpath] UTF8String], &contactDB) == SQLITE_OK) { 
{ 
    NSString *insertSQL = [NSString stringWithFormat: @"insert into path(imagepath,audiopath) values (\"%@\",\"%@\")",name.text,email.text]; 

    const char *insert_stmt = [insertSQL UTF8String]; 

    if (sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK) { 

     NSLog(@"details added"); 

    } 

    else { 
     NSLog(@"details not added"); 
    } 

    sqlite3_finalize(statement); 
    sqlite3_close(contactDB); 
} 

它可能會幫助到你。
謝謝。

+0

感謝。[應用DBPATH]這裏它呈現出一些錯誤 – Mohammed 2013-03-14 11:15:42

+0

u能告訴我,爲什麼它的數據庫 – Mohammed 2013-03-14 11:16:26

+0

內不存儲數據做ü檢查其上創建數據庫在模擬器文件夾中。與您在應用程序中創建的相同嗎? – 2013-03-14 11:20:14