尝试打开(创建)SQLite d/b 时出错(“EXC_BAD_ACCESS”)

发布于 2024-11-03 15:10:06 字数 1381 浏览 0 评论 0原文

这是代码...有人看到出了什么问题吗?另外,为什么“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-11-10 15:10:06

它崩溃是因为 errmsg 不是 Objective-C 对象,而您通过使用 %@ 替换需要它。 errmsg 是一个 char *,这意味着您应该使用 %s

至于为什么崩溃......

sqlite3_open 定义为:

int sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);

您的db 被声明为sqlite3*。换句话说,您传递了错误的内容。您应该这样做:

sqlite3_open(cDatabasePath, &db)

虽然您非常渴望了解 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 a char *, which means you should be using %s.

As for why it's crashing....

sqlite3_open is defined as:

int sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);

Your db is declared as sqlite3*. In other words, you're passing the wrong thing in. You should be doing:

sqlite3_open(cDatabasePath, &db)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文