Linux c programming to access the database, linux to access data

Source: Internet
Author: User

Linux c programming to access the database, linux to access data

The source code is as follows:

# Include <stdio. h> # include <stdlib. h> # include <mysql/mysql. h> int main () {/* defines the mysql variable */MYSQL * coon; // The handle MYSQL_RES * res used for database connection; // returns the row query result MYSQL_ROW row; // a piece of data in the record set: char server [] = "192.168.1.105"; char user [] = "gino"; char password [] = "123456 "; char database [] = "mysql"; coon = mysql_init (NULL); // initialize and obtain the mysql handle/* connect to the database */if (! Mysql_real_connect (coon, server, user, password, database, 0, NULL, 0) {fprintf (stderr, "% s \ n", mysql_errno (coon )); exit (1);}/* execute mysql statement */if (mysql_query (coon, "show tables") {fprintf (stderr, "% s \ n ", mysql_errno (coon); exit (1);}/* initialize the row-by-row result set */res = mysql_use_result (coon); printf ("MySQL Tables in mysql datables: \ n "); // retrieve the next row from the result set while (row = mysql_fetch_row (res ))! = NULL) {printf ("% s \ n", row [0]);} // release the result set using multiple memory mysql_free_result (res ); // close the server connection mysql_close (coon); printf ("finish \ n"); return 0 ;}

-Lmysqlclient is required for compilation after mysql API is used.

Install libmysqlclient:

sudo apt-get install libmysqlclient-dev

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.