mysql linux c api and notes

Commonly used linux C API

MYSQL *conn = mysql_init(void);         //Initialize mysql and return mysql handle

conn = mysql_real_connect(conn, "localhost"Or server IP, User name, password, database databaseļ¼Œ0, NULL, 0);
//iferr (!conn)

int res = mysql_real_quary(conn, (char *)cmd, len);     //Send a mysql request, @cmd is a mysql command statement, see the following commonly used mysql statement for details, @len is the length of @cmd
//iferr (res)

//Open automatic reconnection
char value = 1;
mysql_options(conn, MYSQL_OPT_RECONNECT, &value);

MYSQL_RES *res = mysql_store_result(conn);      //Download offline result sets
r = mysql_num_rows(res);                        //Get rows
c = mysql_num_fields(res);                      //Column Number (Element Number)
while ((r = mysql_fetch_field(res)))            //Traversing each row
    //r[0], r[1]... is the first and second element value of each line.
    //do somthing...

mysql_free_result(res); //Release result set
mysql_close(conn);      //Close connection
mysql_error(conn);      //Error strings
mysql_errno(conn);      //error code

//The following is a multi-threaded access to mysql related functions. See the following multi-threaded routine for detailed usage
mysql_library_init(0, NULL, NULL);      //Initialize mysql client (multithreaded security)
mysql_library_end();                    //
mysql_thread_init();                    //Initialize thread-specific variables
mysql_thread_end();                     //

Multithread related routines

void *thread(void *arg)
    mysql_thread_init();    //Calls must be made as early as possible within the thread created
    MYSQL *conn = mysql_init();
    conn = mysql_real_connect(conn, "localhost", "user", "passwd", "database", 0, NULL, 0);

    //do somthing...    

    mysql_thread_end();   //Call before thread launch to avoid memory leak

int main()
    //This function must be invoked in a multithreaded environment before any thread is created and any mysql operation is performed
    if (mysql_library_init(0, NULL, NULL)) 
    //do somthing...

    pthread_t t;
    pthread_create(&t, NULL, thread, NULL);

    //do somthing...
    mysql_library_end();  //Release resources before the end of the program to avoid memory leaks
    return 0;

Common mysql statements

  • select * from table name // Get all data in the table
  • select element name from table name // Get all the elements in the table
  • select element name from table name where count=1 // Gets all the elements in the table that satisfy the expression count=1
  • delete from table name where expression // delete elements satisfying expression in table
  • insert into table name values (values, values)// insert a data into the table, with each element value in parentheses
  • truncate table name // empty table
  • Create table table table name (element name type (length), element name type (length)//create table
  • drop table table table name // delete table
  • The load data infile file name ignore into table table table table table name fields terminated by'-'// imports the file into the database, where ignore means ignore the unique key value that encounters duplication, which can be replaced by replace means replace the unique key value that encounters duplication, and skip the following data if neither of them is selected. Fields terminated by'-'means to segment elements in a file with'-' characters.

Problems encountered

Start mysql service to report errors

Error: Failed to start mysql.service: Unit mysql.service is masked
Command line execution statement: system CTL unmask mysql. service

Import file error reporting

Execute on the mysql command line: show variables like'% secure_file_priv%'
View the value. If the value is a path, the imported file must be in that path before it can be imported.
If you want to modify the path or value NULL, you need to make the following modifications

Add my.cnf file at / etc /


The path of the specified import file can be written after the equal sign, and the import file can be in any path if it is not specified as shown above.

At present, only these have been used, not related to the opportunity to update from time to time in the future.

Keywords: MySQL Database Linux

Added by kpmonroe on Sun, 19 May 2019 05:54:15 +0300