MySQL basics: MySQL and C combined instances

Source: Internet
Author: User

A simple interaction between MySQL and C, using some MySQL C APIs!

Old birds pass by. You can check it out!

/*************************************** * ** This document describes how to use the C interface of MySQL to initialize a database, check whether the database exists. If the database does not exist, create it. Check whether the table exists, if it does not exist, create ************************************* * ***/# include <stdio. h> # include <stdlib. h> # include <string. h> // the header file containing MySQL is required, /usr/include/MySQL //-L/usr/lib/-lmysqlclient-I/usr/include/MySQL option # include <MySQL. h> // basic header file, declaration of some important struct # include <errmsg. h> // store the error message # include <mysql_version.h> // contains the current MySQL version information // All variable, macro definition // # define debug # define server_host "Lo Calhost "// MySQL remote address # define server_user" root "// database login name # define server_pwd" 123 "// database login password # define db_name" tmp_db "// create a database name # define table_name "mytables" // table int check_tbl in the database (MySQL * MySQL, char * Name); int check_db (MySQL * MySQL, char * db_name); int init_db () {int err = 0; MySQL; If (! Mysql_init (& MySQL) {perror ("mysql_init:"); exit (1) ;}if (! Mysql_real_connect (& MySQL, server_host, server_user, server_pwd, null, 0, null, 0) {perror ("mysql_real_connect"); exit (1 );} printf ("connected ..... \ n "); err = check_db (& MySQL, db_name); If (Err! = 0) {printf ("create dB is err! \ N "); mysql_close (& MySQL); exit (1) ;}// select which dB if (mysql_select_db (& MySQL, db_name) // return 0 is success ,! 0 is err {perror ("mysql_select_db:"); mysql_close (& MySQL); exit (1) ;}// chuangjianbiao if (ERR = check_tbl (& MySQL, table_name ))! = 0) {printf ("check_tbl is err! \ N "); mysql_close (& MySQL); exit (1) ;}mysql_close (& MySQL); Return 0 ;}int check_db (MySQL * MySQL, char * db_name) {mysql_row ROW = NULL; mysql_res * res = NULL; Res = mysql_list_dbs (MySQL, null); If (RES) {While (ROW = mysql_fetch_row (RES ))! = NULL) {printf ("DB is % s \ n", row [0]); If (strcmp (row [0], db_name) = 0) {printf ("find dB % s \ n", db_name); break ;}// mysql_list_dbs allocates memory. Use mysql_free_result to release mysql_free_result (RES);} If (! Row) // if this database does not exist, {char Buf [128] = {0} is created; strcpy (BUF, "create database"); strcat (BUF, db_name ); # ifdef debug printf ("% s \ n", Buf); # endif if (mysql_query (MySQL, Buf) {fprintf (stderr, "query failed (% s) \ n ", mysql_error (MySQL); exit (1) ;}} return 0 ;}int check_tbl (MySQL * MySQL, char * Name) {If (name = NULL) return 0; mysql_row ROW = NULL; mysql_res * res = NULL; Res = mysql_list_tables (MySQL, null); If (RES) {whil E (ROW = mysql_fetch_row (RES ))! = NULL) {printf ("tables is % s \ n", row [0]); If (strcmp (row [0], name) = 0) {printf ("find the table! \ N "); break ;}} mysql_free_result (RES);} If (! Row) // create table {char Buf [128] = {0}; char qbuf [128] = {0}; snprintf (BUF, sizeof (BUF ), "% s (name varchar (20), sex char (1), score int (3);", table_name); strcpy (qbuf, "create table "); strcat (qbuf, Buf); // # ifdef debug printf ("% s \ n", qbuf); // # endif if (mysql_query (MySQL, qbuf )) {fprintf (stderr, "query failed (% s) \ n", mysql_error (MySQL); exit (1) ;}} return 0 ;}int main () {int err = 0; err = init_db (); Return 0 ;}

Compilation Method: The last-I must check whether the directory exists. Otherwise, an error is reported.

gcc -o mysql mysql.c -L/usr/lib/ -lmysqlclient -I/usr/include/mysql

Error:

Root @ jack-desktop :~ # Gcc-O MySQL. C-L/usr/lib/-lmysqlclientgcc: mysql. C: no such file or directory root @ jack-desktop :~ # Root @ jack-desktop :~ # Root @ jack-desktop :~ # Cd KYC/root @ jack-desktop: KYC # gcc-O MySQL. c-l/usr/lib/-lmysqlclientmysql. c: 15: 73: Error: MySQL. h: there is no such file or directory MySQL. c: 16: 43: Error: errmsg. h: there is no such file or directory MySQL. c: 17: 63: Error: mysql_version.h: there is no such file or directory MySQL. c: 27: Error: Expected ') 'before' * 'tokenmysql. c: 28: Error: Expected ') 'before' * 'tokenmysql. c: In function 'init _ db': MySQL. c: 34: Error: 'mysql' undeclared (first use in this function) MySQL. c: 34: Error: (each undeclared identifier is reported only oncemysql. c: 34: Error: for each function it appears in .) mySQL. c: 34: Error: Expected '; 'before' MySQL' MySQL. c: 36: Error: 'mysql' undeclared (first use in this function) MySQL. c: at top level: MySQL. c: 73: Error: Expected ') 'before' * 'tokenmysql. c: 109: Error: Expected ') 'before' * 'token

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.