0

I am trying to do a FMDB insert with this code:

dbCmd = @"INSERT INTO SiteData (SITE_ID, INITIAL_STA, ELEV, SITE_DESC, LOOP_COUNT, DATE) VALUES(?,?,?,?,?,?)",
txtSiteID.text, 
[txtSTA.text floatValue],
[txtElev.text floatValue],
txtSiteDesc.text, 
[NSNumber numberWithInt:0], 
currentDate;

Each of the 3 text fields that are not modified by the "float value" method call are getting an error saying: "Expression result unused" which is causing the parameter count to be off, which in turn is causing FMDB to crash.

I don't see any problem with the code as written... can someone enlighten me as to what I need to change to get this to work?

4

3 に答える 3

4

I think you're extremely confused. The code

dbCmd = @"INSERT INTO SiteData (SITE_ID, INITIAL_STA, ELEV, SITE_DESC, LOOP_COUNT, DATE) VALUES(?,?,?,?,?,?)",
txtSiteID.text, 
[txtSTA.text floatValue],
[txtElev.text floatValue],
txtSiteDesc.text, 
[NSNumber numberWithInt:0], 
currentDate;

isn't creating a statement with placeholders like you think. It's running a whole bunch of expressions separated by the comma operator. The variable dbCmd ends up with the value of currentDate, and the values of all other expressions are discarded. The compiler is warning you that you're writing expressions that are being unused.

I think what you need to do is something like

[db executeUpdate:@"INSERT INTO SiteData (SITE_ID, INITIAL_STA, ELEV, SITE_DESC, LOOP_COUNT, DATE) VALUES(?,?,?,?,?,?)",
    txtSiteID.text, 
    [txtSTA.text floatValue],
    [txtElev.text floatValue],
    txtSiteDesc.text, 
    [NSNumber numberWithInt:0], 
    currentDate];
于 2012-04-19T00:22:18.487 に答える
0

What type of object is dbCmd? If it's a string, you need to use [NSString stringWithFormat:] and the proper string percent encodings. You can't just tack on parameters on an NSString constant (@"").

Edit: See Kevin's answer.

于 2012-04-19T00:21:21.230 に答える
0

Here's the answer:

dbCmd = [NSString stringWithFormat: @"INSERT INTO SiteData (SITE_ID, INITIAL_STA, ELEV, SITE_DESC, LOOP_COUNT, DATE) VALUES(%@, %f, %f, %@, %d, %@)",
    txtSiteID.text,
    [txtSTA.text floatValue],
    [txtElev.text floatValue],
    txtSiteDesc.text, 
    [NSNumber numberWithInt:0], 
    currentDate];

Works like a champ! Thanks everybody for your input. I appreciate it!

于 2012-04-19T16:41:52.573 に答える