RedHat C ++ connects to the MySQL database

Source: Internet
Author: User

I,CodeAs follows:

# Include "/usr/local/MySQL/include/MySQL. H"

# Include <stdio. h>

Int main (INT argc, char * argv [])

{

Char * user = "root", * Pwd = "123456", * dbname = "test ";

MySQL;

Mysql_res * mysql_ret;

Mysql_row row;

Unsigned long num_rows;

Int ret;

Mysql_init (& MySQL );

If (mysql_real_connect (& MySQL, null, user, PWD, dbname, 0, null, 0 ))

{

Printf ("connectionsuccess! \ N ");

Mysql_query (& MySQL, "set names 'latin1 '");

Ret = mysql_query (& MySQL, "select * From sys_userinfo limit 5 ");

If (! RET)

{

Printf ("query successful! ");

Mysql_ret = mysql_store_result (& MySQL );

If (mysql_ret! = NULL)

{

Num_rows = mysql_num_rows (mysql_ret );

If (num_rows! = 0)

{

Printf ("% d \ n", num_rows );

While (ROW = mysql_fetch_row (mysql_ret ))

{

Printf ("% s \ t % s \ n", row [0], row [1], row [2], row [3], row [4], row [5]);

}

}

Else

{

Return-1;

}

Mysql_free_result (mysql_ret );

Return 0;

}

Else

{

Return-1;

}

} Else {

Return-1;

}

Mysql_close (& MySQL );

} Else {

Return-1;

}

}

G ++ mysqlconn. C-o mysqlconn-lmysqlclient-L/usr/local/MySQL/lib/MySQL/

./Mysqlconn

2. MySQL source package in the mysql. h file.

Libmysqlclient. so.15 error: ldconfig link reference does not load libmysqlclient. So file

CD/etc/lD. So. conf. d

VI mysql-i386.conf

Add/usr/local/MySQL/lib/MySQL code OK.

Iii. MySQL C API

My_ulonglong stdcall mysql_num_rows (mysql_res * res); // obtain the number of dataset rows
Unsigned int stdcall mysql_num_fields (mysql_res * res); // obtain the number of dataset Fields
My_bool stdcall mysql_eof (mysql_res * res );
Mysql_field * stdcall mysql_fetch_field_direct (mysql_res * res,
Unsigned int fieldnr );
Mysql_field * stdcall mysql_fetch_fields (mysql_res * res); // cyclically Datasets Based on fields (Field Names)

Mysql_row stdcall mysql_fetch_row (mysql_res * result); // loop database set by row (field number starting from 0)
Mysql_row_offset stdcall mysql_row_tell (mysql_res * res );
Mysql_field_offset stdcall mysql_field_tell (mysql_res * res );

Unsigned int stdcall mysql_field_count (MySQL * MySQL );
My_ulonglong stdcall mysql_affected_rows (MySQL * MySQL); // obtain the number of affected rows
My_ulonglong stdcall mysql_insert_id (MySQL * MySQL); // obtain the insert ID
Unsigned int stdcall mysql_errno (MySQL * MySQL); // error code
Const char * stdcall mysql_error (MySQL * MySQL); // error details
Const char * stdcall mysql_sqlstate (MySQL * MySQL );
Unsigned int stdcall mysql_warning_count (MySQL * MySQL );
Const char * stdcall mysql_info (MySQL * MySQL );
Unsigned long stdcall mysql_thread_id (MySQL * MySQL );
Const char * stdcall mysql_character_set_name (MySQL * MySQL );
Int stdcall mysql_set_character_set (MySQL * MySQL, const char * csname );

MySQL * stdcall mysql_init (MySQL * MySQL); // Initialization
My_bool stdcall mysql_ssl_set (MySQL * MySQL, const char * key,
Const char * cert, const char * ca,
Const char * capath, const char * cipher );
Const char * stdcall mysql_get_ssl_cipher (MySQL * MySQL );
My_bool stdcall mysql_change_user (MySQL * MySQL, const char * user,
Const char * passwd, const char * dB );
MySQL * stdcall mysql_real_connect (MySQL * MySQL, const char * host,
Const char * user,
Const char * passwd,
Const char * dB,
Unsigned int port,
Const char * unix_socket,
Unsigned long clientflag); // connect to MySQL
Int stdcall mysql_select_db (MySQL * MySQL, const char * dB); // switch the database
Int stdcall mysql_query (MySQL * MySQL, const char * q); // add, delete, modify, and query
Int stdcall mysql_send_query (MySQL * MySQL, const char * Q,
Unsigned long length );
Int stdcall mysql_real_query (MySQL * MySQL, const char * Q,
Unsigned long length );
Mysql_res * stdcall mysql_store_result (MySQL * MySQL );
Mysql_res * stdcall mysql_use_result (MySQL * MySQL );

/* Perform query on master */
My_bool stdcall mysql_master_query (MySQL * MySQL, const char * Q,
Unsigned long length );
My_bool stdcall mysql_master_send_query (MySQL * MySQL, const char * Q,
Unsigned long length );
/* Perform query on slave */
My_bool stdcall mysql_slave_query (MySQL * MySQL, const char * Q,
Unsigned long length );
My_bool stdcall mysql_slave_send_query (MySQL * MySQL, const char * Q,
Unsigned long length );
Void stdcall mysql_get_character_set_info (MySQL * MySQL,
My_charset_info * charset );

For more details, see the mysql. h file code.

4. The queried database is garbled on the RedHat terminal. If you have any knowledge, please kindly advise. 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.