sqlite3 select query hang in IOS6 - iphone

I can receive the data from .sql file(sqlite) in iOS5 , But in ios6. The app is getting hang..
If the query is select * from Table then works perfectly
If query is select *from Table order by ID DESC then the app is getting hang
some times the app hang in (sqlite3_open([dbPath UTF8String],&db) == SQLITE_OK)
some times hangs in
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}

Finalize the statements and Close the connection properly each you perform an operation, then you should be fine with the sqlite. If the statements are not finalized then you get to states like SQLITE_BUSY,SQLITE_LOCKED which you need to handle. If the app is hanging that means your main thread is blocked. Following is a sample sqlite operation.
-(int)keyIdForImgId:(int)ImgId
{
#synchronized(self)
{
int keyId=0;
sqlite3 *database=nil;
if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK) {
const char *sql = "SELECT keyId From SomeTableName WHERE imageId=?";
sqlite3_stmt *selectstmt=nil;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(selectstmt, 1, ImgId);
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
keyId = sqlite3_column_int(selectstmt, 0);
}
}
sqlite3_finalize(selectstmt);
sqlite3_close(database);
}
return keyId;
}
}

Related

iPhone SQLite Crash sqlite3_last_insert_rowid or sqlite3_close.

I'm having two crashes show up in my logs using the following code to insert into my SQLite DB - the code works perfectly most of the time however clearly there is a something wrong that's causing the crash.
It crashes on either sqlite3_last_insert_rowid or sqlite3_close with EXC_BAD_ACCESS (I hold a reference to the SMSDatabase and it's in a singleton not sure why it would be deallocated) and SIGABRT (memory issue?)
- (NSInteger)query:(NSString *)query {
NSInteger lastRowID = 0;
if (sqlite3_open([self.databasePath UTF8String], &smsDatabase) == SQLITE_OK)
{
sqlite3_stmt *statement = nil;
const char *sql = [query UTF8String];
if (sqlite3_prepare_v2(smsDatabase, sql, -1, &statement, NULL) == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_DONE)
{
lastRowID = sqlite3_last_insert_rowid(smsDatabase);
}
}
sqlite3_finalize(statement);
}
sqlite3_close(smsDatabase);
return lastRowID;
}
Where am I going wrong?
sqlite3_close(smsDatabase); - isn't this outside your if statement, which means you are closing a database which may have never been opened ?

Data inserted not retained unless I restart the application

I am using SQLITE with my iPhone App. After Inserting the values in the detailedViewController, I am reloading the data from the database in the masterViewController viewWillAppear method. However I can't get the newly updated row there unless I restart the application.
I am finalizing the complied statement and closing the database after INSERT.
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
Any pointers as to where I might be doing something wrong?
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "SELECT * FROM MYCAR1";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
//...getting data from compiled statements...
MyCar *car=[[MyCar alloc] initWithVIN:vin.intValue andMake:make andModel:model andYear:year andColor:color andImageData:image];
[myArray addObject:car];
}
}
sqlite3_finalize(compiledStatement);
carList=myArray;
}
sqlite3_close(database);
[self.tableView reloadData];
I have added exact code for selecting, which is working for me. Try to compare this with your code and figure out the error.
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK)
{
NSString *querySql=[NSString stringWithFormat:#"Select * from xxx"];
const char *sqlStatement=[querySql UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the your array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
//get the data from db here
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);

locked sqlite file on iPhone

Until now all my database access has been reading, but now I need to update (and after this insert)
I have a database containing 'shows' in the app directory (read-only) and that's ok for me (I don't want to copy it to the documents folder as it's rather big and I don't need to change stuff in it.
But I want the user to select some shows as his favorite. Therefore I created a database with a table 'favorite_shows' in the documents folder. It contains 4 fields:
ID (prim key)
show_id
is_favorite
remarks (currently not yet in use)
The user is able to toggle the 'is_favorite' status ONCE, after that I'm getting an error when trying to update:
SQLITE_BUSY 5 /* The database file is locked */
This is my code:
if (sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
sqlStatement = "select * from favorite_shows WHERE (show_id = ?)";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, self.ID);
// search for a show
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// if we can find one, toggle the status
favID = sqlite3_column_int(compiledStatement, 0); // we need the primary key to update it
isFav = (sqlite3_column_int(compiledStatement, 2) == 1); // let's store the favorite status
sqlStatement = "update favorite_shows SET is_favorite = ? WHERE (ID = ?)";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, !isFav );
sqlite3_bind_int(compiledStatement, 2, favID);
int error = sqlite3_step(compiledStatement);
if (SQLITE_DONE != error) {
NSLog(#"error while updating favorite status");
}
}
}
//else : no records found indicating that this show hasn't been a favorite yet, so insert one as favorite
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
What is the reason that it's locked the second time ? Is there some other instruction to give besides :
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
to close everything ?
EDIT:
The BUSY is a result of re-using compiledStatement without deleting the previously compiled statment. You need to free resources properly using finalize and close functions.
Refer to the docs here. http://sqlite.org/c3ref/stmt.html
const char * select = "select * from favorite_shows WHERE (show_id = ?)";
const char * update = "update favorite_shows SET is_favorite = ? WHERE (ID = ?)";
sqlite3_stmt *selectStmt;
sqlite3_stmt *updateStmt;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
if(sqlite3_prepare_v2(database, select, -1, &selectStmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(selectStmt, 1, self.ID);
// search for a show
if(sqlite3_step(selectStmt) == SQLITE_ROW) {
// if we can find one, toggle the status
favID = sqlite3_column_int(selectStmt, 0); // we need the primary key to update it
isFav = (sqlite3_column_int(selectStmt, 2) == 1) ? 0 : 1; // Flip is_favorite value
sqlite3_finalize(selectStmt); // Delete the statement OR create a new one
if(sqlite3_prepare_v2(database, update, -1, &updateStmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(updateStmt, 1, isFav );
sqlite3_bind_int(updateStmt, 2, favID);
int error = sqlite3_step(updateStmt);
if (SQLITE_DONE != error) {
NSLog(#"error while updating favorite status");
} else {
sqlite3_finalize(updateStmt);
}
}
} //else : no records found indicating that this show hasn't been a favorite yet, so insert one as favorite
}
sqlite3_close(database);
}

sqlite3_prepare_v2 problem

I'm having a bit of trouble with statement:
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
in the code below, the code is jumping out of the IF at this point. Anyone any thoughts ?
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select route_name from Route";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
// Add the animal object to the animals Array
//[list addObject:animal];
[list addObject:aName];
//[animal release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
Problem solved, when I view the database through the Firefox plugin SLQite Manage, the table name is actually called ZROUTE.

sqlite3 is not working in my iphone application

I want to use sqlite3 for my iphone application. But it is not working. Scenario is :- On click event of a button a method should run and retrieve the data from the database. But My sequence of program is not going into
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from records";
sqlite3_stmt *compiledStatement;
0NSLog(#"%s", sqlite3_errmsg(database)); // Results - no error
-
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
//here NSLog statement does not work.
There is no problem in opening a database. What should be the reason? Any solution appreciable.
Thanks
Try this: NSLog(#"%s", sqlite3_errmsg(database)); and see if there are some errors in your query.
Here is a modified example of my own code :
NSString *file = #"youdatabase.db"
sqlite3 *db;
if(sqlite3_open([file UTF8String], &db) == SQLITE_OK)
{
NSString *req = #"SELECT * FROM totoTable";
sqlite3_stmt *returnReq;
if(sqlite3_prepare(db, [req cStringUsingEncoding:NSUTF8StringEncoding], -1, &returnReq, NULL) == SQLITE_OK)
{
while (sqlite3_step(returnReq) == SQLITE_ROW)
{
//Printf first returned value
NSLog(#"%s", (char*)sqlite3_column_text(returnReq, 0));
}
}
sqlite3_close(db);
}
Hope it will help you ;-)