/**************************************************************************
*****Programm Name:mysql.cpp
*****Programmer :Li
*****Function :mysql数据库存储过程简单示例
*****Project :Crawler.src
****************************************************************************/
/***************************************************************************
***#mysql 存储过程 包括创建、删除、插入、修改 等操作
***CREATE TABLE urlindex(id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
***host VARCHAR(50) NOT NULL,DNS VARCHAR(50) NOT NULL)
***file name :mysql.txt
*****************************************************************************/
// g++ -g mysql.cpp -o mysql -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz
#include
#ifdef __cplusplus
extern "C"{
#endif
#include
#include
#include
#include
#define SQLCONF "/root/wangxin/mysql.txt"
#ifdef __cplusplus
}
#endif
int main(void){ MYSQL mysql; //MYSQL *mysql; char table[255]; char *p; FILE *f; memset(table,'0',sizeof(table)); mysql_init(&mysql); #ifndef DEBUG printf("1111/n"); #endif if(!(mysql_real_connect(&mysql,"localhost","root","songjun","indexdb",0,NULL,0))) { fprintf(stderr,"Error in connection:%s,%s/n",mysql_error(&mysql),__FUNCTION__); exit(1); } #ifdef DEBUG printf("2222/n"); #endif if((f=fopen(SQLCONF,"r"))==NULL) { fprintf(stderr,"Can t open %s/n",SQLCONF); return 0; } #ifdef DEBUG printf("3333/n"); #endif while(fgets(table,sizeof(table),f)) { p=table; printf("%s/n",p); while(isspace((int)*p)) p++; if(!strncmp(p,"#",1)) continue; else break; } #ifdef DEBUG printf("%s/n",table); #endif int ret=mysql_real_query(&mysql,table,strlen(table)); if(ret==0) printf("ok"); else printf("error");}