I got a little problem I am making a little admin tool for my server that you can edit player stats and things with it.
Problem is if I like make string Test and assign value "Hi" to it.
How can I use the string inside my IMPORT INTO statement.
Here's code:

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"
	
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
	
using namespace std;
	
int main(void)
{
cout << endl;
	
try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;
	
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "*******");
	
  stmt = con->createStatement();
  stmt->execute("USE breakpoint");
  string Test = "Hi";
  stmt->execute("INSERT INTO characters(Name, CID, Age, Char_Strength, Char_Speed, Char_Endurance, Char_Aim, Char_RunningEndurance, Blackmarket, Business, Electronics, Food, Misc, Money, Inventory_room, Model, Title) VALUES ('Test Name', '45264', 20,'20','20','20','20','20','1','1','1','1','1',599,500,'lol','Lol')");

  delete res;
  delete stmt;
  delete con;
	
} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
	
cout << endl;
	
return EXIT_SUCCESS;
}

You see string test how would I like put it to replace "Test name" part, It doesnt work like this:

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"
	
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
	
using namespace std;
	
int main(void)
{
cout << endl;
	
try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;
	
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "*******");
	
  stmt = con->createStatement();
  stmt->execute("USE breakpoint");
  string Test= "Hi";
  stmt->execute("INSERT INTO characters(Name, CID, Age, Char_Strength, Char_Speed, Char_Endurance, Char_Aim, Char_RunningEndurance, Blackmarket, Business, Electronics, Food, Misc, Money, Inventory_room, Model, Title) VALUES (Test, '45264', 20,'20','20','20','20','20','1','1','1','1','1',599,500,'lol','Lol')");

  delete res;
  delete stmt;
  delete con;
	
} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
	
cout << endl;
	
return EXIT_SUCCESS;
}

Sorry for bad explanation but try to understand it please

This whole thing is a literal string....

"INSERT INTO characters(Name, CID, Age, Char_Strength, Char_Speed, Char_Endurance, Char_Aim, Char_RunningEndurance, Blackmarket, Business, Electronics, Food, Misc, Money, Inventory_room, Model, Title) VALUES ('Test Name', '45264', 20,'20','20','20','20','20','1','1','1','1','1',599,500,'lol','Lol')"

It is a SQL Insert statement. If you want to get the value held in the string variable 'Test' into the table, you have to get the characters of the string variable into the above character string where 'Test Name' is. Perhaps parsing it or concatenating it together. In database programming such as this, one typically builds these SQL statements from the parts of the statements themselves such as "INSERT INTO characters(..." and from the various variables one has in memory in one's program.

I have no idea how to that

What you'll need to do is create something like a struct to hold your character data...
e.g.

// create a struct to hold your data
struct CharacterData
{
	string m_Name;
	int m_CID;
	int m_Age;
	int m_Char_Strength;
	int m_Char_Speed;
	int m_Char_Endurance;
	int m_Char_Aim;
	int m_Char_RunningEndurance;
	int m_Blackmarket;
	int m_Electionics;
	int m_Food;
	int m_misc;
	int m_Money;
	int m_Inventory_room;
	string m_Model;
	string m_Title;
	
};

Then somewhere else in your code (before you try to build your query) you'll need to create an instance of the structure and populate it with some data....

// Create an instance of your struct and populate it
CharacterData cdata;
cdata.m_Name="MyName";
cdata.m_CID=23432;
cdata.m_Age=42;
cdata.m_Char_Strength=100;
cdata.m_Char_Speed=25;
cdata.m_CharEndurance=50;
cdata.m_CharAim=38;
cdata.m_CharRunningEndurance=30;
cdata.m_Blackmarket=1;
cdata.m_Electronics=1;
cdata.m_Food=1;
cdata.m_Misc=1;
cdata.m_Money=599;
cdata.m_Inventory_Room=500;
cdata.m_Model="rofl";
cdata.m_Title="lol";

NOTE: For the above step, you might want to set up a loop and get the user to enter the character details into the structure using cin and cout or something. But for the sake of brevity, I've just gone with the simplest option of creating an instance of the structure and populating it with some hard coded data!

Next you'll need to create a function to build your query string:

string BuildCharacterQueryString_Insert(const CharacterData &data)
{
	// This will be our buffer to hold the query string
	ostringstream oss; // <--- you'll need to include sstream.h for this one!
	
	// Build first part of query
	oss << "INSERT INTO characters(Name, CID, Age, Char_Strength, Char_Speed, ";
	oss << "Char_Endurance, Char_Aim, Char_RunningEndurance, Blackmarket, ";
	oss << "Business, Electronics, Food, Misc, Money, Inventory_room, Model,";
	oss << " Title) VALUES (";
	
	// next insert our values into the string
	oss << "'" << data.m_Name << "',";
	oss << "'" << data.m_CID << "',";
	oss << data.m_Age << ",";
	oss << "'" << data.m_Char_Speed << "',"; 
	oss << "'" << data.m_CharEndurance << "',";
	oss << "'" << data.m_Char_Aim << "',";
	oss << "'" << data.m_CharRunningEndurance << "',";
	oss << "'" << data.m_Blackmarket << "',";
	oss << "'" << data.m_Business << "',";
	oss << "'" << data.m_Electronics << "',";
	oss << "'" << data.m_Food << "',";
	oss << "'" << data.m_Misc << "',";
	oss << data.m_Money << ",";
	oss << data.m_Inventory_Room << ",";
	oss << "'" << data.m_Model << "',";
	oss << "'" << data.m_Title << "')";  // don't forget to close the query string with the )

	// return the query string we just built...
	return oss.str();
}

Finally, you are ready to build your query string and execute your query.
So in the part of your current code where you're calling execute and passing that huge string literal; the final piece of the puzzle is to do the following:

// call the function we just created, passing our data (cdata) to create our query string
string querystring = BuildCharacterQueryString_Insert(cdata);

// now execute the query, passing the string we just built
stmt->execute(querystring);

And that's about it.

NOTE: I haven't actually compiled or tested the code, so apologies if there are any syntax errors or any other compiler errors in there. But the principle is pretty much what you are looking for.

So to recap, you need to:
1. Create a structure (or a class!) to hold your character data in.
2. Create an instance of your structure (or class) with some data in it.
3. Create a function which takes your structure as a parameter and creates your query string for you.
4. Call your function, passing in some data to build the query string.
5. Execute your query string.

Hope this is of some help!
Cheers for now,
Jas.

This article has been dead for over six months. Start a new discussion instead.