C语言mysql API
Table 5.1 C API Basic Interface Functions
Name Description Introduced Deprecated
mysql_affected_rows() Number of rows changed/deleted/inserted by last UPDATE, DELETE, or INSERT statement
mysql_autocommit() Set autocommit mode
mysql_bind_param() Define query attributes for next statement executed 8.0.23
mysql_change_user() Change user and database on an open connection
mysql_character_set_name() Default character set name for current connection
mysql_close() Close connection to server
mysql_commit() Commit transaction
mysql_connect() Connect to MySQL server Yes
mysql_create_db() Create database Yes
mysql_data_seek() Seek to arbitrary row number in query result set
mysql_debug() Perform DBUG_PUSH with given string
mysql_drop_db() Drop database Yes
mysql_dump_debug_info() Cause server to write debug information to error log
mysql_eof() Determine whether last row of result set has been read Yes
mysql_errno() Error number for most recently invoked MySQL function
mysql_error() Error message for most recently invoked MySQL function
mysql_escape_string() Escape special characters in string for use in SQL statement
mysql_fetch_field() Type of the next table field
mysql_fetch_field_direct() Table field type for given field number
mysql_fetch_fields() Return array of all field structures
mysql_fetch_lengths() Return lengths of all columns in current row
mysql_fetch_row() Fetch next result set row
mysql_field_count() Number of result columns for most recent statement
mysql_field_seek() Seek to column within result set row
mysql_field_tell() Field position for last mysql_fetch_field() call
mysql_free_result() Free result set memory
mysql_free_ssl_session_data() Dispose of session data handle from last mysql_get_ssl_session_data() call 8.0.29
mysql_get_character_set_info() Information about default character set
mysql_get_client_info() Client version (string)
mysql_get_client_version() Client version (integer)
mysql_get_host_info() Information about the connection
mysql_get_option() Value of a mysql_options() option
mysql_get_proto_info() Protocol version used by the connection
mysql_get_server_info() Server version number (string)
mysql_get_server_version() Server version number (integer)
mysql_get_ssl_cipher() Current SSL cipher
mysql_get_ssl_session_data() Return session data for SSL-enabled connection 8.0.29
mysql_get_ssl_session_reused() Whether a session is reused 8.0.29
mysql_hex_string() Encode string in hexadecimal format
mysql_info() Information about most recently executed statement
mysql_init() Get or initialize a MYSQL structure
mysql_insert_id() ID generated for an AUTO_INCREMENT column by previous statement
mysql_kill() Kill a thread Yes
mysql_library_end() Finalize MySQL C API library
mysql_library_init() Initialize MySQL C API library
mysql_list_dbs() Return database names matching regular expression
mysql_list_fields() Return field names matching regular expression
mysql_list_processes() List of current server threads
mysql_list_tables() Return table names matching regular expression
mysql_more_results() Check whether more results exist
mysql_next_result() Return/initiate next result in multiple-result execution
mysql_num_fields() Number of columns in result set
mysql_num_rows() Number of rows in result set
mysql_options() Set option prior to connecting
mysql_options4() Set option prior to connecting
mysql_ping() Ping server
mysql_query() Execute statement
mysql_real_connect() Connect to MySQL server
mysql_real_connect_dns_srv() Connect to MySQL server using DNS SRV record 8.0.22
mysql_real_escape_string() Encode special characters in statement string
mysql_real_escape_string_quote() Encode special characters in statement string accounting for quoting context
mysql_real_query() Execute statement
mysql_refresh() Flush or reset tables and caches
mysql_reload() Reload grant tables Yes
mysql_reset_connection() Reset the connection to clear session state
mysql_reset_server_public_key() Clear cached RSA public key from client library
mysql_result_metadata() Whether a result set has metadata 8.0.13
mysql_rollback() Roll back transaction
mysql_row_seek() Seek to row offset in result set
mysql_row_tell() Current position within result set row
mysql_select_db() Select database
mysql_server_end() Finalize MySQL C API library
mysql_server_init() Initialize MySQL C API library
mysql_session_track_get_first() First part of session state-change information
mysql_session_track_get_next() Next part of session state-change information
mysql_set_character_set() Set current connection default character set
mysql_set_local_infile_default() Set LOAD DATA LOCAL handler callbacks to default values
mysql_set_local_infile_handler() Install application-specific LOAD DATA LOCAL handler callbacks
mysql_set_server_option() Set option for current connection
mysql_shutdown() Shut down MySQL server
mysql_sqlstate() SQLSTATE value for most recently invoked MySQL function
mysql_ssl_set() Prepare to establish SSL connection to server
mysql_stat() Server status
mysql_store_result() Retrieve and store entire result set
mysql_thread_id() Current thread ID
mysql_use_result() Initiate row-by-row result set retrieval
mysql_warning_count() Warning count for previous statement
代码
#include <winsock2.h>//windows下mysql.h用到,不添加则报错
#include <mysql.h>
#include<iostream>
using namespace std;
int main(){
MYSQL sql;
mysql_init(&sql);
//设置中编码
if(mysql_set_character_set(&sql, "gbk") != 0){
cout<<__LINE__<<"error:"<<mysql_error(&sql)<<endl;
return -1;
}
//链接mysql,主机地址127.0.0.1,用户名root,密码1234,使用的数据库mydb,端口3306
if (mysql_real_connect(&sql, "127.0.0.1", "root", "1234", "mydb", 3306, NULL, 0)){
cout<<"MySQL connect success!"<<endl;
}else{
cout<<"MySQL connect failed!"<<endl;
return -1;
}
//查询设置中文编码,否则显示中文乱码
mysql_query(&sql, "set names gbk");
//从table_person表中读取数据
if (!mysql_query(&sql, "select * from table_person"))
{
cout<<"query success!"<<endl;
}else{
cout<<"query failed!"<<endl;
return -1;
}
//获取查询结果
MYSQL_RES * rs = mysql_store_result(&sql);
//获取结果集的字段数
int num = mysql_num_fields(rs);
//获取行
MYSQL_ROW row = mysql_fetch_row(rs);
while (row)
{
//输出本行的所有字段
for (int i = 0; i < num; i++)
{
cout<<row[i]<<"\t";
}
cout<<endl;
//获取下一行结果
row = mysql_fetch_row(rs);
}
//释放结果集所占用的内存
mysql_free_result(rs);
//关闭与mysql的连接
mysql_close(&sql);
system("pause");
return 0;
}
编译和链接
g++ sqlDemo.cpp -ID:\libs\mysql5.5\include -LD:\libs\mysql5.5\lib -lmysql
使用-I参数指定mysql头文件目录,-L指定库文件目录,-l指定连接的库,c语言文件目录位于mysql安装目录下
对于gcc,链接到libmysql.dll这个文件,而libmysql.lib是给msvc使用的静态库文件
链接库和gcc位数要一直,最好都是64位,否则链接还是会出错,无法使用链接库
运行时需要libmysql.dll动态库的支撑,将libmysql.dll放在程序运行目录,否则报错
运行结果
mysql查询

程序运行
