gpt4 book ai didi

ios - 从iPhone设备读取和写入sqlite数据库中的数据时出错

转载 作者:行者123 更新时间:2023-11-29 04:50:59 37 4
gpt4 key购买 nike

我创建了一个应用程序,将数据存储到数据库中并读取它。我创建了一个 .sql 文件并将其添加到资源文件夹中。我可以通过模拟器保存和检索数据,我的应用程序在模拟器上运行良好,但是当我在设备上安装相同的应用程序时,我收到错误“没有这样的表”

任何人都可以提供此问题的解决方案吗?

这是我正在使用的代码块:

///////AppDelegate.m 内部>>开始>>>

/////检查数据库是否存在>>>>

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];
BOOL success = [fileManager fileExistsAtPath:dbPath];

if(!success) {

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];


if (!success)

NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);

} else {

NSLog(@"Database file found at path %@", dbPath);

}

///////AppDelegate.m 内部 <<

///////在数据库中写入文件的逻辑>>>开始>>

           sqlite3 *pDb; 
char *databaseName;

databaseName = @"TempDatabase.sql";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
//databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

databasePath =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];

//[self copyDatabaseIfNeeded];


if(sqlite3_open([databasePath UTF8String], &pDb) == SQLITE_OK)
{

const char *sqlStatement = "INSERT INTO tablename (name) VALUES(?)";

sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(pDb, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{


sqlite3_bind_text( compiledStatement, 1, [strTxtFldValue UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(compiledStatement) != SQLITE_DONE )
{
NSLog( @"~~~~~~~~~~~ 1 Error: %s", sqlite3_errmsg(pDb) );
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Name name is not added." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

} else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"name is added successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}
sqlite3_finalize(compiledStatement);
}

sqlite3_close(pDb);

///////将文件写入数据库的逻辑 <<

最佳答案

您是否尝试过在运行时创建数据库。在运行时执行查询并创建表,尝试一下,这肯定会对您有所帮助。

关于ios - 从iPhone设备读取和写入sqlite数据库中的数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851079/

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