error: database is locked in sqlite - ios

//create database here
-(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 data is insert on first viewController when ever i goto second viewController and come back to first viewController the data is not inserted error displays database is locked...

check if you have leave the database open in second ViewController or anywhere else.

in createDataBase you have opened your database and it was not closed and you again tried to open this hence your database get locked first you should close database in createdatabase function.

Related

SQLite - best practice for opening database

I was in the situation where I've declared sqlite3 variable globally to store database conection and opening database every time without closing it. This lead to memory leaks on my app. I was able to fix this by closing database before open it.
sqlite3 *dbObj;
-(BOOL)openDB
{
//opening database
if (sqlite3_open(dbpath, &dbObj) == SQLITE_OK)
{
NSLog(#"Database opened successfully .....");
return YES;
}
else
{
return NO;
}
}
-(NSArray)getAllTablesInDatabse:(NSString*)database
{
sqlite3_close(dbObj);
[self openDB];
}
This is first way I fixed it.
There's another way I was able to do the same,to check dbObj is nil/NULL in openDB function instead of closing and opening each time. The second way :
-(BOOL)openDB
{
if(dbObj == nil || dbObj == NULL || dbObj = 0)
{
//opening database
if (sqlite3_open(dbpath, &dbObj) == SQLITE_OK)
{
NSLog(#"Database opened successfully .....");
return YES;
}
else
{
return NO;
}
}
else
{
return YES;
}
}
-(NSArray)getAllTablesInDatabse:(NSString*)database
{
[self openDB];
}
Which is best way from performance and other perspective ?
1) It's a good practice to close database after completion of desired task. For example you are creating one table then do like,
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"contacts.db"]];
sqlite3_stmt *statement;
if (sqlite3_open([self.databasePath UTF8String], &_contactDB) == SQLITE_OK) {
NSLog(#"database opened successfully");
NSString *query = #"create table if not exists Employee (id integer primary key, name text, surname text, roll text, salary integer)";
const char *queryStatement2 = [query UTF8String];
char *errorMessage;
if (sqlite3_exec(self.contactDB, queryStatement2, NULL, NULL, &errorMessage) == SQLITE_OK) {
if (!errorMessage) {
NSLog(#"table created sucessfully!");
}
else{
NSLog(#"error : %s",errorMessage);
}
}
}
sqlite3_close(self.contactDB);
So, You should close your database every time after use.
2) It is good to check before opening database that if it is open or not compare to close database every time before open.
3) If you close database every time then it will definitely open every time so there is no need to put any condition.

implementing WAL in iOS?

The code I placed below works. So my scenario is I have the code below in a class. I use this class simply for merging tables and updating one table. When I call the object that this code lives in the app delegate for example, it works great! But when I call the same object after clicking a button in a tableview controller, I get a database lock error. So here's what I am wondering. After reading sqlite documentation WAL: http://www.sqlite.org/wal.html I am thinking that I cannot read and update concurrently to the sqlite db, right? I am unsure of a solution around this, so what would be a good suggestion around my problem. Remember, keep in mind this code works just fine in the app delegate, it does not work in my tableview controller when I call it using action upon clicking the button. Note: I SELECT data from the SQLITE database to display table cell names etc. Thanks ahead of time!
//Allocates a filemanager object. Ideally, this object is used for searching through the applications context
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dbErr = NO;
//Boolean variable to tell if the database exists
BOOL error,mainDbError;
//Looks through all the databases. If there is a database that does not exist, the following error message will appear to the user. If all the databases exist on the system, the database opens respectively
error = [self checkAndOpenSyncDB];
mainDbError = [self checkAndOpenMainDB];
if (!error&&mainDbError) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops!"
message:#"We didn't mean for this to happen. Looks like there was a problem loading the sync database. Contact technical support for further assistance."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else{
//This is I think to check when the last time the sync occured on the system.. not quite sure though1
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSLog(#"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(syncOpenHandle));
char *errorMessage;
int errorNum = 0;
//Atataching the sync db to the master db
NSString *attachSQL = [NSString stringWithFormat:#"ATTACH DATABASE \'%#\' AS sync_db", self->pathForSync];
NSLog(#"PATH FOR SYNC !!!!!!! %#", pathForSync);
NSLog(#"Here's the arratch string: %#", attachSQL);
//
if (sqlite3_exec(syncOpenHandle, [attachSQL UTF8String], NULL, NULL, &errorMessage) == SQLITE_OK) {
NSString *masterQuery = [NSString stringWithFormat:#"SELECT name FROM sqlite_master WHERE type='table';"];
const char *masterStmt = [masterQuery UTF8String];
sqlite3_stmt *statement;
BOOL loopErr;
loopErr = sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL);
if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)==SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString * currentTable = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSLog(#"Here's the current table: %#",currentTable);
//This is where the magic happens. If there are any keys matching the database, it will update them. If there are no current keys in the database, the query will insert them.
if ([currentTable isEqualToString:#"USER_DATA"] == NO && [currentTable isEqualToString:#"USER_ACTIVITY"]== NO && [currentTable isEqualToString:#"USER_ITINERARY"] == NO) {
NSString *tblUpdate = [NSString stringWithFormat:#"INSERT or REPLACE INTO main.%# SELECT * FROM sync_db.%#;",currentTable, currentTable];
const char *updateStmt = [tblUpdate UTF8String];
//sqlite3_busy_timeout (mainOpenHandle, 60000);
bool update;
update = sqlite3_exec(syncOpenHandle, updateStmt, NULL, NULL, &errorMessage)== SQLITE_OK;
NSLog(#"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
if (sqlite3_exec(syncOpenHandle, updateStmt, NULL, NULL, &errorMessage)== SQLITE_OK) {
NSLog(#"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
if (errorNum == 1) {
//A database reset is needded
//self->isResetDataBase = YES;
}
dbErr = YES;
}
}
}
NSLog(#"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
}
NSLog(#"Here's the error num %d", errorNum);
NSLog(#"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(syncOpenHandle));
NSLog(#"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
sqlite3_finalize(statement);
//Detaching the database from the mainDB
NSString *detachSQL = [NSString stringWithFormat:#"DETACH DATABASE sync_db"]; // reference sync db
if ((errorNum = sqlite3_exec(syncOpenHandle, [detachSQL UTF8String], NULL, NULL, &errorMessage))!= SQLITE_OK) {
NSLog(#"Detatched syncDb Failed. ErrorMessage = %s ", errorMessage);
}
}
}
NSLog(#"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
//Closing the database when finished.
if (syncOpenHandle!=nil) {
sqlite3_close(syncOpenHandle);
NSError *err;
int success = [fileManager fileExistsAtPath:pathForSync];
if (success) {
[[NSFileManager defaultManager]removeItemAtPath:pathForSync error:&err];
}
}

Communication of the database

I am working with the communication of sqlite database from one viewcontroller to another, but some how i am not getting the database on the second view controller. Bellow is the code which i am using for it.:-
On First View controller
//Creating a table of MOtivational Thoughts
dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPath objectAtIndex:0];
Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"S.H.E.D_DB"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([ filemgr fileExistsAtPath: Second.databasePath] == NO) {
const char *dbpath = [Second.databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS THOUGHTS (ID integer ,Motivation_Thaought TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Failed to create table");
}
else
{
NSLog(#" created table");
}
sqlite3_close(contactDB);
} else {
}
}
// Fetching Data from table of MOtivational Thoughts
if(sqlite3_open([Second.databasePath UTF8String], &contactDB) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
int randomNumber = [self getRandomNumberBetween:0 to:6];
NSString *queryString = [NSString stringWithFormat:#"Select * FROM THOUGHTS where ID= '%d'",randomNumber];
const char* sql = [queryString UTF8String];
NSLog(#"path value : %#", Second.databasePath);
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(contactDB, sql, -1, &compiledStatement, nil)==SQLITE_OK) {
// Loop through the results and add them to the feeds array
NSLog(#"Ready to enter while loop");
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSLog(#"reading");
NSString *aid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
motivationView.text = aid;
NSLog(#"Thought of the day %#", aid);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(contactDB);
// Do any additional setup after loading the view from its nib.
}
On Second View Controller
NSLog(#"path value : %#", databasePath);
if(sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK){
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS SHEDSLIST (SHED_ID integer ,SHED_Name TEXT ,SHED_Description TEXT, SHED_TIME DATETIME)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Failed to create table");
}
else
{
NSLog(#" created table");
}
sqlite3_close(contactDB);
} else {
}
PLease locate my error and give me sutable solution.
May be you should access the database path as self.databasePath in SecondViewController because I believe you have created a property for that. Instead of that you are trying to access some instance variable databasePath. Look at your following code in FirstViewController:
Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"S.H.E.D_DB"]];

Nesting of Sql Statements

Hi i have made a function in my app delegate to remove the redundancy of my database , I am not sure do i have coded right as i have to perform nesting of SQL statements to find out the error in DB.
Can any body suggest me where i am wrong because the application is running well in simulator and crashing in Device.
I am even Not Sure where to Put finalize and sqlite3_close . Kindly help
-(void) removeRedundancy2
{
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dbPathString = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:#"turfnutritiontool_ver_99.db"];
sqlite3_stmt *selectStmt;
sqlite3_stmt *selectStmt1;
BOOL isMyFileThere = [[NSFileManager defaultManager] fileExistsAtPath:dbPathString];
if (isMyFileThere)
{
if (sqlite3_open([dbPathString UTF8String], &database1)==SQLITE_OK)
{
// TO REMOVE FROM from tnt_scenario_product when NO ProductID Found
NSString *querySql2= [NSString stringWithFormat:#"SELECT productid from tnt_scenarioproduct"];
const char* query_sql2 = [querySql2 UTF8String];
if(sqlite3_prepare_v2(database1, query_sql2, -1, &selectStmt, NULL) == SQLITE_OK)
{
while (sqlite3_step(selectStmt) == SQLITE_ROW)
{
int productid = sqlite3_column_int(selectStmt, 0);
// NSLog(#"ProductId1 =%d",productid);
NSString *querySql21= [NSString stringWithFormat:#"SELECT productid from tnt_productcontent WHERE productid = %d",productid];
const char* query_sql21 = [querySql21 UTF8String];
if(sqlite3_prepare_v2(database1, query_sql21, -1, &selectStmt1, NULL) == SQLITE_OK)
{
if (sqlite3_step(selectStmt1) == SQLITE_ROW)
{
// DO NOTHING
}
else
{ // to delete scenario without product id
NSLog(#"Delete this Product from TPC 2 %d",productid);
NSString *querydelete2= [NSString stringWithFormat:#"DELETE from tnt_scenarioproduct WHERE productid = %d",productid];
const char* query_delete2 = [querydelete2 UTF8String];
char *error;
sqlite3_exec(database1, query_delete2, NULL, NULL, &error);
NSLog(#"error=%s ",error);
sqlite3_finalize(selectStmt1);
}
}
sqlite3_finalize(selectStmt1);
sqlite3_close(database1);
}
sqlite3_finalize(selectStmt);
}
sqlite3_close(database1);
}
sqlite3_close(database1);
}
}
After calling sqlite3_open, you must call sqlite3_close for that connection exactly once.
After calling sqlite3_prepare_v2, you must call sqlite3_finalize for that statement exactly once.
if (sqlite3_open([dbPathString UTF8String], &database1)==SQLITE_OK)
{
...
if(sqlite3_prepare_v2(database1, query_sql21, -1, &selectStmt1, NULL) == SQLITE_OK)
{
...
}
sqlite3_finalize(selectStmt);
....
}
sqlite3_close(database1);
Furthermore, you should not reuse the same variable (selectStmt) for two different queries to prevent confusion about its lifetime.

Checking if a table exists and if it does not exist, create it... iOS/SQLite

I currently have a table that exists which I'm able to pull data from. What I would like to do is check if the table already exists in the bundle, and if it does not, I want to create the table and save it to the bundle (meaning the path would be in the main bundle). I want the database to be checked and created at the top of the setInput method. I've been scouring SO for something similar to this, but I haven't come up with anything yet. Any help is very appreciated. Here is my code:
-(IBAction)setInput:(id)sender
{
NSString *strStoreNumber;
NSString *strRegNumber;
strStoreNumber = StoreNumber.text;
strRegNumber = RegNumber.text;
lblStoreNumber.text = strStoreNumber;
lblRegNumber.text = strRegNumber;
NSString* databasePath = [[NSBundle mainBundle] pathForResource:#"tblStore" ofType:#"sqlite"];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSLog(#"Opened sqlite database at %#", databasePath);
sqlite3_exec(database, "CREATE TABLE IF NOT EXISTS tblStore (ID INTEGER PRIMARY KEY AUTOINCREMENT, Message TEXT)", NULL, NULL, NULL);
//...stuff
}
else
{
NSLog(#"Failed to open database at %# with error %s", databasePath, sqlite3_errmsg(database));
sqlite3_close (database);
}
//
NSString *querystring;
// create your statement
querystring = [NSString stringWithFormat:#"SELECT strStore, strReg FROM tblStore WHERE strStore = %# AND strReg = %#;", strStoreNumber, strRegNumber];
const char *sql = [querystring UTF8String];
NSString *szStore = nil;
NSString *szReg = nil;
sqlite3_stmt *statement = nil;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL)!=SQLITE_OK) //queryString = Statement
{
NSLog(#"sql problem occured with: %s", sql);
NSLog(#"%s", sqlite3_errmsg(database));
}
else
{
// you could handle multiple rows here
while (sqlite3_step(statement) == SQLITE_ROW)
{
szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
}
}
sqlite3_finalize(statement);
lblStoreNumber.text = szStore;
lblRegNumber.text = szReg;
//
}
I'm still quite new to iOS and SQLite, so if I did not provide an accurate enough description of what I'm trying to do, let me know and I'll try to be more specific. Thanks!
A quick search for "iphone sql create table if not exists" gave this as the top result.
This part of the SQL is probably what you are looking for:
CREATE TABLE IF NOT EXISTS tableName( ... )
It creates a table if it does not already exist.
Create table using sqlite swift
func createTable(_ tableName:String) {
sqlStatement = "CREATE TABLE \(tableName)(Id TEXT,Details BLOB,Type TEXT)"
if sqlite3_prepare_v2(database, sqlStatement,-1, &compiledStatement, nil) == SQLITE_OK {
if sqlite3_step(compiledStatement) == SQLITE_DONE {
print("table created")
}
else {
print("table could not be created")
}
}
else {
print("Create table statement could not be prepared")
}
sqlite3_finalize(compiledStatement)
}

Resources