2014-07-24 125 views
0

我正在Xcode中开发iPhone应用程序,我无法访问我创建的数据库。以下是我在哪里创建我的数据库Sqlite3数据库未执行查询

//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

如果([filemgr fileExistsAtPath:databasePath] == NO)在这里放置断点,并检查这个条件是否为真。 – karthikeyan

+0

好吧我已经把这个休息和文件存在...所以这意味着表没有创建!但我找不到为什么? – user1554693

回答

0

基本上这个错误是由于数据库中的Table无法使用或错误的查询造成的。根据你的帖子,IT看起来像查询中的错误。

查询应该是这样的:

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

打印您"String q",并确保它上面的跟随。

+0

请检查我编辑的问题。我收到一个错误“没有这样的表格存在:评级”。我想我的表格没有正确创建。你可以在创建数据库的代码中发现任何问题吗?谢谢 – user1554693

+0

请从您尝试创建表的第一个方法发布错误日志。解决您的问题可能会有帮助。 – Surjeet

0

感谢大家的帮助。同样的数据库文件已存在于该路径。因此,我的第一个方法是创建数据库 if([filemgr fileExistsAtPath:databasePath] == NO)检查是否为false,并且未创建表。从文件目录

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"); 
      } 
     } 
+0

这有什么用?你在这里没有得到真正的问题(OP也有他的“答案”)。问题是创建过程的任何失败都会留下无法使用的半创建的数据库文件。它应该在失败时被删除,应用程序应该崩溃,我估计。 – trojanfoe

+0

@trojanfoe是这个错误的答案?如果它告诉我,我会删除它.. – karthikeyan

+0

它没什么大错,但它与OPs代码没有什么不同。 – trojanfoe