开发者

How to build an iOS SQL statment

开发者 https://www.devze.com 2023-03-11 14:15 出处:网络
I have a database called catalog, it has two three columns one is just the key field, the other is a integer called \"code\" and the other is a NSString called \"make\".

I have a database called catalog, it has two three columns one is just the key field, the other is a integer called "code" and the other is a NSString called "make".

My app allows the user to type in a code to search for any makes that have that code, so there is a uitextfield where they can type the code then the user presses a uitextfield cell that loads a new uitableview onto the navigation stack I want to display any makes that have the code that was entered in the previous view here.. I have most of the code done as I am following a great guide "professional iphone and ipad database application programing" however in the step I am doing they just show all the data not a restriction of data, I would like to know how to do this.

here is my sql request string

const char *sql = "SELECT catalog.make From catalog where catalog.code =???

I would like to know what should come next, 开发者_JAVA百科how to I execute this statement with the code the user has entered in the uitextfield?

any help would be greatly appreciated.


I'd just make the SQL statement into an NSString, it's easier.

Assuming the UITextField you're collecting data from is an IBOutlet called myText:

NSString *sql = [[NSString alloc] initWithFormat:@"SELECT catalog.make FROM catalog WHERE catalog.code = %@", myText.text];
const char *cString = [sql cStringUsingEncoding:NSASCIIStringEncoding];
[sql release];


if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

    } 

    sqlite3_bind_text(statement, 1, [str UTF8String], -1, SQLITE_TRANSIENT);

where str is the text you entered. You can pass it as parameter in the method you are making this query. And finally

sqlite3_finalize(statement);


If you are building SQL statements for iOS, you may want to consider using an Objective-C library for iOS such as https://github.com/ziminji/objective-c-sql-query-builder. It will help you sanitize your data since you are dealing with user data. You don't want to expose your iOS application to SQL insertion attacks. Using the SQLite query builders in this library will help prevent such attacks and will build the SQL statements for you. It also provides a class to manage your database connections. If you prefer, you can use the ORM to help simplify the building of queries. Give it a try.

0

精彩评论

暂无评论...
验证码 换一张
取 消