0

//这里创建数据库

   -(void) createDataBase
   {
           docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

            fullPath = [docPath stringByAppendingPathComponent:@"Recording.sqlite"];
           sqlite3_open([fullPath UTF8String], &dbForEmf);
    }

    // create table

    -(void) createTable
    {    
            if (sqlite3_open([fullPath UTF8String], &dbForE) == SQLITE_OK)
            {
                    const char* createQuery = "CREATE TABLE IF NOT EXISTS EEE(E_ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, E_NAME TEXT,E_READING TEXT,E_DATE TEXT,E_SIZE TEXT,E_LABLE TEXT)";

                    int errorCode = sqlite3_prepare(dbForE, createQuery, -1, &prepareStmt, NULL);

                    if (errorCode == 0) {
                        sqlite3_step(prepareStmt);

                    }
                    else
                    {

                    }
                    sqlite3_finalize(prepareStmt);
                    sqlite3_close(dbForE);
            }
          else
           {
               NSLog(@"data base not Open");
           }
        }
    // insert data here

        -(void) insertData
        {
            if (sqlite3_open([fullPath UTF8String], &dbForE)==SQLITE_OK)
            {    
                NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO EEE(E_NAME,E_READING,E_DATE,E_SIZE,E_LABLE) VALUES('%@','%@','%@','%@','%@')",_referenceObject.lastFilePath,_referenceObject.fileString,_referenceObject.dateString,_referenceObject.fileSizeString,_referenceObject.titleOfButtonString];               
                const char *insert_stmt=[insertSql UTF8String];

                sqlite3_prepare_v2(dbForE,insert_stmt,-1,&prepareStmt,NULL);
                sqlite3_reset(prepareStmt);
                if (sqlite3_step(prepareStmt)==SQLITE_DONE)
                {
                    NSLog(@"inserted the values in table");

                }
                else
                {
                    NSLog(@" not inserted the values in table");
                    NSLog(@"error: %s", sqlite3_errmsg(dbForE));
                }
                sqlite3_finalize(prepareStmt);
                sqlite3_close(dbForE);
            }


        }

当我转到第二个 viewController 并返回到第一个 viewController 时,hai 数据会插入到第一个 viewController 上,数据未插入错误显示数据库已锁定...

4

2 回答 2

1

检查您是否在第二个 ViewController 或其他任何地方打开了数据库。

于 2013-04-25T06:19:04.957 回答
0

在 createDataBase 中,您已经打开了数据库并且它没有关闭,您再次尝试打开它,因此您的数据库首先被锁定,您应该在 createdatabase 函数中关闭数据库。

于 2013-04-25T11:57:00.213 回答