//在这里创建数据库
-(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);
}
}
hai数据插入第一个viewController时,我转到第二个viewController并返回第一个viewController数据未插入错误显示数据库被锁定...
答案 0 :(得分:1)
检查您是否在第二个ViewController或其他任何地方打开数据库。
答案 1 :(得分:0)
在createDataBase中你已经打开了数据库而它没有关闭,你再次尝试打开它,因此你的数据库首先被锁定你应该在createdatabase函数中关闭数据库。