linux下批量新增数据,linux下批量插入数据到mysql

一、源码(doMysql.c)

#include

#include

#include

/*******************************************************

**  函数:   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) > 10)

{

memset(strSql, 0, sizeof(strSql));

sprintf(strSql, "insert into documents(group_id, date_added, content)values(1, now(), '%s')", strBuf);

//printf("strSql: %s \n", strSql);

mysql_real_query(pMysql, strSql, strlen(strSql));

}

memset(strBuf, 0, sizeof(strBuf));

}

//printf("\r\n");

mysql_query(pMysql, "COMMIT");//提交事务

fclose(pFile);

return 0;

}

/******************************************************

** 函数:   PrintMysqlRes

** 功能:   打印结果集

** 参数:   pRes  --- mysql返回的结果集

** 返回值: 无

*/

void PrintMysqlRes(MYSQL_RES *pRes)

{

MYSQL_ROW  row = NULL;//每条记录

int i = 0;

int nResSetCol = 0; //每条记录的列数

if(pRes == NULL)

{

printf("fail to print, result set is null. \r\n");

return;

}

//mysql_num_rows(res)获取结果集的行数

nResSetCol = (int)mysql_num_fields(pRes);

row = mysql_fetch_row(pRes);

while(row != NULL)

{

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

{

printf("%s ",row[i]);

}

printf("\r\n");

row = mysql_fetch_row(pRes);

}

}

int main(void)

{

MYSQL        *pMysql;

MYSQL_RES    *pRes;

const char   *pszServerIP = "localhost";

const char   *pszUserName = "root";

const char   *pszPassWord = "";

const char   *pszDatabaseName = "test";

const char   *pszQuerySql = "select * from documents";

const char   *pszFilePath = "1.txt";

int i = 0;

pMysql = mysql_init(NULL);

//连接数据库

if(mysql_real_connect(pMysql, pszServerIP, pszUserName, pszPassWord, pszDatabaseName, 0, NULL, 0) == NULL)

{

printf("fail to connect database: %s , error:[%s].\r\n", pszDatabaseName, mysql_error(pMysql));

}

else

{

printf("connected Ok! \r\n");

}

//插入数据

ReadFileToDB(pszFilePath, pMysql);

//打印结果

/*if(mysql_real_query(pMysql, pszQuerySql, strlen(pszQuerySql)))

{

printf("fail to query, error:[%s].\r\n", mysql_error(pMysql));

}

else

{

pRes = mysql_store_result(pMysql);

PrintMysqlRes(pRes);

mysql_free_result(pRes);

}  */

mysql_close(pMysql);

return 0;

}

二、makefile文件内容

# Makefile for mysql test.CC     := gcc

# Include paths(mysql.h)INCS := -I/usr/local/mysql/include/

# Lib paths(libmysqlclient.so)LIB := -L/usr/local/mysql/lib/ -lmysqlclient

# Targets of the buildOUTPUT := Main

# Source files

SRCSOBJS := doMysql.c

# common rules

${OUTPUT} : ${SRCSOBJS}

${CC} ${SRCSOBJS} ${INCS} ${LIB} -o ${OUTPUT}  #注意此处开头是1个tab键

clean:

-rm -f ${OUTPUT}  #注意此处开头是1个tab键

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值