user2884789 user2884789 - 1 month ago 17
MySQL Question

Ignore space without deleting space in a string while inserting into SQL database

I am reading data from a text file into a SQL database using C++ and embedded SQL.
Here is the text file "employee.txt":

1000 Smith Joe 35000.00


Where empId=100, empName= "Smith Joe" and empSal = 35000.00.

The statements I've tried so far do not work properly with the space between the first and last name in the employee.txt file. It is reading the space between "Smith" and "Joe" to mean that Smith and Joe are two separate items rather than a string with a space. Here is my code:

inFile >> empId;

while (!inFile.eof())
{
inFile >> empName;
inFile >> empSal;

string sql;
sql = "insert into Employee values (\'"+ empID + "\', \' "+empName+ " \', \'"
+empSal+ "\')";

mysql_query(connect, sql.c_str());
inFile >> empSal;

}


If I remove the space from the text file ("SmithJoe" rather than "Smith Joe"), the database populates properly.

I would like to ignore the white-space while reading into the file, without deleting the white-space, in order to put the entire string "Smith Joe" into one cell of the database.

Is there a way to ignore the white space between the two names without deleting the space so that the entire string "Smith Joe" can be read into one cell of the database?

Thankyou

Answer

You can try by reading the empName into two variables like empFirstName and empLastName, then concat them while inserting into database.