Note that there are some explanatory texts on larger screens.

plurals
  1. POpopulating a database from a file using c++
    primarykey
    data
    text
    <p>I've searched all over the net and looked in books, but I'm totally stuck. I'm trying to populate a database using a .txt file, sql and c++. I know one can use LOAD DATA INFILE, but I must use C++ for this project. </p> <p>Here is the database table in SQL:</p> <pre><code>create table Employee( employeeID char(5) not null, employeeTYPE char(15), employeeNAME char(25), hireYEAR integer, primary key(employeeID) ); </code></pre> <p>Here is the text file to be inserted into the database:</p> <pre><code>E0001 Assistant Joseph 2000 E0002 Assistant Thomas 2012 </code></pre> <p>I understand how to connect to the database and I know how to read into a file in C++, however I'm stuck on how to read into a file with the C++/SQL combo. Here is what I have so far for int main (), but any help or links would be so appreciated. Thank you:</p> <pre><code>int main () { MYSQL *connect, mysql; connect=mysql_init(&amp;mysql); //initialize the instance //log in and connect to the database connect=mysql_real_connect (connect, SERVER, USER, PASSWORD, DATABASE, 0, NULL, 0); //not included for privacy ifstream inFile; inFile.open ("employee.txt"); if (inFile.fail()) { cout &lt;&lt; "open for employee.txt. failed"; exit (1); } while (inFile) { mysql_query(connect, "insert into Employee values"); //not sure how to do this part... } inFile.close(); mysql_close(connect); return 0; } </code></pre> <p>//EDIT WITH ANSWER Thank you for the help, I have come to understand the problem. In order to read into the database from a file using C++ embedded in SQL, there are a few syntax ideas that must be known. </p> <p>For example, to concatenate a string, the "+" character is used. This differs from PHP where the "." character is used. Additionally, in order to utilize the ' character, an escape character \ must be used. </p> <p>Once you've connected to the database and opened the file using C++, you enter into a loop to read in from the file.</p> <p>the syntax to store the Insert statement in the string sql:</p> <pre><code> sql = "insert into TABLE values (\'"+ string1 + "\', \' " +string2 + " \', \'" +string3 + "\', \'" +string4+ "\')"; </code></pre> <p>Finally, in order to convert the strings to chars, the statement:</p> <pre><code>sql.c_str() </code></pre> <p>must be used as the second argument to mysql_query</p> <p>Hope this helps someone searching for help with this in the future. I'm a noob, so please let me know if there are any errors in my explanation above. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload