gpt4 book ai didi

iphone - 从 iPhone 5 中的 SQLite 删除数据时应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:08 24 4
gpt4 key购买 nike

当应用程序"didFinishLoading" 数据库被清除并调用网络服务来获取数据并将其插入数据库时​​,我开发了一个 SQLite 数据库。它在所有 iPhone 和 iPad 设备上运行良好,但在 iPhone5 上,当从数据库中删除数据时它会崩溃。

这里,tableNamesArray 表示 SQLite 中的表。

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
for (int i = 0; i < [tableNamesArray count]; i++)
{
NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM %@", [tableNamesArray objectAtIndex:i]];

NSLog(@"querySQL %@", querySQL);

// SELECT QuationID,Quation FROM QuationsTable WHERE GroupID="Group0"

const char *query_stmt = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &compiledStatement, NULL) == SQLITE_OK)
{
}

if (sqlite3_step(compiledStatement) != SQLITE_DONE )
{
}
else
{
}

sqlite3_finalize(compiledStatement);
}

sqlite3_close(contactDB);
}

请帮帮我。

最佳答案

使用此函数将您的数据库复制到缓存目录中。

-(void)createDBcopy:(NSString*)fileName
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;

// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *DBPath=[documentsDirectory stringByAppendingPathComponent:fileName];
//DBPath = [DBPath stringByAppendingString:@".sqlite"];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:DBPath];

// If the database already exists then return without doing anything
if(success) return;

// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];

[fileManager release];

}

并将其用作:

[yourClassObjectWhereTheMethodIsWritten createDBcopy:@"yourDB.sqlite"];

您应该在 Appdelegate 的 didFinishLaunchWithOption 中使用它。

关于iphone - 从 iPhone 5 中的 SQLite 删除数据时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com