0

Can anyone help me with SQLite query in my iOS app? I want to make query, which will depend on NSInteger varialbe at WHERE clause, but i dont know how to add variable into query.

I tried something like this:

MySqlite *sqlConnect = [[MySqlite alloc] init];  
sqlite3_stmt *statement = [sqlConnect makeSQL:"select * from table where id = %i", myIntegerVariable - 1];

But of course it doesn't work.

Thanks.

EDIT

MySqlite.h

#import <Foundation/Foundation.h>  
#import <sqlite3.h>  

@interface MySqlite : NSObject {  

}  
- (sqlite3_stmt *)makeSQL:(char *)sql;  

@end

MySqlite.m

#import "MySqlite.h"  

@implementation MySqlite  

NSString *dbFileName = @"data.sqlite";  

- (void)createEditableCopyOfDatabaseIfNeeded {  

NSFileManager *fileManager = [NSFileManager defaultManager];  
NSError *error;  

NSString *writableDBPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]  
                            stringByAppendingPathComponent:dbFileName];  

if ([fileManager fileExistsAtPath:writableDBPath]){  
    return;  
}  

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFileName];  

if (! [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error] ) {  
    NSLog(@"Fail edit db");  
}  
}  

- (sqlite3 *) getDBConnection{  

[self createEditableCopyOfDatabaseIfNeeded];  

sqlite3 *DBConnection;  

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]   
                  stringByAppendingPathComponent:dbFileName];  

if( sqlite3_open([path UTF8String], &DBConnection) != SQLITE_OK) {  
    NSLog(@"Fail open db");  
    return FALSE;  
}  

return DBConnection;  
}  

- (sqlite3_stmt *)makeSQL:(char *)sql{  
NSLog(@"Executing query: %s", sql);  

sqlite3_stmt *statement = nil;  

sqlite3 *db = [self getDBConnection];  

if ( db ) {  
    if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL) != SQLITE_OK) {  
        NSLog(@"Error at query: %s", sqlite3_errmsg(db));  
    }  
}  
else {  
    NSLog(@"Error connect to db");  
}  

return statement;  
}  


@end  
3
  • Tried to put it into [NSString stringWithFormat:@"select * from table where id = %i", myIntegerVariable - 1]? Commented Sep 30, 2011 at 15:25
  • It is my class for connecting to the database and making queries. Commented Sep 30, 2011 at 15:28
  • I edited my question - added MySqlite class files. Commented Sep 30, 2011 at 15:40

1 Answer 1

1

If I understood you correctly, you want to inject an integer variable into the SQL Query. If so, something like below should do the job:

MySqlite *sqlConnect = [[MySqlite alloc] init];  
sqlite3_stmt *statement = [sqlConnect makeSQL:[[NSString stringWithFormat:@"select * from table where id = %d", myIntegerVariable - 1] UTF8String]];

%d is the modifier for an Integer. That is assuming that the makeSQL selector takes in an NSString SQL String

Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't work. Make sql expect (char *). This is the method: -(sqlite3_stmt *)makeSQL:(char *)sql.
I have modified my code sample above, the UTF8String addition will give you a char string
Show me this: Passing argument 1 of 'makeSQL:' discards qualifiers from pointer target type. Semantic Issue: Sending 'const char *' to parameter of type 'char *' discards qualifiers
I have changed my -(sqlite3_stmt *)makeSQL:(char *)sql method to -(sqlite3_stmt *)makeSQL:(const char *)sql and it works fine. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.