首先安装必需的开发
包
sudo apt-get install gcc g++ libgcc1 libg++ make gdb
安装MYSQL的C语言开发包
sudo apt-get install libmysql++-dev libmysql++2c2a libmysqlclient15-dev libmysqlclient15off
把lib文件拷贝到公用
sudo cp /usr/lib/mysql/* /usr/lib/
建立一个新文件test.cpp
vim test.cpp
输入内容
#include
#include
#include "/usr/include/mysql/mysql.h"
int main() {
MYSQL mysql;
mysql_init(&mysql);
}
ctrl+O保存ctrl+x退出。
然后编译
g++ -lmysqlclient test.cpp -o test
运行
./test
如果什么都没提示,没错误,就说明成功了。
下面是自己写的一个小程序:查询mysql进行弱点关联。
#include
#include
#include
#include
#include "/usr/include/mysql/mysql.h"
void asso_1(){
MYSQL mysql_conn; /* Mysql connection handle */
MYSQL_RES *mysql_result1, *mysql_result2, *mysql_result3; /* Result handle */
MYSQL_ROW mysql_row1, mysql_row2, mysql_row3; /* Row data */
int f1, f2, num_row1, num_col1, num_row2, num_col2;
printf("Start association analyse type 1: Weekness association\n");
int i;
for (i = 0; i<20; i++) printf("# ");
printf("\n");
if (mysql_init(&mysql_conn) == NULL) printf("Initialization fails\n");
if
(mysql_real_connect(&mysql_conn, "localhost", "root", "123456",
"test", 3306, NULL, 0) == NULL) printf("Connection fails\n");
if (mysql_query(&mysql_conn, "SELECT scid,aid,proto,dip,dport from `scan_table`") != 0)
printf("Query fails\n");
mysql_result1 = mysql_store_result(&mysql_conn);
num_row1 = mysql_num_rows(mysql_result1); /* Get the no. of row */
num_col1 = mysql_num_fields(mysql_result1); /* Get the no. of column */
for (f1 = 0; f1 < num_row1; f1++) {
mysql_row1 = mysql_fetch_row(mysql_result1); /* Get a row */
printf ("%s %s %s %s\n",mysql_row1[1],mysql_row1[2],mysql_row1[3],mysql_row1[4]);
char *str;
str = (char *)malloc(1024);
strcpy(s