2013-07-26 99 views
0

我有需要与数据库执行的查询序列.. 大多数时间其工作正常..但有些时候它未能插入查询。SqlLite不执行查询一段时间

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 

     for (int i=0;i<[queries count]; i++) 
     { 

      NSString *query = [queries objectAtIndex:i]; 
      const char *Insert_query = [query UTF8String]; 

      sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL); 
      if (sqlite3_step(statement) == SQLITE_DONE) 
      { 
       //NSLog(@" \n\n\n\n %@ done query",query); 
      } 
      else { 

       NSLog(@" \n\n\n\n %@ not done query",query); 
      } 
     } 
     sqlite3_finalize(statement); 
     sqlite3_close(contactDB); 
    } 

以上是我已经实现执行插入操作码...

任何一个可以帮我找到,如果再失败了就无法插入到数据库中,以便我能处理的错误是什么原因..

+0

你得到的错误是什么。 –

+0

它刚刚坠毁没有理由..那就是为什么我想知道我们可以实现NSError或可以帮助我调试为什么它失败了一些时间.. –

+0

当它崩溃只需在调试器中键入bt并按回车,请请告诉我什么是urro得到 –

回答

2

使用此方法在文件目录上的SQLite

//-----------------------------------------------------------------------------------------------------// 
#pragma mark - Helper methods 
//-----------------------------------------------------------------------------------------------------// 

-(BOOL)dbOpenedSuccessfully 
{ 
    if(sqlite3_open([[self dbPath] UTF8String], &_database) == SQLITE_OK) 
    { 
     return YES; 
    } 
    else 
    { 
     [[[UIAlertView alloc]initWithTitle:@"Error" 
            message:@"Error on opening the DB" 
            delegate:self 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil, nil]show]; 
     return NO; 
    } 
} 






//-----------------------------------------------------------------------------------------------------// 
#pragma mark - Query 
//-----------------------------------------------------------------------------------------------------// 


- (void) executeQuery:(NSString *)strQuery 
{ 
    char *error = NULL; 
    if([self dbOpenedSuccessfully]) 
    { 
     NSLog(@"%@",strQuery); 
     sqlite3_exec(_database, [strQuery UTF8String], NULL, NULL,&error); 
     if (error!=nil) { 
      NSLog(@"%s",error); 
     } 
     sqlite3_close(_database); 


    } 

} 

而且执行查询如果插入不正常工作的原因可能是文件没有如果它的存在捆绑将取回塔数据,但不能更新或插入值,如果db是捆绑,将其复制到文档目录,然后尝试用它

-(void) checkAndCreateDatabase 
{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:_databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil]; 

} 

For more info see this

0

你可以试试按以下方式打印错误,根据错误您可以做出决定。

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
{ 

    for (int i=0;i<[queries count]; i++) 
    { 

     NSString *query = [queries objectAtIndex:i]; 
     const char *Insert_query = [query UTF8String]; 

     sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      //NSLog(@" \n\n\n\n %@ done query",query); 
     } 
     else { 
   NSLog(@"sqlite3_step error: '%s'", sqlite3_errcode(contactDB)); 
  NSLog(@" \n\n\n\n %@ not done query",query); 
     } 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(contactDB); 
} 

此外,

SQLITE_DONE means that the statement has finished executing successfully. sqlite3_step() should not be called again on this virtual machine without first calling sqlite3_reset() to reset the virtual machine back to its initial state.

可以使用,SQLITE_OK代替SQLITE_DONE。