下面是代碼...任何人看到有什麼問題?另外,爲什麼的「ERRMSG」第二個的NSLog導致調試調試到設備時崩潰(iPhone 3GS)嘗試打開(創建)SQLite時出錯(「EXC_BAD_ACCESS」)d/b
// Get the path to the database file
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
NSString *databasePath = [documentPath stringByAppendingPathComponent:@"ppcipher.s3db"];
const char *cDatabasePath = [databasePath cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"databasePath: %@", databasePath);
NSString *sqlCommand = @"CREATE TABLE CardData (card_id TEXT PRIMARY KEY NOT NULL, card_name TEXT NOT NULL, "
@"card_type TEXT, cide_val TEXT, create_date TEXT DEFAULT CURRENT_DATE, user_notes TEXT, gps_loc TEXT)";
const char cSQLCommand = [sqlCommand cStringUsingEncoding:NSUTF8StringEncoding];
char * errmsg = NULL;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databasePath error:NULL]; // <------------ delete d/b TESTING ONLY!
BOOL fileExists = [fileManager fileExistsAtPath:databasePath];
if(!fileExists) {
if(sqlite3_open(cDatabasePath, db) == SQLITE_OK) { // doesn't exist, so create it...
sqlite3_exec(db, &cSQLCommand, NULL, NULL, &errmsg); // now create the table...
NSLog(@"error: %@", errmsg);
}
只有受虐者直接在Objective-C中使用SQLite C API。 [使用FMDB](http://github.com/ccgus/fmdb)(一個SQLite包裝器)或[CoreData](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/ cdProgrammingGuide.html)(一個對象圖管理器)。 – 2011-04-26 20:36:47
我尊重你的意見......但是,我選擇直接使用API,主要是學習......你能告訴我爲什麼我得到sqlite3_open錯誤嗎? – SpokaneDude 2011-04-26 20:49:37
編輯我的答案。 – 2011-04-26 21:15:51