开发者

Sqlite UPDATE question

开发者 https://www.devze.com 2023-03-06 18:43 出处:网络
Hi i\'m writing an iphone application that requires sqlite and one update query. So simply,why this works:

Hi i'm writing an iphone application that requires sqlite and one update query. So simply,why this works:

NSString *query=[NSString stringWithFormat:@"%@%@%@%@%@",
    @"UPDATE ZTABELLA SET ZTYPE =",@"1",
    @" WHERE ZNAME='",@"stringaoggetto",@"';"];

sqlite3_exec(database2, [query UTF8String], NULL, NULL, NULL);  

and why this doesn't works:

NSString *query=[NSString stringWithFormat:@"%@%@%@%@%@",
    开发者_如何学JAVA@"UPDATE ZTABELLA SET ZTYPE =",@"1",
    @" WHERE ZNAME='",oggetto,@"';"];

sqlite3_exec(database2, [query UTF8String], NULL, NULL, NULL);  

The difference between this two examples is only @"stringaoggetto" in the first and oggetto in the second. In the first i have inserted value directly with a string,in the second example oggetto is a nsstring object that have the right value inside (i have printed a nslog). Why this difference?

The log for query is the same for each example:

NSLog(@" %@",query);
//this log print: UPDATE ZTABELLA SET ZTYPE =1 WHERE ZNAME='stringaoggetto';

The log for the nsstring object "oggetto" is:

NSLog(@"%@",oggetto);
//this log print   stringaoggetto

I have also tried another code to check the error,but the result is the same:

if (sqlite3_open([dbPath2 UTF8String], &database2) == SQLITE_OK) {


      const char *update = "UPDATE ZTABELLA SET ZTYPE =1 WHERE ZNAME=?";

      sqlite3_stmt *updatestmt;
if(sqlite3_prepare_v2(database2, update, -1, &updatestmt, NULL) == SQLITE_OK) {

sqlite3_bind_text(updatestmt, 1, [oggetto UTF8String] , -1, SQLITE_TRANSIENT);


if(SQLITE_DONE != sqlite3_step(updatestmt)){

    NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database2));

                }
sqlite3_reset(updatestmt);                  

            }
            else {
                NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database2));

            }
            }

As the first direct query (with sqlite3_exc) in this case the sqlite3_bind_text doesn't works after where clause. With the same query if i try for testing purpose to set column ZTYPE insted of ZNAME (however any other column can't be set "Where") it works. Why?

This is the init of the nsstring oggetto,is the result of select query:

NSMutableString *oggetto;
oggetto=[NSString stringWithCString:(char *)sqlite3_column_text(selectstmt, 0)];

this string retain correctly the value (the value is "stringaoggetto"),i have tried with a nslog.


  1. Do this instead; [query cStringUsingEncoding:NSUTF8StringEncoding] I think this is really the key you need to solve this issue. Ensure that the string is being retained; Both these should work.

    NSLog(@"%s",[query cStringUsingEncoding:NSUTF8StringEncoding]);

    NSLog(@"%s",[query UTF8String]);

  2. This may have some helpful hints also; http://www.how2s.org/index.php/How_to_get_started_with_SQLite_on_the_iPhone Simplify the design.

  3. Also of note, you use NULL,NULL, NULL. Get the error data from the sqlite3_exec statement. http://www.sqlite.org/c3ref/exec.html has reference material to get you started on that.

  4. you listed multiple issues. Separate the others out to other questions. Inserts are different from Updates. Focus this question on the Update.

  5. If none of this helped, how did NSString *oggetto get set, where, same file, diff file, is it a function argument etc?

0

精彩评论

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