【问题标题】:sqlite3_prepare_v2() == SQLITE_OK returns NO and existing table is not foundsqlite3_prepare_v2() == SQLITE_OK 返回 NO 并且未找到现有表
【发布时间】:2013-05-10 04:18:50
【问题描述】:

我正在开发一个使用 sqlite3 数据库文件的 iphone 应用程序。我正在使用以下代码从 sqlite3 数据库中进行选择;

NSString* dbYolu = [NSString stringWithFormat:@"%@/emhdb3.sqlite3",NSHomeDirectory()];

sqlite3* db;

NSLog(@"%@ dbyolu : ", dbYolu);

if (sqlite3_open([dbYolu UTF8String], &db) == SQLITE_OK)
{
    NSString *query = [NSString stringWithFormat: @"SELECT username, mobile FROM peopleto WHERE namesurname=\"%@\"", name.text];
    NSLog(@"%@ : query", query);
    sqlite3_stmt *stmt;

    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, nil) == SQLITE_OK) //  statement stmt returns null and returns SQLITE_OK = FALSE.
    {
        NSLog(@"stepped in SQLITE_OK is TRUE");
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            NSString* ders_kodu = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)];
            double not = sqlite3_column_double(stmt, 1);

            NSLog(@"%@ : %f", ders_kodu, not);
        }
    }
    else
    {
        NSLog(@"%@ - but failed", stmt);
    }
    sqlite3_finalize(stmt);
}
else
    NSLog(@"db could not be opened");

它在上面的“sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, nil) == SQLITE_OK" 点失败。错误是:“错误:没有这样的表:peopleto。但是当我在 sql 浏览器中运行此查询时,它成功返回了表的内容。我的意思是这个查询是正确的并且可以工作。

顺便说一句,我在 viewLoad 上获得了带有以下代码的路径名文件

- (void)viewDidLoad
{
NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"emhdb3.sqlite3"]];

NSLog(@"dbpath : %@", databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
    const char *dbpath = [databasePath UTF8String];
    NSLog(@"db not found");
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"SQLITE_OK is passed");
        //.... some codes here, doesn't metter for me because SQLITE_OK = true
    } else {
        status.text = @"Failed to open/create database";
    }
}
else if ([filemgr fileExistsAtPath:databasePath] == YES)
{
    NSLog(@"i found the file here : %s",[databasePath UTF8String]);
}
NSLog(@"completed");
[super viewDidLoad];
}

我的项目文件夹中有数据库文件并添加到项目中。 我错过了什么?

谢谢

///////

我很抱歉更新我的问题,但 stackoverflow 不允许我添加新的,

请在下面找到我的更新;

这是代码的更新版本:

查看加载部分;

- (void)viewDidLoad
{
[self createEditableCopyOfDatabaseIfNeeded];

NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"emhdb3.sqlite3"]];

NSLog(@"dbpath : %@", databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS peopleto (pId INTEGER PRIMARY KEY AUTOINCREMENT, namesurname TEXT, mobile TEXT)";
        if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
            status.text = @"Failed to create table";
        }
        sqlite3_close(contactDB);
    } else {
        status.text = @"Failed to open/create database";
    }
}
else if ([filemgr fileExistsAtPath:databasePath] == YES)
{
    NSLog(@"%s is filepath",[databasePath UTF8String]);
}
NSLog(@"checkpoint 4");
[super viewDidLoad];
}

buraya başka birşey yaz

- (void)createEditableCopyOfDatabaseIfNeeded {
NSLog(@"entered createEditableCopyOfDatabaseIfNeeded");
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"emhdb3.sqlite3"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
    NSLog(@"success == YES returned");
    return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"emhdb3.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
    NSLog( @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
else{
    NSLog(@"checkpoint 2");
}
}

以及执行选择查询的部分;

-(IBAction)findcontact2:(id)sender
{
NSString *dbYolu = databasePath;
sqlite3* db;

NSLog(@"%@ dbyolu : ", dbYolu);

if (sqlite3_open([dbYolu UTF8String], &db) == SQLITE_OK)
{
    NSString *query = [NSString stringWithFormat: @"SELECT namesurname, mobile FROM peopleto"]; //any query

    NSLog(@"%@ : query", query);

    sqlite3_stmt *stmt;

    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, nil) == SQLITE_OK)
    {
        NSLog(@"SQLITE is OK");
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            NSString* ders_kodu = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)];
            double not = sqlite3_column_double(stmt, 1);

            NSLog(@"%@ : %f", namesurname, mobile);
        }
    }
    else
    {
        NSLog(@"Error=%s",sqlite3_errmsg(db));   // error message : Error=no such table: peopleto
        NSLog(@"%@ - is stmt", stmt); //stmt is null
    }
    sqlite3_finalize(stmt);
}
else
    NSLog(@"could not open the db");
}

正如我上面指出的,错误是 Error=no such table: peopleto and stmt is returned

【问题讨论】:

    标签: iphone sqlite


    【解决方案1】:

    您是否正在将您的 SQL 文件复制到文档中,如果没有,您需要先这样做。将 SQL 文件添加到项目时,它会添加到您的包中,而不是文档目录中。你需要先复制那个

    - (void)createEditableCopyOfDatabaseIfNeeded {
        // First, test for existence.
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
        success = [fileManager fileExistsAtPath:writableDBPath];
        if (success)
            return;
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {
            NSLog( @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }
    

    【讨论】:

    • 非常感谢您的快速回复crazycreator,我想问一些问题以澄清更多了解; 1 - 数据库文件应该在哪里? (物理上) 2 - 上面的代码在哪里寻找文件? 3 - 该代码是将文件复制到缓存还是物理上的另一个位置以让应用程序写入/读取?我问这些问题是因为 fileexists 命令可以找到我的数据库文件(fileexists 过程通过)并且错误是“没有这样的人的表”。我知道应用程序找到了该文件,但表格找不到。
    • @MuratCalim 我很乐意回答你的问题,请拍
    • 我应用了你的代码,它通过了 if(success) 返回点。这不是说“数据库文件存在于路径可写数据库路径中”吗?但在那之后,我的应用程序返回相同的错误“没有这样的表 peopleto”。但我可以查看和查询 peopleto table !?
    • 你改过文件名了吗?
    • 顺便说一句,我打印了 stmt 并看到它是空的。
    【解决方案2】:

    请检查主包中的数据库是否包含该表,然后尝试将其复制到资源文件夹并应用上述内容

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 2013-08-17
      • 2011-12-18
      • 2014-04-15
      • 1970-01-01
      • 2016-05-25
      • 2019-09-20
      • 2023-03-19
      • 2016-07-25
      相关资源
      最近更新 更多