Redhat C++串連Mysql資料庫

來源:互聯網
上載者:User

一、代碼如下

#include "/usr/local/mysql/include/mysql/mysql.h"

#include <stdio.h>

int main(int argc,char *argv[])

{

char *user="root",*pwd="123456",*dbname="test";

MYSQL 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("查詢成功!");

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\t%s\t%s\t%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

二、Mysql.h檔案中mysql 的源碼包中。

Libmysqlclient.so.15錯誤為ldconfig連結引用無載入libmysqlclient.so檔案

Cd /etc/ld.so.conf.d

Vi mysql-i386.conf

加入/usr/local/mysql/lib/mysql代碼OK。

三、Mysql C API

my_ulonglong STDCALL mysql_num_rows(MYSQL_RES *res);//擷取資料集行數
unsigned int STDCALL mysql_num_fields(MYSQL_RES *res);//擷取資料集欄位數
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);//根據欄位迴圈資料集(欄位名)

MYSQL_ROW    STDCALL mysql_fetch_row(MYSQL_RES *result);//根據行迴圈資料庫集(從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);//擷取影響行數
my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql);//擷取插入ID
unsigned int STDCALL mysql_errno(MYSQL *mysql);//錯誤號碼
const char * STDCALL mysql_error(MYSQL *mysql);//錯誤詳細資料
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);//初始化
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);//串連MYSQL
int        STDCALL mysql_select_db(MYSQL *mysql, const char *db);//切換資料庫
int        STDCALL mysql_query(MYSQL *mysql, const char *q);//增,刪,改,查
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);

更具體的可以查看mysql.h檔案代碼

四、查詢出來的資料庫在Redhat終端中顯示亂碼。有知道的博友麻煩賜教一下。謝謝!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.