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?
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
應用程序包是隻讀的,您需要將數據庫複製到文檔目錄並使用它。 – 2013-05-08 10:23:58
@efdalustaoglu你實施了我的答案。 – 2013-05-08 10:32:15