We are attempting to take data from a file and insert it into a mysql table with this table format. And then later retrieve the data and put it back into the same format that the data was in the original file. This is how we create the table.
create_query = "CREATE TABLE IF NOT EXISTS ";
string date = "20170223";
string table_format = " (IDpk INT NOT NULL auto_increment,"
" symbol CHAR(8) NOT NULL,"
" number_1 BIGINT unsigned DEFAULT NULL,"
" number_2 BIGINT unsigned NOT NULL,"
" PRIMARY KEY(IDpk))";
This is how we are create the strings that are being inserted into the database.
uint32_t item_type = stoi(tokens[2]);
query = insert_query;
switch(item_type)
{
case 0: //bid
case 1: //ask
{ "###,%s,%s,"
"NULL,"
"%s,%s",
tokens[0], tokens[1], tokens[2]);
break;
}
And then we are passing the data to this function where we actually are inserting the data into the table.
void read_in_data(string date, string file, MYSQL* conn)
{
string query = "LOAD DATA INFILE " + file +
"INTO TABLE " + date +
"FIELDS TERMINATED BY ','"
"LINE STARTING BY '###'";
mysql_query(conn, query.c_str());
}
The issue arises when we are attempting to get the data back from the database later and reformat it to be like the original input file. We noticed that our NULL values were represented by 0 and not NULL. We are curious how we would got about inserting them so that they are NULL and not 0. This is our current extraction method.
ofstream out_file;
out_file.open("test.txt", fstream::in);
if(out_file.good())
{
res = mysql_store_result(conn);
int num_fields = mysql_num_fields(res);
while((row = mysql_fetch_row(res)))
{
unsigned long * lengths;
lengths = mysql_fetch_lengths(res);
for(int i = 0; i < num_fields; i++)
{
if(row[i])
{
out_file << row[i] << " ";
}
}
out_file << endl;
}
}else
cout << strerror(errno) << endl;