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?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

3 Answers3

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];
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • I took your code, modified the "db" to "fmdb" and ran it... not getting that particular error anymore, but it's dying inside FMDB... seems that it's not getting the args... I'm thinking about dropping FMDB and using just straight SQLite... XD XD (Jack, you'll recognize that! ) – SpokaneDude Apr 19 '12 at 01:07
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.

Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
  • Really bad idea. This is how SQL injection vulnerabilities are created. – Lily Ballard Apr 19 '12 at 00:22
  • Yeah realized that right after I posted it. I'm not familiar with FMDB's methods so I just gave a generic "this is why the compiler is flipping out" but I've upvoted your answer because it's much better. – Jack Lawrence Apr 19 '12 at 00:26
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!

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120