views:

235

answers:

1

I have not done much database programming at all. I am working from some example code for using MySQL Connector/C++.

When I run the following code I get a crash on the last line in some std::string code - but it ONLY crashes when the table is not empty. If the table is empty, it inserts the row and works fine. If the table is non empty it crashes. I am pretty confused. Is there something I am doing wrong with the primary key or the other values? (the column names have been changed here, but otherwise the code is verbatim)

When I look at the variable in the std::string template code (what little I can see) I don't see any familiar values of data that I was attempting to insert - so that was no help at all. I see something like "HY000" as a string value, but I am not certain where that is coming from.

Initially i thought it might be the date string, but the code works fine with an empty table and then crashes when non-empty. That indicates the date string works fine.

prep_stmt = con->prepareStatement("INSERT INTO 
   sometable(val1, val2, val3, Date, val5, val6, val7) 
   VALUES (?, ?, ?, ?, ?, ?, ?)");

     /*
      `idLicenses` INT NOT NULL ,
      `val1` VARCHAR(45) NOT NULL ,
      `val2` INT NOT NULL ,
      `val3` INT ZEROFILL NULL ,
      `Date` DATETIME NOT NULL ,
      `val5` VARCHAR(45) NOT NULL ,
      `val6` VARCHAR(45) NOT NULL ,
      `val7` VARCHAR(45) NOT NULL ,
     */

        //  val1, val5, val6 and val7 are std::strings, val2 and val3 are ints.
     prep_stmt->setString(1, val1);
     prep_stmt->setInt(2, val2);
     prep_stmt->setInt(3, 0);
     prep_stmt->setDateTime(4, /* I created some date string here*/);
     prep_stmt->setString(5, val5);
     prep_stmt->setString(6, val6);
     prep_stmt->setString(7, val7);
     prep_stmt->execute();

This is on a MS platform using Visual Studio 2008.

The database is up and running and I can use other database query tools to see the tables, etc.

EDIT:

I see looking at the mysql exception that I get:

"Using unsupported buffer type: 4191960 (parameter: 3)" errno 2036

EDIT:

I am not sure if the accepted answer was exactly the right answer, but it helped me get to a solution. Basically I removed all non-null attributes, took out zero fill and set the primary key to auto-increment. Now it works fine

+1  A: 

I suspect it's either your zero-filled INT or the DATETIME column (not sure which is column 3).

Try creating your table with something like:

`val3` INT(10) ZEROFILL ,

where 10 is the number of positions to zero-fill. I'm also not sure the NULL is necessary, so I excluded it above.

Also, can you post the date string you created if the above doesn't work?

Dolph
Thanks for the suggestion - I will update. The curious part is why it works when the table is empty, but NOT when the table has a row in it...
Tim
I'll try dropping the table and creating it differently.
Tim
I am not sure if this was exactly the right answer, but it helped me get to a solution. Basically I removed all non-null attributes, took out zero fill and set the primary key to auto-increment. Now it works fine
Tim
Ah, I didn't catch that the PK wasn't set to auto. I don't know why it WOULD work the first time (nor what that error means), but you were definitely violating the NOT NULL on your PK field by not setting it manually. Glad I could help, if I did :P
Dolph
I'm guessing that it was also zero fill so it just made it zero for the first one. (When there was one entry the id was 0)
Tim