Linux环境MySQL的C编程起步


我的Linux环境是debian 2.6.x

首先安装必需的开发包

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(str,"SELECT * FROM ids_table WHERE aid=");
strcat(str,mysql_row1[1]);
strcat(str," and proto='");
strcat(str,mysql_row1[2]);
strcat(str,"' and dip='");
strcat(str,mysql_row1[3]);
strcat(str,"' and dport='");
strcat(str,mysql_row1[4]);
strcat(str,"'");
printf("%s\n",str);
if (mysql_query(&mysql_conn, str) != 0) printf("Query error!\n");

mysql_result2 = mysql_store_result(&mysql_conn);
num_row2 = mysql_num_rows(mysql_result2); /* Get the no. of row */
num_col2 = mysql_num_fields(mysql_result2); /* Get the no. of column */

mysql_row2 = mysql_fetch_row(mysql_result2); /* Get a row */

char *str2, *str3;
str2 = (char *)malloc(1024);
str3 = (char *)malloc(1024);
sprintf(str3, "%d", num_row2);
strcpy(str2,"INSERT INTO asso_table (scid,num,aid,proto,dip,dport) VALUES(");
strcat(str2,mysql_row1[0]);strcat(str2,",");
strcat(str2,str3);strcat(str2,",");
strcat(str2,mysql_row1[1]);strcat(str2,",'");
strcat(str2,mysql_row1[2]);strcat(str2,"','");
strcat(str2,mysql_row1[3]);strcat(str2,"','");
strcat(str2,mysql_row1[4]);strcat(str2,"')");
printf("%s\n",str2);
if (mysql_query(&mysql_conn, str2) != 0) printf("Insert error!\n");
}

mysql_free_result(mysql_result1);
mysql_free_result(mysql_result2);
mysql_close(&mysql_conn);

printf("Association analyse type 1 finished\n");

}

void asso(int type){
if (type == 1) asso_1();
}

int main(int argc, char **argv) {
int param;

printf("select association type:\n1: Weekness association\n2:\n3:\nInput number>");
scanf("%d",?m);
asso(param);

return 0;
}

相关内容