linux msyql 簡單使用

來源:互聯網
上載者:User

#include "mysql.h"
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
MYSQL*  sql_conn = NULL;
MYSQL_RES *res_ptr = NULL;
MYSQL_ROW sql_row;
sql_conn = mysql_init(NULL);
if( sql_conn == NULL)// 
{
cout <<"init conect fail" <<endl;
return 0;
}
if(mysql_real_connect(sql_conn,"192.168.1.223","root","123456","myLearnSql",MYSQL_PORT,NULL,0) == NULL)
{
cout << "connect database myLearnSql fail " << endl;
mysql_close(sql_conn);
return 0;
}
string strSqlQuery = "select f001v_0031,f002n_0031 from tb_0031";
int ret  = 0;
ret = mysql_query(sql_conn,strSqlQuery.c_str());
if(ret != 0)
{
cout << "query:" << strSqlQuery << "fail" << endl;
mysql_close(sql_conn);
return 0;
}
res_ptr = mysql_use_result(sql_conn);
if(res_ptr)
{
while ((sql_row = mysql_fetch_row(res_ptr))) 
{
printf("query: f002n_0031= %s, f001v_0031=%s\n", sql_row[0],sql_row[1]);
}
mysql_free_result(res_ptr);
}
mysql_close(sql_conn);
return 0;
}
編譯:
g++ -g -o mysql_main -I/usr/include/mysql/ mysql_main.cpp -L/usr/lib/mysql/ -lz
出現如下錯誤:
mysql_main.cpp:1:19: error: mysql.h: No such file or directory
mysql_main.cpp: In function ‘int main(int, char**)’:
mysql_main.cpp:9: error: ‘MYSQL’ was not declared in this scope
mysql_main.cpp:9: error: expected ‘;’ before ‘sql_conn’
mysql_main.cpp:10: error: ‘sql_conn’ was not declared in this scope
mysql_main.cpp:10: error: ‘mysql_init’ was not declared in this scope
mysql_main.cpp:15: error: ‘sql_conn’ was not declared in this scope
[root@localhost thread_learn]# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
[root@localhost thread_learn]# find / -name mysql.h
/usr/include/mysql/mysql.h
/home/yuj/install-hxlt/mysql-5.1.37/include/mysql.h
mysql.h 已經存在了,但是為什麼還是找不到檔案?
知道原因了,原來是把-I 寫成了l,是大寫的i 對 gcc編譯器不熟悉的原因
完整命令如下:
g++ -g -o mysql_main -I/usr/include/mysql mysql_main.cpp -L/usr/lib/mysql -lmysqlclient -lz
需要串連 mysqlclient庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.