sqlite3_prepare_v2 problem - iphone

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.

Related

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

Memory optimization for sqlite requests - help needed

I'm using two methods to r/w sqlite table:
+ (NSString *) getWeatherData:(int)rowID:(int)columnID {
NSString *savedWeatherData = [[NSString alloc] init];
if (sqlite3_open([[DBController getDBPath] UTF8String], &database) == SQLITE_OK) {
const char *sql = "select * from TABLE where id=?";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(selectstmt, 1, rowID);
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Weather *weatherObj = [[Weather alloc] initWithPrimaryKey:primaryKey];
weatherObj.weatherData = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, columnID)];
savedWeatherData = weatherObj.weatherData;
[weatherObj release];
}
}
}
return [savedWeatherData autorelease];
}
And to save some data:
+ (void) saveDataToDataBase:(NSString *)columnToUpdate:(int)rowID:(NSString *)value {
if (sqlite3_open([[DBController getDBPath] UTF8String], &database) == SQLITE_OK) {
updateStmt = nil;
NSString *sqlString = [[#"update TABLE set " stringByAppendingString:columnToUpdate] stringByAppendingString:#"=? where id=?"];
const char *sql = [sqlString UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(database));
} else { // select statement ok
sqlite3_bind_text(updateStmt, 1, [value UTF8String], -1, SQLITE_TRANSIENT); // replace first ? with value
sqlite3_bind_int(updateStmt, 2, rowID); // replace second ? with rowID
if(SQLITE_DONE != sqlite3_step(updateStmt)) {
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
} else {
// NSLog(#"Update completed !!!");
}
sqlite3_reset(updateStmt);
}
sqlite3_finalize(updateStmt);
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
With the Instruments, I see pretty big memory consumption for sqlite.
Info: during app startup, cca 100 different kind of data is stored in DB - for each data, this saveDataToDataBase method is called. (In case of no internet connection - getWeatherData would be used - and again cca 100 different kind of data would be read)
Please, can you instruct me - is it possible to optimize memory consumption.
Thanks a lot!
Kind regards!

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 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.