Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Sqlite3数据库未执行查询_Ios_Sqlite - Fatal编程技术网

Ios Sqlite3数据库未执行查询

Ios Sqlite3数据库未执行查询,ios,sqlite,Ios,Sqlite,我正在用Xcode开发iPhone应用程序,无法访问我创建的数据库。下面是我创建数据库的代码 //create local database NSString *docsDir; NSArray *dirPaths; sqlite3 *localDB; // Get the documents directory dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

我正在用Xcode开发iPhone应用程序,无法访问我创建的数据库。下面是我创建数据库的代码

//create local database
NSString *docsDir;
NSArray *dirPaths;
sqlite3 *localDB;

// 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: @"local.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO) {
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &localDB) == SQLITE_OK) {
        char *errMsg;
        const char *sql_stmt1 = "CREATE TABLE IF NOT EXISTS My_choices (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, na TEXT, got_it TEXT)";

        const char *sql_stmt2 = "CREATE TABLE IF NOT EXISTS Ratings (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, rating NUMERIC)";

        const char *sql_stmt3 = "CREATE TABLE IF NOT EXISTS Consultant_Details (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,website TEXT,domain TEXT,domain_no NUMERIC,phone TEXT,facebook TEXT)";

        if (sqlite3_exec(localDB, sql_stmt1, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"%@",@"Failed to create table My_choices");

        }
        if (sqlite3_exec(localDB, sql_stmt2, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"%@",@"Failed to create table ratings");

        }
        if (sqlite3_exec(localDB, sql_stmt3, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"%@",@"Failed to create table Consultant_Details");

        }


        sqlite3_close(localDB);
    }
    else {
        NSLog(@"%@",@"Failed to open/create database");

    }
}
下面是我试图访问它的代码

NSString *docsDir;
NSArray *dirPaths;
sqlite3 *localDB;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
NSString *dbPath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"local.db"]];


NSString *q = @"select * from Ratings where perfume_id in";

q = [q stringByAppendingString:idList];

NSLog(@"%@",q);
const char *sqlStatement2 = [q UTF8String];
sqlite3_stmt *compiledStatement2;
if(sqlite3_open([dbPath UTF8String], &localDB) == SQLITE_OK) {
    if(sqlite3_prepare_v2(localDB, sqlStatement2, -1, &compiledStatement2, NULL) == SQLITE_OK) {

        NSLog(@"%d",  SQLITE_ROW);
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement2) == SQLITE_ROW) {


            // Read the data from the result row
            int r = sqlite3_column_int(compiledStatement2, 1);
            int id = sqlite3_column_int(compiledStatement2, 0);
            NSLog(@"%d", id);

            for (ListItem *item in self.listItemArray) {

                if(item.perfumeId == id){
                    item.rating = r;

                }
            }

        }
    }
    else{
        NSLog(@"query failed: %s", sqlite3_errmsg(localDB));
    }
}
sqlite3_close(localDB);
我总是在日志中看到“查询失败,不存在这样的表:评级”。
感谢您的帮助。

基本上,此错误是由于数据库中的表不可用或查询错误造成的。根据你的帖子,这看起来像是查询中的一个错误

查询应该是这样的:

NSString *q = @"select * from Ratings where perfume_id in ('12','13','14','15')";

打印您的
“String q”
,并确保其符合上述要求。

感谢所有人的帮助。该路径中已存在具有相同内容的数据库文件。所以我的第一个方法是创建一个数据库
如果([filemgr fileExistsAtPath:databasePath]==NO)检查为false且未创建表。

首先从文档目录中删除数据库文件,然后使用以下代码创建sqlite表

 if (![fileManager fileExistsAtPath: _databasePath ])
        {
            const char *dbpath = [_databasePath UTF8String];
            if (sqlite3_open(dbpath, &_myDataBase) == SQLITE_OK)
            {
                char *errMsg;
                const char *sql_stmt =
                "CREATE TABLE IF NOT EXISTS My_choices (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, na TEXT, got_it TEXT);" //TEST
                "CREATE TABLE IF NOT EXISTS Ratings (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, rating NUMERIC);" //USER
                "CREATE TABLE IF NOT EXISTS Consultant_Details (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,website TEXT,domain TEXT,domain_no NUMERIC,phone TEXT,facebook TEXT)"; //METADATA


                if (sqlite3_exec(_myDataBase, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
                {
                    NSLog(@"Failed to create tables");
                }
                sqlite3_close(_myDataBase);
                NSLog(@"DataBase created at: %@",_databasePath);
            } else {
                NSLog(@"Failed to open/create database");
            }
        }

查询中表名后面的“in”是什么…?如果([filemgr fileExistsAtPath:databasePath]==否),请在此处放置断点,并检查此条件是否为真。好的,我已放置此断点,并且文件存在…因此这意味着未创建表!!但我找不到原因?请检查我编辑的问题。我得到一个错误“不存在这样的表:评级”。我想我的表没有正确创建。你能在我创建数据库的代码中找到任何问题吗?感谢您尝试创建表的第一个方法中的Post错误日志。解决你的问题可能会有帮助。这有什么帮助?你没能在这里找到真正的问题(OP和他的“答案”也是如此)。问题是创建过程中的任何失败都会留下一个半创建的数据库文件,无法使用。它应该在出现故障时被删除,我认为应用程序应该崩溃。它没有什么问题,但它与OPs代码没有什么不同。