2013-05-08 65 views
-2
NSString *docsDir; 
NSArray *dirPaths; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

docsDir = [dirPaths objectAtIndex:0]; 

databasePath = [[NSBundle mainBundle] pathForResource:@"etkYeni2" ofType:@"sqlite"];  

const char *dbpath = [databasePath UTF8String]; 
sqlite3_stmt *statement;  


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

    NSLog(@"icerdeyim"); 

    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO etk (etkTip) VALUES ('%@')",yeniEkleLabel.text]; 

    const char *query_stmt = [insertSQL UTF8String]; 
    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL); 

    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     NSLog(@"OK INSERTED"); 

    } else { 
     NSLog(@"NOT INSERTED !!"); 

    } 
    sqlite3_finalize(statement); 
    sqlite3_close(contactDB); 
} 

這是我從文本框中獲取一個字符串並將其插入到sqlite數據庫的代碼的一部分。我從firefox擴展sqlite管理器創建了數據庫,並將其添加到應用程序中。它正如我在模擬器和iPod touch中所預期的那樣運行(我插入元素並在使用SELECT查詢時可以看到它們),但它不會在iphone上插入元素。我的iPod touch是在iOS 5上,而iPhone是在iOS 6上。是因爲iOS或iPhone的版本,而iPod touch是以不同的方式來裝飾sqlite還是什麼?我閱讀了這個問題的大部分答案,但仍然沒有得到解決方案。在這種情況下有人能幫助我嗎? 謝謝..Sqlite INSERT INTO不在iphone中進行,但在模擬器和iPod touch?

+0

http://stackoverflow.com/questions/9261601/sqlite- insert-works-in-simulator-but-not-on-device和http://stackoverflow.com/questions/717108/where-would-you-place-your-sqlite-database-file-in-an-iphone-應用程序 – Buntylm 2013-05-08 10:14:51

+0

應用程序包是隻讀的,您需要將數據庫複製到文檔目錄並使用它。 – 2013-05-08 10:23:58

+0

@efdalustaoglu你實施了我的答案。 – 2013-05-08 10:32:15

回答

2

您的數據庫不復制在電話目錄你需要添加該數據庫。 使用此代碼。

-(void)createdatabase 
{ 

    NSString *databaseName = @"Card.sqlite";  // Database Name 



    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDir = documentPaths[0]; 

    NSLog(@"Dir : %@ ",documentsDir); 

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 


    BOOL success; 

     NSFileManager *fileManager = [NSFileManager defaultManager] ; 

     success = [fileManager fileExistsAtPath:databasePath]; 


    if(success) 
     return; 


    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 


    NSLog(@"%@",databasePathFromApp); 


    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 

} 

將你的sqlite文件添加到mainbundle中。 這真的很有幫助。

+0

非常感謝你 – efdalustaoglu 2013-05-08 11:14:06

0

請嘗試使用這一個。如果你想在這種情況下插入數據在sqlite中,你必須創建你的數據庫和插入查詢使用這種方法。我希望它會有所幫助..

- (void)executeQuery:(NSString *)insertQuery 
{ 
    sqlite3_stmt *statement; 
    if (sqlite3_open([[self databaseFilePath] UTF8String],&sqliteDB) == SQLITE_OK) 
    { 
     if(sqlite3_prepare_v2(sqliteDB,[insertQuery UTF8String], -1, &statement, NULL) == SQLITE_OK) 
     { 
      if(sqlite3_step(statement) != SQLITE_DONE) 
      { 
       sqlite3_finalize(statement); 
      } 
     } 
     else 
      NSLog(@"query Statement Not Compiled"); 

     sqlite3_finalize(statement); 
     sqlite3_close(sqliteDB); 
    } 
    else 
     NSLog(@"Database Not Opened"); 
} 



- (NSString *)databaseFilePath 
{ 
    [email protected]"abc.sqlite"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *path = [paths objectAtIndex:0]; 
    return [path stringByAppendingPathComponent:databaseName]; 
} 
相關問題