Linux c mysql Programming

Source: Internet
Author: User

First go to one

// Test1.c:

# Include <stdio. h>
# Include <mysql. h>

Int main (int argc, char * argv [])
{
Int I;
Char * names [3];
Names [0] = "haha ";
Names [1] = "xixi ";
Names [2] = "wuwu ";

Char execsql [1024];

MYSQL mysql_conn;

If (mysql_init (& mysql_conn )! = NULL ){
If (mysql_real_connect (& mysql_conn, "localhost", "root", "xiaodan", "test", MYSQL_PORT, NULL, 0 )! = NULL)
{
Printf ("Good connection! \ N ");
Sprintf (execsql, "create database one_db ");
Mysql_real_query (& mysql_conn, execsql, strlen (execsql ));

// If (! Mysql_create_db (& mysql_conn, "one_db ")){
I = mysql_affected_rows (& mysql_conn );
If (I <= 0 ){
Printf ("Can not create database one_db \ n ");
}
Else {
Mysql_select_db (& mysql_conn, "one_db ");
Sprintf (execsql, "create table girls (name char (10), age int (8 ))");
Mysql_real_query (& mysql_conn, execsql, strlen (execsql ));
Mysql_select_db (& mysql_conn, "one_db ");
For (I = 0; I <3; I ++ ){
Sprintf (execsql, "insert into girls values ('% s', % d)", names [I], 19 + I );
Printf ("% s \ n", execsql );
Mysql_query (& mysql_conn, execsql );
}
}
} Else {
Printf ("Connection Failed! \ N ");
}
} Else {
Printf ("Initialization Failed. \ n ");
Return-1;
}

Mysql_close (& mysql_conn );
Return 0;
}

Compile command: gcc-g-o test1-I/usr/include/mysql test1.c-L/usr/lib/mysql-lmysqlclient-lz

// Test2.c

# Include <stdio. h>

// # Include <dmalloc. h>

# Include <mysql. h>

Int main (int argc, char ** argv ){

MYSQL mysql_conn;/* Connection handle */

MYSQL_RES * mysql_result;/* Result handle */

MYSQL_ROW mysql_row;/* Row data */

Int f1, f2, num_row, num_col;

If (mysql_init (& mysql_conn )! = NULL ){

If (mysql_real_connect (& mysql_conn, "localhost", "root", "xiaodan", "one_db", MYSQL_PORT, NULL, 0 )! = NULL ){

If (mysql_query (& mysql_conn, "select * from girls") = 0 ){

/* Run the select statement to query the records in the Table */

Mysql_result = mysql_store_result (& mysql_conn); // get the result from the executing select query

Num_row = mysql_num_rows (mysql_result);/* Get the no. of row */

Num_col = mysql_num_fields (mysql_result);/* Get the no. of column */

For (f1 = 0; f1 <num_row; f1 ++ ){

// For (f2 = 0; f2 <num_col; f2 ++ ){

Mysql_row = mysql_fetch_row (mysql_result);/* Fetch one by one */

Printf ("[Row % d, Col 0] ==> [% s] \ n", f1, mysql_row [0]);
Printf ("[Row % d, Col 1] ==> [% s] \ n", f1, mysql_row [1]);

//}

}

} Else {

(Void) printf ("Query fails \ n ");

}

} Else {

(Void) printf ("Connection fails \ n ");

}

} Else {

(Void) printf ("Initialization fails \ n ");

}

Mysql_free_result (mysql_result );

Mysql_close (& mysql_conn );

Return 0;

}

Compile command: gcc-g-o test2-I/usr/include/mysql test2.c-L/usr/lib/mysql-lmysqlclient-lz

Note: The databases installed using the rpm package do not have header files and libraries for development. You need to download the MySQL-devel rpm development kit.

 

Below is

 

/*
*
* File name: mysql. c
*
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include "mysql. h"

Int main (void)
{
Const char * host = "localhost ";
Const char * user = "root ";
Const char * pass = "";
Const char * db = "mysql ";
/* Define mysql variables */
MYSQL mysql;
MYSQL_RES * rs;
MYSQL_ROW row;
Mysql_init (& mysql);/* initialization */
/* Connect to the database */
If (! Mysql_real_connect (& mysql, host, user, pass, db, 0, NULL, 0 ))
{
Printf ("% s", mysql_error (& mysql ));

}
Char * SQL = "select host, user from user order by rand ()";
If (mysql_query (& mysql, SQL )! = 0)
{/* Query */
Printf ("% s", mysql_errno (& mysql), mysql_error (& mysql ));

}
Rs = mysql_store_result (& mysql);/* obtain the query result */
While (row = mysql_fetch_row (rs )))
{/* Obtain each row of records */
Printf ("% s --- % s", row [0], row [1]);
}
Mysql_free_result (rs);/* release result set */
Mysql_close (& mysql);/* close the connection */
Return 1;
}

Compile: $ gcc-g-o mysql-I/usr/local/mysql/include/mysql. c-L/usr/lolinoleic/mysql/lib/mysql-lmysqlclient-lz
If the error libmysqlclient. so.16 is not found during running, run the following command.
$ Ln-s/usr/local/mysql/lib/mysql/libmysqlclient. so.16/lib/libmysqlclient. so.16

If you run the program:

$ Gcc-g-o mysql-I/usr/local/mysql/include/mysql. c-L/usr/lolinoleic/mysql/lib/mysql-lmysqlclient-lz will generate mysql files in the current directory

./Run mysql

Or

/Path-to-mysql/mysql

 

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.