SqlLite Not execute query Some time - iphone

I have Sequence of Queries that need to be performed with database..
Most of time its working fine.. but some time it failed to insert query.
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
for (int i=0;i<[queries count]; i++)
{
NSString *query = [queries objectAtIndex:i];
const char *Insert_query = [query UTF8String];
sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
//NSLog(#" \n\n\n\n %# done query",query);
}
else {
NSLog(#" \n\n\n\n %# not done query",query);
}
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
Above is code which i have implemented to perform insert operation...
Can any one help me to find if it fails then for what reason it failed to insert to database so i can handle error..

Use this method to execute query on sqlite
//-----------------------------------------------------------------------------------------------------//
#pragma mark - Helper methods
//-----------------------------------------------------------------------------------------------------//
-(BOOL)dbOpenedSuccessfully
{
if(sqlite3_open([[self dbPath] UTF8String], &_database) == SQLITE_OK)
{
return YES;
}
else
{
[[[UIAlertView alloc]initWithTitle:#"Error"
message:#"Error on opening the DB"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil]show];
return NO;
}
}
//-----------------------------------------------------------------------------------------------------//
#pragma mark - Query
//-----------------------------------------------------------------------------------------------------//
- (void) executeQuery:(NSString *)strQuery
{
char *error = NULL;
if([self dbOpenedSuccessfully])
{
NSLog(#"%#",strQuery);
sqlite3_exec(_database, [strQuery UTF8String], NULL, NULL,&error);
if (error!=nil) {
NSLog(#"%s",error);
}
sqlite3_close(_database);
}
}
Also If insert not works properly the reason may be the file is not in the documents directory and if it is there in bundle it will fetch tha data but cannot update or insert value if db is in bundle ,Copy it to the documents directory and then try using it
-(void) checkAndCreateDatabase
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:_databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];
}
For more info see this

You can try printing the error in the following way, Based on the error you can make decision.
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
for (int i=0;i<[queries count]; i++)
{
NSString *query = [queries objectAtIndex:i];
const char *Insert_query = [query UTF8String];
sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
//NSLog(#" \n\n\n\n %# done query",query);
}
else {
NSLog(#"sqlite3_step error: '%s'", sqlite3_errcode(contactDB));
NSLog(#" \n\n\n\n %# not done query",query);
}
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
Additionally,
SQLITE_DONE means that the statement has finished executing
successfully. sqlite3_step() should not be called again on this
virtual machine without first calling sqlite3_reset() to reset the
virtual machine back to its initial state.
You can use, SQLITE_OK instead of SQLITE_DONE.

Related

Prepare-error : no such table: items

this function is showing me the error that "Prepare-error #0: no such table: items
". can some one please help me out in resolving this error.
- (void)viewDidLoad
{
[super viewDidLoad];
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB)== SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"Select name FROM items"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{ NSLog(#"Data not fetched");
if (sqlite3_step(statement) == SQLITE_ROW)
{NSLog(#"Prepare-error #%i: %s", (sqlite3_prepare_v2(contactDB, [querySQL UTF8String], -1, &statement, NULL) == SQLITE_OK), sqlite3_errmsg(contactDB));
NSString *namefeild = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
[list objectAtIndex:namefeild];
}
else{
NSLog(#"Data not fetched");
}
sqlite3_finalize(statement);
}else {NSLog(#"Prepare-error #%i: %s", (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK), sqlite3_errmsg(contactDB));}
sqlite3_close(contactDB);
}
These two functions are n different viewcontrollers.
In view didload the database is created.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPath;
dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPath objectAtIndex:0];
databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"contactDB"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath] == NO) {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) {
char *errMsg;
const char *sql_stmt = "create table if not exists items(name varchar, price integer, description varchar)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Fail to create table");
}
sqlite3_close(contactDB);
}else{
NSLog(#"Failed to open database");
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
and in save action the data is added to the database.
- (IBAction)save:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [ databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:#"insert into items(name, price, description) values (\"%#\",\"%#\",\"%#\")", nametxt.text, pricetxt.text, description.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"contact added");
nametxt.text= #"";
pricetxt.text = #"";
description.text = #"";
}else{
NSLog(#"Failed to add contact");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
const char *dbpath = [databasePath UTF8String];
NSLog("%#", databasePath);
...
...
}
Copy the database path to your clipboard and paste it into your console.
cd "[database path]"
sqlite3 database_filename.db
.dump
After the .dump command, do you see the creation statement for your table? If not, then you need to double check the location of where you are actually creating your database. It would actually be very helpful if you updated your answer with the contents of the .dump command.
Typically this means that the table does not exist in the database you opened. You should find the database in your simulator's Documents folder (~/Library/Application Support/iPhone Simulator) and, open it in your MacOS SQLite tool of choice, see for yourself whether the table is there. I suspect it will not be there.
A common source of this problem is for a file at the databasePath to not exist (e.g. you might have copy of database in the bundle, but not the Documents folder), in which case sqlite3_open will quietly create a new, blank database at databasePath.
Assuming you don't want it to create a blank database when it doesn't find it, you should:
Remove your app from the simulator/device (so that any blank databases are removed);
Check your original opening routine and use NSFileManager to check for the existence of the database if it's not already there (perhaps copying the database from the bundle to documents before continuing);
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:databasePath]) {
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"itemsdb" ofType:#"sqlite"];
[fileManager copyItemAtPath:bundlePath toPath:databasePath error:nil];
}
Or perhaps your code should dynamically create the table(s) if the database didn't exist, but the idea is the same. Check for existence of the file before opening it.
Perhaps in the future, consider using sqlite3_open_v2 with the SQLITE_OPEN_READWRITE option (but not the SQLITE_OPEN_CREATE option), which will not create the database for you and will report an error if the database was not found.
Having said the above (which is the general counsel when someone encounters an error like yours, where the table that you know "should" be there, isn't), there are specific issues unique to your code sample in the way you handle error reporting:
If step succeeds, you're reporting an error. Surely you meant to only do that if step failed.
The error you generate as a result of step says "prepare error". Surely that should be "step error".
Your logging of errors is calling the function that failed again in order to get the return code. You should save the return code when you first called the function, saving you from having to call it again for your error message. (This is important because sometimes the value returned by the function will change and reset your error message. Don't call the failed function again!) It's also more efficient to just save the original return code.
Thus:
if (sqlite3_open(dbpath, &contactDB)== SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"Select name FROM items"];
const char *query_stmt = [querySQL UTF8String];
int rc; // variable to hold the return code
if ((rc = sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)) == SQLITE_OK)
{
if ((rc = sqlite3_step(statement)) == SQLITE_ROW)
{
NSString *namefeild = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
[list objectAtIndex:namefeild];
}
else {
if (rc == SQLITE_DONE)
NSLog(#"step found no data");
else
NSLog(#"step-error #%i: %s", rc, sqlite3_errmsg(contactDB));
}
sqlite3_finalize(statement);
} else {
NSLog(#"Prepare-error #%i: %s", rc, sqlite3_errmsg(contactDB));
}
sqlite3_close(contactDB);
}

Cannot insert data into sqlite3 database using iOS

Using MacOS Terminal I created a database name database.sql and inserted some records. Using iOS I can retrive the inserted values.
But using iOS code I tried to insert the record to the database and it does not enter the record in the database.
Should I set the need to set some permission? This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self openDB];
}
-(IBAction)save:(id)sender{
[self insertRecordIntoTableNamed:#"Contacts"
field1Value:fname.text
field2Value:lname.text
field3Value:comp.text
field4Value:email.text
field5Value:pnumber.text
field6Value:mnumber.text
field7Value:add.text
field8Value:city.text
field9Value:state.text];
}
-(void)openDB{
NSString *sqlfile=[[NSBundle mainBundle]pathForResource:#"database" ofType:#"sql"];
if(sqlite3_open([sqlfile UTF8String], &db)!= SQLITE_OK){
sqlite3_close(db);
NSLog(#"Database connected");
NSAssert(0,#"Database failed to open");
}
else
{
NSLog(#"Database connected");
}
}
-(void) insertRecordIntoTableNamed:(NSString *) tableName
field1Value:(NSString *) field1Value
field2Value:(NSString *) field2Value
field3Value:(NSString *) field3Value
field4Value:(NSString *) field4Value
field5Value:(NSString *) field5Value
field6Value:(NSString *) field6Value
field7Value:(NSString *) field7Value
field8Value:(NSString *) field8Value
field9Value:(NSString *) field9Value {
NSString *sql = [NSString stringWithFormat:#"INSERT INTO %# VALUES ('%#','%#','%#','%#',%#,%#,'%#','%#','%#');",tableName, field1Value, field2Value,field3Value,field4Value,field5Value,field6Value,field7Value ,field8Value,field9Value];
NSLog(#"%#",sql);
// char *err;
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)
{
if (SQLITE_DONE!=sqlite3_step(statement))
{
sqlite3_close(db);
NSAssert(0, #"Error updating table.");
}
else{
NSLog(#"Success");
NSLog(#"%#",sql);
}
}
sqlite3_finalize(statement);
}
#end
Here is what i have done:
change the folder and path permissions using CHMOD
split the sqlite3_exec() into sqlite3_prepare(), sqlite3_step() and sqlite3_finalize() - I get the same output - query is created but record is not created in database
I am able to retrieve record information
You can't write to a database in the resource bundle. You need to copy it and then use it.
Here's some code I've successfully used to do that (key is ensureDatabasePrepared where it is copied from resources):
- (BOOL)ensureDatabaseOpen: (NSError **)error
{
// already created db connection
if (_contactDb != nil)
{
return YES;
}
NSLog(#">> ContactManager::ensureDatabaseOpen");
if (![self ensureDatabasePrepared:error])
{
return NO;
}
const char *dbpath = [_dbPath UTF8String];
if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
error != nil)
{
*error = [[[NSError alloc] initWithDomain:#"ContactsManager" code:1000 userInfo:nil] autorelease];
return NO;
}
NSLog(#"opened");
return YES;
}
- (BOOL)ensureDatabasePrepared: (NSError **)error
{
// already prepared
if ((_dbPath != nil) &&
([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
{
return YES;
}
// db in main bundle - cant edit. copy to library if !exist
NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:#"contacts" ofType:#"db"];
NSLog(#"%#", dbTemplatePath);
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
_dbPath = [libraryPath stringByAppendingPathComponent:#"contacts.db"];
NSLog(#"dbPath: %#", _dbPath);
// copy db from template to library
if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
{
NSLog(#"db not exists");
NSError *error = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
{
return NO;
}
NSLog(#"copied");
}
return YES;
}
If you are interacting with the database only from the code you posted - you are missing a sqlite3_close. Most likely the changes are not getting flushed onto disk

LocalStorage or SQLite ?

I'm working on my app that let me to store my online DVD's information on my iphone. Just wonder to use localStorage or SQLite. Also I'm gonna update my app in the future and I dont want to loose my stored data. Which one do you suggest me ?!
Cheers
my online DVD's information
can be large information, then use core data or SQLite.
Also I'm gonna update my app in the future and I dont want to loose my
stored data
if you update without deleting the old version, SQLite data remains. This works same for NSUserDefault too. You can use Keychains for permanant data storage, but for small amount of data. Can't say more without knowing more about your requirement.
I do not know much about localStograge anyway SQLite will give you good options:
Portable to Android
Can be updated without losing your previous database.
here is a sample code to init DB and upgrade it.
+(void) InitalizeDB
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"QuestionsDB.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
if (success)
{
if (sqlite3_open([dbPath UTF8String], &gDatabase) != SQLITE_OK)
{
sqlite3_close(gDatabase);
gDatabase = nil;
}
}
if (gDatabase != nil)
[self Upgrade];
}
+(void)Upgrade
{
sqlite3 *NewDatabase = nil;
NSString *NewDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"QuestionsDB.sqlite"];
const char *szVersion = "SELECT DBVersion FROM Version";
sqlite3_stmt *sqlOldVersion;
sqlite3_stmt *sqlNewVersion;
if (sqlite3_open([NewDBPath UTF8String], &NewDatabase) != SQLITE_OK)
{
sqlite3_close(NewDatabase);
NewDatabase = nil;
}
if (NewDatabase != nil)
{
if (sqlite3_prepare_v2(gDatabase, szVersion, -1, &sqlOldVersion, NULL) != SQLITE_OK)
NSAssert1(0, #"Failed to compile sql statment %s", sqlite3_errmsg(gDatabase));
if (sqlite3_prepare_v2(NewDatabase, szVersion, -1, &sqlNewVersion, NULL) != SQLITE_OK)
NSAssert1(0, #"Failed to compile sql statment %s", sqlite3_errmsg(NewDatabase));
if (sqlite3_step(sqlOldVersion) == SQLITE_ROW && sqlite3_step(sqlNewVersion) == SQLITE_ROW)
{
if (sqlite3_column_int(sqlOldVersion, 0) < sqlite3_column_int(sqlNewVersion, 0))
{
[self UpgradeCategories:NewDatabase];
[self UpgradeQuestions:NewDatabase];
[self UpgradeQuestionChoices:NewDatabase];
}
}
else
{
NSAssert(0, #"Failed to retreive questions database version.");
}
}
sqlite3_close(NewDatabase);
}

XCode sqlite3 - SELECT always return SQLITE_DONE

a noob here asking for help after a day of head-banging....
I am working on an app with sqlite3 database with one database and two tables. I have now come to a step where I want to select from the table with an argument. The code is here:
-(NSMutableArray*) getGroupsPeopleWhoseGroupName:(NSString*)gn;{
NSMutableArray *groupedPeopleArray = [[NSMutableArray alloc] init];
const char *sql = "SELECT * FROM Contacts WHERE groupName='?'";
#try {
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
NSString *theDBPath = [docsDir stringByAppendingPathComponent:#"ContactBook.sqlite"];
if (!(sqlite3_open([theDBPath UTF8String], &database) == SQLITE_OK))
{ NSLog(#"An error opening database."); }
sqlite3_stmt *st;
NSLog(#"debug004 - sqlite3_stmt success.");
if (sqlite3_prepare_v2(database, sql, -1, &st, NULL) != SQLITE_OK)
{ NSLog(#"Error, failed to prepare statement."); }
//DB is ready for accessing, now start getting all the info.
while (sqlite3_step(st) == SQLITE_ROW)
{
MyContacts * aContact = [[MyContacts alloc] init];
//get contactID from DB.
aContact.contactID = sqlite3_column_int(st, 0);
if (sqlite3_column_text(st, 1) != NULL)
{ aContact.firstName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(st, 1)]; }
else { aContact.firstName = #""; }
// here retrieve other columns data ....
//store these info retrieved into the newly created array.
[groupedPeopleArray addObject:aContact];
[aContact release];
}
if(sqlite3_finalize(st) != SQLITE_OK)
{ NSLog(#"Failed to finalize data statement."); }
if (sqlite3_close(database) != SQLITE_OK)
{ NSLog(#"Failed to close database."); }
}
#catch (NSException *e) {
NSLog(#"An exception occurred: %#", [e reason]);
return nil; }
return groupedPeopleArray;}
MyContacts is the class where I put up all the record variables.
My problem is sqlite3_step(st) always return SQLITE_DONE, so that it i can never get myContacts. (i verified this by checking the return value).
What am I doing wrong here?
Many thanks in advance!
I think you are not binding the value, if not use this
sqlite3_bind_text(stmt, 1, [groupName UTF8String], -1, SQLITE_STATIC);
You're not binding any value to your statement.
You're literally executing SELECT * FROM Contacts WHERE groupName='?' as is.
And that likely returns an empty set, which is why sqlite3_step returns SQLITE_DONE, there's nothing to read in the set, you're done.
This page has an example of binding parameters to a statement..
EDIT: Also, you don't need the quotes around ?
SELECT * FROM Contacts WHERE
groupName=?
then use sqlite3_bind_text

Bulk Insert to Sqlite3 causing Db Error: Library routine called out of sequence

I need to load some configuration data after app updates for my iphone app. I bundle in a file with a bunch of SQL statements (800+) to run on first launch. It looks like I may be painted in a corner now. If I run it on the main thread at startup, it take so long to run that the app crashes due to the startup taking too long. If I run it on a separate thread, I am getting database contention errors (Library routine called out of sequence). I think this is because the app is continuing to load and read the DB on the main thread.
Here is the method that receives the data form the CSV file and then loops through and writes to the DB.
Any ideas about how to either make this run faster on startup or run successfully without contention on a low priority background thread?
+ (void) updateDB:(NSString *)data {
NSArray *lineArray = [data componentsSeparatedByString:#"\n"];
if (sqlite3_open([[BIUtility getDBPath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSLog(#"Failed to opendatabase in updateDB");
}
char *errorMsg;
for(int k = 0; k < [lineArray count]; k++){
NSString *loadSQLi = [lineArray objectAtIndex:k];
if (sqlite3_exec(database, [loadSQLi UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
NSLog(#"DB Error. '%s'", sqlite3_errmsg(database));
}
}
if(database) sqlite3_close(database);
}
You can make this faster by doing all inserts in a single transaction instead of doing the inserts in separate transactions as it happens by default in that code.
+ (void) updateDB:(NSString *)data {
NSArray *lineArray = [data componentsSeparatedByString:#"\n"];
if (sqlite3_open([[BIUtility getDBPath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSLog(#"Failed to opendatabase in updateDB");
}
char *errorMsg;
execQuery(#"Begin Transaction");
for(int k = 0; k < [lineArray count]; k++){
NSString *loadSQLi = [lineArray objectAtIndex:k];
execQuery(loadSQLi);
}
execQuery(#"Commit");
if(database) sqlite3_close(database);
}
+ (void) execQuery:(NSString *)query {
char *errorMsg;
if (sqlite3_exec(database, [query UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
NSLog(#"DB Error. '%s'", sqlite3_errmsg(database));
}
}