0

I'd like to update a SQLite DB within an iPhone app via a JSON call and I'm wondering if i'm approaching it the right way and would be keen for your feedback/input.

I was thinking of using a JSON encoded string containing x-number of dictionaries in an array.

Each dictionary would have a few properties that correspond to the CRUD function i'd like to execute within the app, as well as the data I would like updated/deleted/added - i'm thinking it might look something like this...

[{ "crudFunction": "INSERT", "name": "John", "city": "city1", "phone": 11111},
{ "crudFunction": "UPDATE", "name": "Andrew", "city": "city2", "phone": 22222},
{ "crudFunction": "DELETE", "name": "Matt", "city": "city3", "phone": 33333},
{ "crudFunction": "UPDATE", "name": "Mike", "city": "city4", "phone": 44444} ]

For the sake of keeping things simple, in the above example i'll be updating the same table.

Not having a great knowledge of Objective C - I was wondering if I could construct my SQL statement using the variables I grabbed from the above array/dictionary? Something like:

NSString *insertQuery = @"INSERT INTO my_table (name, city, phone){[name],[city],[phone]}";

or in the case of an update...

NSString *insertQuery = @"UPDATE my_table SET city=[city], phone=[phone] WHERE name=Mike";

I'm thinking that in scenario's where I would have multiple updates to make, I could create a CRUD Object (of sorts) containing (transaction related) CRUD records/variables e.g. a DELETE Object containing all the records I would like to delete from my DB, or an UPDATE Object containing all the records/values I wanted to update.

Does this all make sense? Am I going about this the right way? Or should I just put down what i'm smoking and get back to my day job?

Thanks in advance Vortex

4

1 回答 1

0

您可以sqlite3_prepare_v2用来准备稍后执行的语句

例如

//Set ? for variables
const char *sql = "INSERT INTO my_table (name, city, phone){?,?,?}";
sqlite3_stmt *statement;
int returnValue = sqlite3_prepare_v2(database, sql, -1, &statment, NULL);

sqlite3_bind_text如果它们是文本,则用于设置值

sqlite3_bind_text(statement, 1, yourName, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, yourCity, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, yourPhone, -1, SQLITE_TRANSIENT);

然后使用sqlite3_step执行,

您可以保存您的statementsql 以在其他时间执行它

于 2012-07-03T11:55:35.260 回答