Sample programs for processing MySQL Databases in C language in Linux

Source: Internet
Author: User

In Linux, it took me a long time to figure out how to process the MySQL database sample program in C language.

Compile:

Reference

G ++-Imysql/include-Lmysql/lib-o a. out a. cpp-lmysqlclient-lz

This mysql directory is the source code directory of a mysql. If mysql-devel is installed in/usr/ooxx/mysql, you can go down the source code by yourself.

# Include <iostream>

# Include <string>

# Include <fstream>

# Include "mysql. h"

Using namespace std;

MYSQL * conn = NULL; // MySQL connection handle

// Read Configuration

Bool read_conf (string & dbhost,

String & dbuser,

String & dbpass,

String & dbname ){

Ifstream is ("judge. conf ");

Is> dbhost> dbuser> dbpass> dbname;

Return true;

}

Bool query (){

Int status, state, state2;

MYSQL_RES * res;

MYSQL_ROW row;

String cmd;

MYSQL * conn, mysql;

String dbhost, dbuser, dbpass, dbname;

If (read_conf (dbhost, dbuser, dbpass, dbname) {// read the database

If (mysql_init (& mysql) = NULL ){

Fprintf (stderr, "initialization error \ n ");

Return false;

}

Conn = mysql_real_connect (& mysql, dbhost. c_str (), dbuser. c_str (),

Dbpass. c_str (), dbname. c_str (), NULL,

"/Var/run/mysqld. sock", 0 );

If (conn = NULL) {// connection failed

Fprintf (stderr, "connection failed! \ N ");

Return false;

}

} Else {// reading configuration failed

Fprintf (stderr, "reading configuration failed! \ N ");

Return false;

}

Cmd = "select ooxx from ooxx ";

Cout <"Command:" <cmd <endl;

State = mysql_query (conn, cmd. c_str ());

Cout <"state:" <state <endl;

If (state! = 0) {// query Error

Fprintf (stderr, "% s \ n", mysql_error (conn ));

Return false;

}

Res = mysql_store_result (conn );

Cout <"affected_rows:" <conn-> affected_rows <endl;

While (row = mysql_fetch_row (res), row! = NULL ){

Printf ("% s \ n", row [0]);

}

Return true;

}

Int main (){

Query ();

Return 0;

}

If you think this article is useful to you, leave a message below to let me know. Thank you very much ^_^

Reprint please indicate from http://felix021.com/blog/read.php? 1505 (if it is reproduced, the original source is indicated), thank you :)

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.