Subscribe ( )

iPhone Programming Tutorials

 

Forum

You must be logged in to post Login Register

Search 

Using a variable in const char *sqlStatement

User Post

1:20 pm
October 7, 2008


bobcubsfan

iCoder

Los Angeles

posts 48

1

In PHP, I have used the following:

           $query=”SELECT * FROM `zipcodes` WHERE zip =  '$czip'”;

The table is zipcodes, zip is a field in the table. czip is a variable from input. So, I can retrieve all the instances of czip from zipcodes by using the construct '$czip'.

How can this be done in XCode in a const char statement?

12:14 pm
October 10, 2008


Neil

iCoder

North Wales, UK

posts 6

2

5:13 pm
October 11, 2008


bobcubsfan

iCoder

Los Angeles

posts 48

3

Thanks, but I needed to use a string as the ? See my Tips and Tricks Post

5:10 pm
October 21, 2008


cmcintosh2

iCoder

posts 9

4

you would need a couple parts, the first format the sql for the objective-C:

$query=”SELECT * FROM `zipcodes` WHERE zip =  '$czip'”; to:

sql = “SELECT * FROM zipcodes WHERE zip = ?”;

you then need to setup the statement so you can use it to set through

sqlite3_bind_int(sql, 1, czip);

Unfortunately I am not sure how to handle multiple ?'s or how to run them after the initial run.  Maybe this will be enough to get you in the right direction though.


<a href=”http://www.chrismcintoshdesigns.com”>Chris McIntosh</a>


1:03 pm
October 23, 2008


jab2109

Noob

posts 5

5

Use the sqlite3_bind_text() function instead of sqlite3_bind_int() if you want to replace a placeholder ? with a string.

sqlite3_stmt *insert = nil;

const char *sql = “SELECT * FROM zipcodes WHERE zip='?'”;

sqlite3_prepare_v2(database, sql, -1, $insert, NULL);

sqlite3_bind_text(insert, 2, [czip UTF8String], -1, SQLITE_TRANSIENT);

assuming czip is an instance variable of self. You could also use an array of strings, or input as czip.

2:55 pm
October 23, 2008


bobcubsfan

iCoder

Los Angeles

posts 48

6

Did you read my post?

5:51 pm
October 23, 2008


jab2109

Noob

posts 5

7

Yeah, I don't really see the problem.. Just build a sql statement like the one in youre post and loop through the results.


About the iCodeBlog forum

Currently Online:

5 Guests

Maximum Online: 19

Forums:

Groups: 2

Forums: 6

Topics: 156

Posts: 503

Members:

There are 268 members

There are 1 guests


Brandon has made 81 posts

Top Posters:

bobcubsfan - 48

crazyiez - 30

Uhu - 17

VertigoSol - 17

Nick - 15

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)  

Share and Enjoy: