5

I am having problem to INSERT some values into database. Database name is users, and table is heroes. I am doing some mmorpg game developing for purpose of learning.

This is mysql code that works

INSERT INTO heroes (HeroID,Strenght, Dexterity, Vitality, Wisdom, Inteligence, Luck, Name, Level, XP) VALUES (NULL,  17, 13, 17, 15, 9, 8, 'works', 4, 3750);

But when I try that from c++ via mysql++ I get error.

Code:

#include <mysql++/mysql++.h>

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
// Connect to the database.
mysqlpp::Connection conn(false);

if (conn.connect("users", "localhost",
        "root", "toor")) 
{

    mysqlpp::Query query = conn.query();
   query << "INSERT INTO heroes" <<
    "VALUES (NULL, 17, 13, 17, 15, 9, 8, doSomething,3, 3260);";

    query.execute();
    if (mysqlpp::StoreQueryResult res = query.store()) 
    {
     // nothing to do here
    }

    else 
    {
        cerr << "Failed to get item list: " << query.error() << endl;
        return 2;
    }

    return 0;
}

   else 
    {
    cerr << "DB connection failed: " << conn.error() << endl;
    return 1;
    }
}

Error I get is: Failed to get item list; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL, 17, 13, 17, 15, 9, 8, doSOmething,3, 3260)' at line 1

p.s. I am sending NULL becouse that field is auto-increment (using for ID) I tried almost all possible alternatives like adding '', separataning and whatnot. Can somebody help me with this line of code, since I can't find ang good tutorial that helps on this matter.

Thanks in advance

Lain
  • 2,166
  • 4
  • 23
  • 47
user2597689
  • 75
  • 1
  • 8

4 Answers4

5

Have you tried to add an space in your string?

query << "INSERT INTO heroes " <<
    "VALUES (NULL, 17, 13, 17, 15, 9, 8, 'doSomething', 3, 3260);";

Also, as pointed out by hmjd, doSomething needs to be quoted, like 'doSomething'.

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
5

The string doSomething needs quoted:

query << "INSERT INTO heroes "
      << "VALUES (NULL, 17, 13, 17, 15, 9, 8, 'doSomething', 3, 3260);";

and as pointed out by Streppel a space is required between heroes and VALUES.

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
2

If your field is auto-increment, just don't mention it in your query, and that's it !

But then you would need to name all the fields in your insert query, which is actually a best-practice anyway !

query << "INSERT INTO heroes (fieldName1,fieldName2,...) " <<
"VALUES (17, 13, 17, 15, 9, 8, doSomething,3, 3260);";

Where fieldname1 is your first field right after the id field (or any other field actually, you can use whatever order you wish).

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
0

you can create a header and paste this in. i will use table1.h

#include <mysql++.h>
#include <ssqls.h>
#include <vector>       

    //you may get some weird underlines but don't panic, have some vitality.
    sql_create_10(heroes, 1, 10,
            mysqlpp::sql_int, HeroId, mysqlpp::sql_int,
            Strenght, mysqlpp::sql_int, Dexterity, mysqlpp::sql_int, Vitality,
            mysqlpp::sql_int, Wisdom, mysqlpp::sql_int, Inteligence,
            mysqlpp::sql_int, Luck, mysqlpp::sql_varchar, Name, mysqlpp::sql_int,
            Level, mysqlpp::sql_bigint, Xp)
            //if bigint doesn't work for you u can use just int.

then in the main method paste this in

    //connect to database
mysqlpp::Connection conn(false);
try{
    conn.connect("users", "localhost", "j0y57/Qxx", "rootsman");

    heroes row(0, 17, 13, 17, 15, 9, 8, "doSomething", 3, 3260 );
    mysqlpp::Query query = conn.query();
    query.insert(row);
    query.execute();

} catch (const mysqlpp::BadQuery& bq){
    cerr << "query error: " << bq.what() << endl;
    return -1;
}