Thread: how to import csv file to embedded MySQL in C

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    1

    how to import csv file to embedded MySQL in C

    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 in C format?


    My code is like:

    Code:
    #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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by cercig View Post
    I need an urgent help.
    Maybe you can find out more here. There's a similar question (more like identical)

    MySQL :: how to import csv file to embedded MySQL in C

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    1
    Thanks c99tutorial, for at least searching my question. But actually I wrote that question in the link you shared, I guess that's why it looks very identical
    I was more sure that I would get an answer on MySQL forum, but I didn't get any answer yet there too

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Try issuing the line

    Code:
    LOAD DATA LOCAL INFILE 'data.csv' INTO TABLE test FIELDS TERMINATED BY',' LINES TERMINTED BY'\n';
    as a query by calling mysql_query. This should tell the MySQL server to import the data for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cross platform embedded MySQL server
    By onetwoandthree in forum C++ Programming
    Replies: 1
    Last Post: 08-23-2010, 02:23 AM
  2. File import opinion.
    By Kennedy in forum Tech Board
    Replies: 2
    Last Post: 09-22-2006, 05:52 PM
  3. File import problem
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 11-15-2005, 04:13 PM
  4. import xml file to visual c++
    By Psico_Mind in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2005, 10:09 AM
  5. embedded MySQL .exe incompatibility ?
    By trekker in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 06:19 PM

Tags for this Thread