尝试打开(创建)SQLite d/b 时出错(“EXC_BAD_ACCESS”)
这是代码...有人看到出了什么问题吗?另外,为什么“errmsg”的第二个 NSLog 会导致调试器在调试设备(iPhone 3GS)时崩溃
// 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);
}
Here's the code... anybody see what's wrong? Also, why does the 2nd NSLog of "errmsg" cause the debugger to crash when debugging to the device (iPhone 3GS)
// 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它崩溃是因为
errmsg
不是 Objective-C 对象,而您通过使用%@
替换需要它。errmsg
是一个char *
,这意味着您应该使用%s
。至于为什么崩溃......
sqlite3_open
定义为:您的
db
被声明为sqlite3*
。换句话说,您传递了错误的内容。您应该这样做:虽然您非常渴望了解 SQLite C API,但我仍然认为您应该使用 FMDB。它确实减少了此类错误,让您能够专注于代码的真正问题。
It's crashing because
errmsg
is not an Objective-C object, which you're requiring by your use of the%@
substitution.errmsg
is achar *
, which means you should be using%s
.As for why it's crashing....
sqlite3_open
is defined as:Your
db
is declared assqlite3*
. In other words, you're passing the wrong thing in. You should be doing:While your desire to understand the SQLite C API is great, I still think you should use FMDB. It really mitigates these sorts of errors and lets you concentrate on the real problems with your code.