Mysql进程多数据库_多进程插入mysql数据库

#include

#include

#include

#include

#include

#include

#include

#include

#define DB_HOST "127.0.0.1"

#define DB_USER "root"

#define DB_PASS ""

#define DB_NAME "test"

#define DB_PORT 3307

int main(int argc, char **argv)

{

MYSQL mysql;

unsigned int i;

int rtn; /*子进程的返回数值*/

unsigned int process_no;

unsigned int userId;

unsigned int type;

unsigned int itemCate;

unsigned int itemType;

unsigned int itemNum;

unsigned int totalCost;

unsigned int consumeTime;

unsigned int leftBalance;

char itemName[1000] ;

char sql[20000] ;

struct timeval time_start,time_end;

float execute_time;

//srand(time(0));

for(process_no = 1; process_no < 18; process_no++)

{

if(fork() == 0)

{

// 开始执行时间

gettimeofday(&time_start,NULL);

mysql_init(&mysql);

if(!mysql_real_connect(&mysql,DB_HOST,DB_USER,DB_PASS,DB_NAME,DB_PORT,NULL,0))

{

fprintf(stderr, "Failed to connect to database: Error: %s\n",mysql_error(&mysql));

}

mysql_query(&mysql,"SET NAMES utf8");

for(i = 0; i < 444444; i++)

{

sprintf(itemName,"%s","");

userId = rand()%19071388 + 1;

type = rand()%4 + 1;

itemCate = rand()%15 + 1;

itemType = rand()%100 + 1;

itemNum = rand()%100 + 1;

totalCost = rand()%10000 + 1;

consumeTime = rand()%1309120853 + 1;

leftBalance = rand()%10000 + 1;

sprintf(sql,"insert into user_consume1(userId,type,itemCate,itemType,itemNum,totalCost,consumeTime,leftBalance,itemName)values(%d,%d,%d,%d,%d,%d,%d,%d,'')",

userId,type,itemCate,itemType,itemNum,totalCost,consumeTime,leftBalance);

if(mysql_query(&mysql,sql))

{

fprintf(stderr,"query failed:%s",mysql_error(&mysql));

}

}

mysql_close(&mysql);

// 结束执行时间

gettimeofday(&time_end,NULL);

execute_time = (time_end.tv_sec - time_start.tv_sec) + (time_end.tv_usec - time_start.tv_usec)/1000000;

printf("execute time:%f\n",execute_time);

exit(0);

}

else

{

/* 父进程, 等待子进程结束,并打印子进程的返回值 */

//wait ( &rtn );

//printf("child process return %d\n",rtn);

//return 0;

//printf("execute child process return %d\n",process_no);

}

}

return 0;

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-05-09 11:29

浏览 1263

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值