insert_test.c

来自「本原码为为《嵌入式Linux应用开发详解》一书的配套代码」· C语言 代码 · 共 85 行

C
85
字号
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>#include <libmsql/msql.h>#define INSERT_QUERY "insert into test (name,num) values ('item %d', %d)"#define VERBOSEint main(argc,argv)	int	argc;	char	*argv[];{	unsigned int count, num;	int	sock,		oldTime,		curTime,		cur;	char	qbuf[160],		*host = NULL,		*db,		*qty;		if (argc != 3 && argc != 5)	{		printf("usage : insert_test [-h host] <dbname> <Num>\n\n");		exit(1);	}	if (argc == 5)	{		host = argv[2];		db = argv[3];		qty = argv[4];	}	else	{		host = NULL;		db = argv[1];		qty = argv[2];	}	if ((sock = msqlConnect(host)) < 0)	{		printf("Couldn't connect to engine!\n%s\n\n", msqlErrMsg);		perror("");		exit(1);	}	if (msqlSelectDB(sock,db) < 0)	{		printf("Couldn't select database %s!\n%s\n",argv[1],msqlErrMsg);	}	num = atoi(qty);	count = 0;	cur = 0;	oldTime = time(NULL);	while (count < num)	{		snprintf(qbuf,sizeof(qbuf),INSERT_QUERY,count,count);		if(msqlQuery(sock,qbuf) < 0)		{			printf("Query failed for count=%d(%s)\n",count,				msqlErrMsg);			printf("Inserted %d rows\n",count);			exit(1);		}		count++;		cur++;		if (cur == 5000)		{#ifdef VERBOSE			curTime = time(NULL);			printf("%d  (%d seconds)\n",count, curTime - oldTime);			oldTime = curTime;#endif			cur = 0;		}	}	msqlClose(sock);	exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?