一、源码(doMysql.c)
#include <mysql.h>
#include <stdio.h>
#include <string.h>
/*******************************************************
** 函数: ReadFileToDB
** 功能: 逐行读取文件数据,并插入数据到数据库
** 参数: pszFilePath -- 文件路径
** pMysql -- 数据库句柄
** 返回值: 读取失败,返回-1,否则返回0
*/
int ReadFileToDB(const char *pszFilePath, MYSQL *pMysql)
{
FILE *pFile = NULL;
char strBuf[1024];
char strSql[1024];
pFile = fopen(pszFilePath, "r");
if(pFile == NULL)
{
printf("fail to read file %s .\r\n", pszFilePath);
return -1;
}
mysql_query(pMysql, "BEGIN"); //开启事务
memset(strBuf, 0, sizeof(strBuf));
while(fgets(strBuf, sizeof(strBuf), pFile) != NULL)
{
//printf("%s ", strBuf);
if(strlen(strBuf)

本文介绍了如何在Linux环境下使用C语言编写的doMysql.c程序,从文件中读取数据并批量插入到MySQL数据库中。程序通过开启事务,逐行读取文件内容并构造SQL插入语句,最后提交事务完成数据导入。
最低0.47元/天 解锁文章
1184

被折叠的 条评论
为什么被折叠?



