我正在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);
我总是得到"查询失败没有这样的表存在:评级"在我的日志中。 任何帮助将不胜感激。
答案 0 :(得分:0)
基本上由于数据库中的表不可用或查询错误而发生此错误。根据您的帖子,IT在查询中看起来像是一个错误。
查询应该是这样的:
NSString *q = @"select * from Ratings where perfume_id in ('12','13','14','15')";
打印您的"String q"
,并确保按照上述步骤进行操作。
答案 1 :(得分:0)
感谢大家的帮助。在该路径上已存在相同的数据库文件。所以我的第一个方法是创建一个数据库 if([filemgr fileExistsAtPath:databasePath] == NO)check is false且未创建表。
答案 2 :(得分:0)
首先从文档目录中删除数据库文件,然后使用以下代码创建一个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");
}
}