Read and Write mysql in C Language

Source: Internet
Author: User

Read and Write mysql in C Language
Create a database

CREATE DATABASE tiger;CREATE TABLE `test` (    `id` int(11) NOT NULL auto_increment,    PRIMARY KEY (`id`));ALTER TABLE `test`    ADD COLUMN `name` varchar(20);
C language read/write Database
/* ===================================================== ============================================================ Name: mysqltest. c Author: Version: Copyright: Your copyright notice Description: Hello World in C, ansi-style ========================================== ======================================================= * /# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
MYSQL * g_conn; // mysql connection MYSQL_RES * g_res; // mysql record set MYSQL_ROW g_row; // string array, mysql record line # define MAX_BUF_SIZE 1024 // maximum number of buffer bytes const char * g_host_name = "127.0.0.1"; const char * g_user_name = "root"; const char * g_password = "123456 "; const char * g_db_name = "tiger"; const unsigned int g_db_port = 3306; void print_mysql_error (const char * msg) {// print the last error if (msg) printf ("% s: % s \ n", msg, mysql_error (g _ Conn); else puts (mysql_error (g_conn);} int executesql (const char * SQL) {/* query the database according the SQL */if (mysql_real_query (g_conn, SQL, strlen (SQL) // return-1 if the request fails; // return 0 if the request fails; // successful execution} int init_mysql () {// initialize the connection // init the database connection g_conn = mysql_init (NULL);/* connect the database */if (! Mysql_real_connect (g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0) // if return-1 fails; // whether the connection is available if (executesql ("set names utf8") // if return-1; return 0; // return successful} int main (void) {puts ("!!! Hello World !!! ");/* Prints !!! Hello World !!! */If (init_mysql (); print_mysql_error (NULL); char SQL [MAX_BUF_SIZE]; sprintf (SQL, "INSERT INTO 'test' ('name ') VALUES ('testname') "); if (executesql (SQL) print_mysql_error (NULL); if (executesql (" SELECT * FROM 'test '")) // There is no semicolon print_mysql_error (NULL) at the end of the sentence; g_res = mysql_store_result (g_conn); // transfer the result set from the server to the local machine, mysql_use_result directly uses the record set int iNum_rows = mysql_num_rows (g_res) on the server; // get the number of record rows int iNum_fields = mysql_num_fields (g_res ); // obtain the number of record columns printf ("% d records in total, each record % d field \ n", iNum_rows, iNum_fields); puts ("id \ tname \ n "); while (g_row = mysql_fetch_row (g_res) // print the result set printf ("% s \ t % s \ n", g_row [0], g_row [1]); // first, the second field mysql_free_result (g_res); // release the result set mysql_close (g_conn); // close the link return EXIT_SUCCESS ;}
    
   
  
 
Compile

Install mysql and specify the mysqlclient header file and library directory during compilation.

gcc -l mysqlclient -I /usr/local/mysql/include/ -L /usr/local/mysql/lib/ mysqltest.c
Run

You need to create a soft link/usr/lib/libmysqlclient. so.18->/usr/local/mysql/lib/libmysqlclient. so.18 in the/usr/lib/directory.

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.