【问题标题】:App crashes on sqlite after updating the app更新应用程序后,应用程序在 sqlite 上崩溃
【发布时间】:2012-12-03 19:50:38
【问题描述】:

更新应用后,sqlite数据库出现问题。应用程序崩溃。这是我的日志,但我不确定有什么问题:

Stack: (
"0   MyApp                       0x00085bdb MyApp + 543707",
"1   MyApp                       0x0008633d MyApp + 545597",
"2   libsystem_c.dylib                   0x3af73e8b _sigtramp + 34",
"3   libsqlite3.dylib                    0x3792c447 sqlite3_log + 4986",
"4   libsqlite3.dylib                    0x3792c447 sqlite3_log + 4986",
"5   libsqlite3.dylib                    0x3791d1cb sqlite3_exec + 11954",
"6   libsqlite3.dylib                    0x3791b989 sqlite3_exec + 5744",
"7   libsqlite3.dylib                    0x3791b119 sqlite3_exec + 3584",
"8   libsqlite3.dylib                    0x3791ac17 sqlite3_exec + 2302",
"9   libsqlite3.dylib                    0x3791a977 sqlite3_exec + 1630",
"10  libsqlite3.dylib                    0x3795195f sqlite3_prepare_v2 + 30",
"11  MyApp                       0x00091ca7 MyApp + 593063",
"12  MyApp                       0x00094211 MyApp + 602641",
"13  MyApp                       0x000c09d9 MyApp + 784857",
"14  UIKit                               0x3a044545 <redacted> + 412",
"15  UIKit                               0x3a02930b <redacted> + 1310",
"16  UIKit                               0x3a0407c7 <redacted> + 206",
"17  UIKit                               0x39ffc803 <redacted> + 258",
"18  QuartzCore                          0x3bcd0d63 <redacted> + 214",
"19  QuartzCore                          0x3bcd0901 <redacted> + 460",
"20  QuartzCore                          0x3bcd1835 <redacted> + 16",
"21  QuartzCore                          0x3bcd121b <redacted> + 238",
"22  QuartzCore                          0x3bcd1029 <redacted> + 316",
"23  UIKit                               0x3a1cc1e1 <redacted> + 112",
"24  UIKit                               0x3a07c627 <redacted> + 34",
"25  CoreFoundation                      0x342be6cd <redacted> + 20",
"26  CoreFoundation                      0x342bc9c1 <redacted> + 276",
"27  CoreFoundation                      0x342bcc91 <redacted> + 608",
"28  CoreFoundation                      0x3422febd CFRunLoopRunSpecific + 356",
"29  CoreFoundation                      0x3422fd49 CFRunLoopRunInMode + 104",
"30  GraphicsServices                    0x377742eb GSEventRunModal + 74",
"31  UIKit                               0x3a04d2f9 UIApplicationMain + 1120",
"32  MyApp                       0x000036e3 MyApp + 9955",
"33  MyApp                       0x0000369c MyApp + 9884"
)

第 11 行显示我的函数中的函数 sqlite3_prepare_v2 存在问题:

- (BOOL) checkIfTableExist:(NSString *) tableName
{
BOOL exist = NO;
sqlite3_stmt *stmt;

NSString *sql = [[NSString alloc] initWithFormat:@"SELECT name FROM sqlite_master WHERE type = 'table' AND name = '%@'", tableName];

if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL) == SQLITE_OK) { 
    if (sqlite3_step(stmt) == SQLITE_ROW)
        exist = YES;

    sqlite3_finalize(stmt);
}

[sql release];

return exist;
}

可能是更新应用程序后数据库文件损坏或其他原因?

【问题讨论】:

  • 您是否在更新应用后检查过所需的 sqlite 文件是否存在。您可以以编程方式或通过 firefox 插件 Sqlite 进行检查。可能,您重建了您的数据库,这就是您的查询无法执行的原因,因为没有数据库。从您的功能来看,您似乎要检查数据库是否存在......所以请谷歌搜索,因为这不是正确的方法。
  • 在我的类的 init 函数中,我检查 sqlite 数据库文件是否存在。在上次更新中,我没有更改数据库的结构或任何内容。也许文件已损坏?但为什么呢?

标签: ios database sqlite crash


【解决方案1】:

问题是您没有打开数据库。您的代码中没有 sqlite3_open 调用。

所以数据库变量将包含一个垃圾值。这就是它崩溃的原因。

- (BOOL) checkIfTableExist:(NSString *) tableName
{
  BOOL exist = NO;
  if (sqlite3_open([yourDatabasePath UTF8String], &dataBase) == SQLITE_OK)
  {
    NSString *sql = [NSString stringWithFormat:@"SELECT name FROM sqlite_master WHERE type = 'table' AND name = '%@'", tableName];
    const char *query = [sql UTF8String];
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, query, -1, &stmt, NULL) == SQLITE_OK)
    { 
       if (sqlite3_step(stmt) == SQLITE_ROW)
           exist = YES;

       sqlite3_finalize(stmt);
    }
  }
 return exist;
}

【讨论】:

  • 我在班级的init函数中打开数据库。我忘了提。
  • @bialek:请删除[sql release]并检查
  • 当我测试应用程序时,一切正常。只有部分用户更新后,应用会崩溃。
猜你喜欢
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
相关资源
最近更新 更多