Iphone sqlite3_prepare_v2()==SQLITE_OK返回否,并且未找到现有表

Iphone sqlite3_prepare_v2()==SQLITE_OK返回否,并且未找到现有表,iphone,sqlite,Iphone,Sqlite,我正在开发一个使用sqlite3数据库文件的iphone应用程序。我使用以下代码从sqlite3数据库中进行选择 NSString* dbYolu = [NSString stringWithFormat:@"%@/emhdb3.sqlite3",NSHomeDirectory()]; sqlite3* db; NSLog(@"%@ dbyolu : ", dbYolu); if (sqlite3_open([dbYolu UTF8String], &db) == SQLITE_O

我正在开发一个使用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\u prepare\u v2(db,[query UTF8String],-1,&stmt,nil)=SQLITE\u 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这样的表:peopleto和stmt返回null

您是否要将SQL文件复制到文档中,如果不是,您需要先这样做。将SQL文件添加到project时,它会添加到包中,而不是文档目录中。您需要先复制该文件

- (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过程通过),错误是“没有这样的表”“。我知道应用程序找到了该文件,但无法找到该表。@Muratcalam我很乐意回答您的问题,请回答我应用了您的代码,它通过了if(success)返回点。这不是意味着“数据库文件存在于路径可写数据库路径”吗?但在这几行之后,我的应用程序返回相同的错误“no-this table peopleto”。但我可以查看和查询peopleto表!?你改变文件名了吗?顺便说一下,我打印了stmt,看到它是空的。