how to import csv file to embedded MySQL in C
Posted by: Gurol Cerci
Date: June 06, 2014 09:45AM

Hi,

I need an urgent help.

I am trying to write a C code with embedded MySQL with server and client options. I am trying to important a csv data file for MySQL server and client.

I saw on internet like:
LOAD DATA LOCAL INFILE 'data.csv' INTO TABLE test FIELDS TERMINATED BY',' LINES TERMINTED BY'\n';

But I think that line is for SQL workspace. How to write it in C format?

My code is like:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "/usr/include/mysql/mysql.h"

MYSQL *mysql;
MYSQL_RES *results;
MYSQL_ROW record;

static char *server_options[] = { "data.csv", "--defaults-file=my.cnf" };
int num_elements = sizeof(server_options)/ sizeof(char *);

static char *server_groups[] = { "libmysqld_server", "libmysqld_client" };

int main(void)
{
mysql_server_init(num_elements, server_options, server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

mysql_real_connect(mysql, NULL,NULL,NULL, "data", 0,NULL,0);

mysql_query(mysql, "SELECT data_id, from the data series");

results = mysql_store_result(mysql);

while((record = mysql_fetch_row(results))) {
printf("%s - %s \n", record[0], record[1]);
}

mysql_free_result(results);
mysql_close(mysql);
mysql_server_end();

return 0;
}

Options: ReplyQuote


Subject
Views
Written By
Posted
how to import csv file to embedded MySQL in C
3396
June 06, 2014 09:45AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.