Data inserted not retained unless I restart the application - iphone

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);

Related

Delete all the records in table in iphone sdk with sqlite3

EDITED:- This is what i had implements:-
- (void) Delete_LoginData {
sqlite3 *database;
if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "DELETE FROM Login";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, sql,-1, &statement, NULL) == SQLITE_OK)
{
sqlite3_reset(statement);
}
}
}
I want to delete all the rows from the database in one click.I had implemented the below methods:-
- (void) Delete_LoginData {
sqlite3 *database;
if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
const char *sql = "DELETE * FROM Login";
sqlite3_stmt *statement;
statement = [self PrepareStatement:sql];
// int a1 = sqlite3_bind_int(statement, 1, 1);
sqlite3_step(statement);
sqlite3_reset(statement);
}
}
-(sqlite3_stmt*) PrepareStatement:(const char *)sql{
// Setup the database object
sqlite3 *database;
// Init the animals Array
// Open the database from the users filessytem
if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = sql;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement,-1, &compiledStatement, NULL) == SQLITE_OK) {
//NSLog(#"COMPILED STATEMENT: %#",compiledStatement);
return compiledStatement;
}
}
return nil;
}
But it not working.
I am getting 0X0 for this statement = [self PrepareStatement:sql];
in delete_logindata method.
How to solve this.
Is there any solution regarding this?
write const char *sql = "DELETE FROM Login"; instead of const char *sql = "DELETE * FROM Login";
change your const char*sql
const char *sql = "DELETE * FROM Login";
change above with this
const char *sql = "DELETE FROM Login";
Please change your method like:
- (void) Delete_LoginData
{
sqlite3 *database;
if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "DELETE FROM Login";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, sql,-1, &statement, NULL) == SQLITE_OK)
{
sqlite3_step(statement);
}
}
}
In your code you are opening the database twice. In the second method you are opening a database that was already opened !
I don't know it is the actual issue or not, but I'll suggest don't do this.
I dont know what will be correct answer for it.
But I can suggest u this link http://klanguedoc.hubpages.com/hub/IOS-5-SDK-Database-Insert-Update-Delete-with-SQLite-and-Objective-C-C-How-To may be it will help u.

how can i update data in table using sqlite in obj c

hii every one
can any one suggest me one good tutorial which shows update table in sqlite database
Open your database with:
sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK;
To update a row:
#synchronized(sqlLock]) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [[NSString stringWithFormat:#"UPDATE TABLE Set Text = ? where ID = '%#'",objectID] UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [text UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_reset(compiledStatement);
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}

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 ;-)

sql won't add new entries. :(

My function won't add any entry to my existing sql database. Any ideas?
sqlite3 *database;
sqlite3_stmt *compiledStatement;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *tmpSQLStatement = [NSString stringWithFormat:#"INSERT INTO data (test) VALUES ('teststring');"];
const char *sql = [tmpSQLStatement UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
No ERRORMESSAGE Is called. But unfortunately nothing is added.
After sqlite3_prepare_v2, you need to actually execute the statement by calling sqlite3_step. Values should be inserted by then.