数据库锁定错误

时间:2012-05-07 06:23:40

标签: iphone

我收到数据库锁定错误。

-(IBAction)YesData:(id)sender
 {
 [self Check_Create_DB];

sqlite3 *database;



if(idimg == 0)
{

 NSData *imgData = UIImagePNGRepresentation(image.image);  

UIImage *dataImage=[[UIImage alloc] init];

 dataImage=[UIImage imageWithData:imgData];  

    int ycount1=1;

int ncount1=0;

int neutcount1=0;  

if(sqlite3_open([dbpath UTF8String],&database)==SQLITE_OK)

{
    NSLog(@"Connection Open");  

   const char *sqlTmp="insert into 

    images(imgname,ycount,ncount,neutcount)values(?,?,?,?)";


    sqlite3_stmt *cmp_sqlstmt;

    int returnvalue=sqlite3_prepare_v2(database,sqlTmp,-1,&cmp_sqlstmt,NULL );

    if(returnvalue ==  SQLITE_OK)
    {      
      sqlite3_bind_blob(cmp_sqlstmt, 1, [imgData bytes], [imgData length], NULL);

        sqlite3_bind_int(cmp_sqlstmt, 2, ycount1);


        sqlite3_bind_int(cmp_sqlstmt, 3, ncount1);

        sqlite3_bind_int(cmp_sqlstmt, 4, neutcount1); 

        if(SQLITE_DONE != sqlite3_step(cmp_sqlstmt))
                               NSAssert1(0, @"Error while inserting. '%s'",sqlite3_errmsg(database));

        NSLog(@"insert successfully") ;              

    }
    sqlite3_step(cmp_sqlstmt);

    sqlite3_finalize(cmp_sqlstmt);

   sqlite3_close(database);





    lblvote.text=@"You Voted Yes";

     [newView setHidden:NO];
  }
    else
    {
        sqlite3_close(database); 
   }


  }
   else
  {
   // [self Check_Create_DB];
    //sqlite3 *database;

    sqlite3_open([dbpath UTF8String],&database);

    if(sqlite3_open([dbpath UTF8String],&database)==SQLITE_OK)
    {

        NSLog(@"Connection Open");

        idycount+=1;

         const char  *sqlTmp="update Images set ycount=? where imgid= ?";  


        sqlite3_stmt *cmp_sqlstmt;


        int returnvalue=sqlite3_prepare_v2(database,sqlTmp,-1,&cmp_sqlstmt,NULL);


        NSLog(@"return value.....%d",returnvalue);


        if(returnvalue == SQLITE_OK)

        {  

          sqlite3_bind_int(cmp_sqlstmt,1,idycount );


             sqlite3_bind_int(cmp_sqlstmt,2,idimg );


            NSLog(@"updated");

            if(SQLITE_DONE != sqlite3_step(cmp_sqlstmt))

                NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));


            sqlite3_reset(cmp_sqlstmt);     


        }
        else
        {

            NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));


        }

        sqlite3_step(cmp_sqlstmt);


        sqlite3_finalize(cmp_sqlstmt);


        sqlite3_close(database);



    }
    else
    {

    sqlite3_close(database);


    }


  }
}

2 个答案:

答案 0 :(得分:0)

对我来说,你没有正确关闭数据库(sqlite3_close(数据库))。这可能发生在你的实现如上所述正确关闭数据库之前。尝试重新安装数据库和表

答案 1 :(得分:0)

Offtopic:

对于未来的项目,请查看FMDatabase

  

FMDatabase是围绕SQLite的Objective-C包装器