gpt4 book ai didi

ios - 在iOS问题中打开SQLite文件

转载 作者:行者123 更新时间:2023-11-29 02:13:03 28 4
gpt4 key购买 nike

我试图打开一个 SQLite 文件并将其上传到我的项目,但我总是收到错误 no such table: Words 而我可以在 sql 应用程序中获得结果。

当我调试时,我发现我无法输入 if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK)

请问我的问题在哪里?

我的代码:

- (void)GetData{

NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"wordsDb.sqlite"]];

sqlite3 *database;
sqlite3_stmt *selectstmt;

if(sqlite3_open([_databasePath UTF8String], &database) == SQLITE_OK) {

NSString *sqlString = [NSString stringWithFormat:@"SELECT words FROM Words"];
NSLog(@"getCurrentUserInfo:%@",sqlString);
const char *SqlCommand = [sqlString UTF8String];

if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {

NSLog(@"success!");
while (sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSLog(@"select result:%@",userInfoStr);
}
}
else
{
NSLog(@"%s",sqlite3_errmsg(database));
}
sqlite3_finalize(selectstmt);
}
sqlite3_close (database);
}

最佳答案

试试这个

此方法将数据库形式的xcode-resource文件夹复制到文档文件夹

- (void)copyAndInitialiseDatabase{

@try {
// get the database path
documentDBPath = [NSString stringWithFormat:@"%@/Documents/%@.sqlite",NSHomeDirectory(),@"wordsDb"];

if ([[NSFileManager defaultManager] fileExistsAtPath:documentDBPath] == NO){// database is not available in document folder

NSString *resourceDBPath = [[NSBundle mainBundle] pathForResource:@"wordsDb" ofType:@"sqlite"];

if (resourceDBPath == nil){// check the database in souppotingFiles (resource) in our applicataion

NSLog(@" %s : %d : %s dabase is not found in resource folder. Please check the name of database or copy in resource folder.",__FILE__,__LINE__,__PRETTY_FUNCTION__);
return;
}// data base available in resource folder then copy to resource folder to document folder
[[NSFileManager defaultManager] copyItemAtPath:resourceDBPath toPath:documentDBPath error:nil];
}
// database is copied, open the database
if (sqlite3_open_v2([documentDBPath UTF8String], &_database, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK){// database is open success

NSLog(@"database is opend successfully");

}else{// database is open fail

NSLog(@"fail to open");

sqlite3_close(_database);

NSLog(@" %s: %d: %s fail to open the database. error == %s",__FILE__,__LINE__,__PRETTY_FUNCTION__,sqlite3_errmsg(_database));
}
}
@catch (NSException *exception) {
}
@finally {
}
}

-(IBAction)getThedata:(UIButton *)sender {
[self copyAndInitialiseDatabase];
sqlite3 *database;
sqlite3_stmt *selectstmt;

if(sqlite3_open([documentDBPath UTF8String], &database) == SQLITE_OK) {

NSString *sqlString = [NSString stringWithFormat:@"SELECT words FROM Words"];
NSLog(@"getCurrentUserInfo:%@",sqlString);
const char *SqlCommand = [sqlString UTF8String];

if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {

NSLog(@"success!");
while (sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSLog(@"select result:%@",userInfoStr);
}
}
else
{
NSLog(@"%s",sqlite3_errmsg(database));
}
sqlite3_finalize(selectstmt);
}
sqlite3_close (database);
}

关于ios - 在iOS问题中打开SQLite文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29071006/

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