Problem with SQL statement - iphone

I have the following code:
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID %#", mapID;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the Route Name array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Read the data from the result row
if((char*)sqlite3_column_text(compiledStatement, 0) != NULL)
{
NSString *xCoordinate = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 0)];
NSString *yCoordinate = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
NSLog(#"xCoordinate: %#", xCoordinate);
NSLog(#"yCoordinate: %#", yCoordinate);
CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
placeName:#"Keenan Stadium"
description:#"Tar Heel Football"];
[self.mapView addAnnotation:pin];
[pin release];
}
}
}
else
{
NSLog(#"Error: failed to select details from database with message '%s'.", sqlite3_errmsg(database));
}
I have a few questions:
in my SQL statement how do I include the variable mapID as part of the SQL statement
the line
CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
gives me the following warning "Incompatible types in initialization"
Thanks

You should use a parameterized statement and bind values, like this:
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID = ?";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_text( compiledStatement, 1, [mapID UTF8String], -1, SQLITE_TRANSIENT );
(Note that when binding parameters, you start at 1, but when reading columns from result rows, you start at 0...). I'm assuming your mapID is an NSString since you tried to stick it in your query using %#. If it's something else, you'll have to use a different bind function and obviously skip the UTF8String.

To include MapID,
Change this:
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID %#", mapID;
To this:
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y, MapID from ZWAYPOINT where ZMAP_ID %#", mapID;

You get a warning creating that struct since the values are not NSString * but double.
http://developer.apple.com/iphone/library/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#jumpTo_17

Related

not getting inside while loop when selecting a single row from sqlite

I have a problem with sqlite, when i select a single row from table and then check sqlite3_step(statement) == SQLITE_ROW both values are different and not getting inside while statement.
This is the code:
if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
//NSLog(#"working777.............%d",sqlite3_step(statement));
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(#"working888.............%d",SQLITE_ROW);
NSString *addressField = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSLog(#"............statement...........addressField %#, phoneField %#",addressField,phoneField);
}
sqlite3_finalize(statement);
}
sqlite3_close(db);
}
The proper way to create such a query would be like this:
NSString *querySQL = #"SELECT * FROM Major_Events WHERE temple_id = ?";
Then prepare the statement. I assume query_stmt is the char * value from querySQL.
Once the statement is prepared you then need to bind the value.
sqlite3_bind_int(statement, 1, temp_id); // bind is 1-based
Of course temp_id needs to be an int value and not a string. There are various sqlite3_bind_xxx statements for different data types. Use the appropriate one.
Once all of the query parameters are bound, you can execute the query using sqlite3_step.
The nice thing about this approach over string formats is that strings get properly escape and put in quotes for you. It's much harder to mess up and it makes your queries much safer against SQL injection attacks.
For many records:-
if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK) {
const char *sql = "select * from Place";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
}
sqlite3_finalize(selectstmt);
sqlite3_close(database);
}
}
else
{
sqlite3_close(database);
}
If you want a single record then change while to if
if(sqlite3_step(selectstmt) == SQLITE_ROW) {
rest everything will be same
I hope it helps and if your while loop is not getting executed then it means there is some problem with your query.You need to check that also.

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.

Iinsert/Replace data in SQLite

Why can't I update data to SQLite eventhought it print "yes yes", please help, here my code, it called from viewdidload:
-(void)updateData{
CryptTestAppDelegate *delegate=(CryptTestAppDelegate *)[[UIApplication sharedApplication] delegate];
sqlite3_stmt *stmt=nil;
NSString *dd = #"testing";
const char *sql = "Update ProvPuzz set desc2=?";
sqlite3_prepare_v2(delegate.db, sql, 1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [dd UTF8String], -1, SQLITE_TRANSIENT);
if(sqlite3_step(stmt)){
NSLog(#"yes yes");
}else{
NSLog(#"no no");
}
sqlite3_finalize(stmt);
sqlite3_close(delegate.db);
}
It seems to pass every step, but my data not change at all. Any help please...
How should I call/code to be able to insert/replace these record in SQLite?
NSString *UpdateSql = [NSString stringWithFormat:#"Update ProvPuzz set desc2 = something"];
const char *sqlStatement = [UpdateSql UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(filesDB, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
if(sqlite3_step(compiledStatement) == SQLITE_DONE)
{
sqlite3_reset(compiledStatement);
}
}
// Release the compiled statement from memory
sqlite3_reset(compiledStatement);
sqlite3_finalize(compiledStatement);
sqlite3_close(filesDB);
and also check you db connection opened properly or not

iOS sqlite will return NULL when using SELECT statement

I'm very confused why the SELECT statement doesn't work correctly. It doesn't give me any errors, just returns null. I know it is writing the string correctly and the right string is there, it's just not reading it correctly. Everything as far as I know is correct because I use the same SQLstmt "method" for many other methods/functions similar to this. This one just doesn't make sense on why it shouldn't work.
- (NSString *)returnNote {
selStmt=nil;
NSLog(#"Reading note");
NSString *SQLstmt = [NSString stringWithFormat:#"SELECT 'Notes' FROM '%#' WHERE Exercises = '%#';", currentRoutine, currentExercise];
// Build select statements
const char *sql = [SQLstmt UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK) {
selStmt = nil;
}
// Building select statement failed
if (!selStmt) {
NSAssert1(0, #"Can't build SQL to read Exercises [%s]", sqlite3_errmsg(database));
}
NSString *note = [NSString stringWithFormat:#"%s", sqlite3_column_text(selStmt, 0)];
sqlite3_reset(selStmt); // reset (unbind) statement
return note;
}
You're not calling sqlite3_step. The statement is never executed.
NSString *querySQLS1 = [NSString stringWithFormat: #"SELECT Notes FROM \"%#\" where Exercises=\"%#\"", currentRoutine, currentExercise];
sqlite3_stmt *statements;
const char *query_stmts1 = [querySQLS1 UTF8String];
if(sqlite3_prepare_v2(UsersDB, query_stmts1, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(#"in prepare");
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(#"Query executed");
}
else {
NSLog(#"in else");
}
sqlite3_finalize(statement);
}

table value is not updated using update statement

i need to update a column value into table.
for that my code is,
NSString *sql_str = [NSString stringWithFormat:#"update %# Set Quantiry = %# Where ItemName = %#", tableName,quantity,itemname];
const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(#"query %s",sqlStatement);
sqlite3_stmt *compiledStatement;
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
sqlite3_close(database);
displayed query in console is update allcategories Set Quantiry = 11 Where ItemName = Bananas
but the value in table is not updated.
what the wrong,can any one please help me.
Thank u in advance.
you are missing one line after sqlite3_prepare_v2. you have to use sqlite3_step() after this. use following:
NSString *sql_str = [NSString stringWithFormat:#"update %# Set Quantiry = %# Where ItemName = %#", tableName,quantity,itemname];
const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(#"query %s",sqlStatement);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement);
}
sqlite3_close(database);
hope helps.. :)