插入数据ios时数据库被锁定

时间:2015-01-27 12:01:48

标签: ios sqlite

现在我基于count 插入到数据库中。如果count大于零而" SELECT" 将会运行,我的数据将会显示,否则" INSERT" 将会发生。现在我的数据库是空的,所以条件转到" INSERT" 部分,并且我得到了#34;数据库被锁定:错误"。我已经通过了一些例子,它说我们必须在开始新的查询之前敲定。我也做过那件事,但我的问题仍然存在。在这里发布我的代码:

 if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:
                              @"SELECT count(*) FROM tables WHERE table_id='%d'",
                              table_id];
        NSLog(@"fetch query is....  %@",querySQL);
        const char *query_stmt = [querySQL UTF8String];
        sqlite3_stmt *countstmt;
        if(sqlite3_prepare_v2(_contactDB, query_stmt, -1, &countstmt, NULL) == SQLITE_OK)
        {

            NSLog(@"FEtch");
            sqlite3_step(countstmt);

            rows = sqlite3_column_int(countstmt, 0);
            NSLog(@"no of rows is %d",rows);
        }
        sqlite3_finalize(statement);
           }
    if(rows>0)
    {
        NSLog(@"NO of rows is : %d",rows);
        //if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
       // {

        NSString *querySQQL = [NSString stringWithFormat:
                               @"SELECT * FROM tables WHERE table_id='%d'",
                               table_id];
        NSLog(@"fetch query is....  %@",querySQQL);
        const char *query_stmmt = [querySQQL UTF8String];
        if (sqlite3_prepare_v2(_contactDB,
                               query_stmmt, -1, &statement, NULL) == SQLITE_OK)
        {
            NSLog(@"yes...");

        }
        while (sqlite3_step(statement) == SQLITE_ROW)
        {

            //int i=0;
            NSLog(@"yesssss...");
            // NSInteger count = sqlite3_column_int(statement, 2);
            //NSLog(@"Rowcount is %d",(long)count);
            table_data= [[NSString alloc]
                         initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            status= [[NSString alloc]
                     initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
            row_id= [[NSString alloc]
                     initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
            table_idd= [[NSString alloc]
                        initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
            //searchResults = [NSMutableArray arrayWithObject:namee];
            NSString *finalarrayy;
            //for(loop=1;loop<=z;loop++)
            //{
            finalarrayy=[NSString stringWithFormat:@"%@,%@,%@,%@",row_id,table_data,status,table_idd];
            [stringArray addObject:table_data];
            NSLog(@"Array....is...%@",stringArray);

        }
        [defaults setObject:stringArray forKey:@"stringarray"];
        [defaults synchronize];
           //}
        sqlite3_finalize(statement);
    }




    else{
        NSLog(@"INsert");
        //if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
        // {
        for(i=mn; i<=o ; i++)
        {
            for(j=1;j<=19;j++)

            {
                k=i*j;
                NSString *val=[NSString stringWithFormat:@"%d   %@   %d   %@   %d",i,@"x",j,@"=",k];
                //tab_data.value=val;
                [stringArray addObject:val];
                NSString *insertSQLL = [NSString stringWithFormat:
                                        @"INSERT INTO tables(table_data,status,table_id) VALUES ('%@', '%@', '%d')",
                                        val,@"NO",table_id];
                NSLog(@"sql stm. is ..%@",insertSQLL);


                const char *insert_stmtt = [insertSQLL UTF8String];
                sqlite3_prepare_v2(_contactDB, insert_stmtt,
                                   -1, &statement, NULL);
                if (sqlite3_step(statement) == SQLITE_DONE)
                {
                    NSLog(@"yes");
                }
                else
                {

                    NSLog(@"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(_contactDB));

                }

            }
        }

   // }

        sqlite3_finalize(statement);
        sqlite3_close(_contactDB);

    }

请告诉我我的代码有什么问题。代码第一次运行,就好像数据库中没有任何内容,然后值插入到数据库中,但是如果我想第二次将新内容插入数据库,那么它的抛出错误&#34;数据库被锁定&#34; 。实际上我在 viewdidload segmentcontrol 的操作中运行相同的代码。对于 viewdidload ,它运行正常,但在 segmentcontrol 的情况下,它正在创建错误,对此有任何想法。如果有人可以修改我的代码,我将非常感谢他/她。

1 个答案:

答案 0 :(得分:0)

  

使用后必须始终关闭sqlite数据库...所以添加此行sqlite3_close(DB);就在sqlite3_finalize(statement);

之后
相关问题