day7
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
day7_网络编程
将文件内容拷贝到数据库
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sqlite3.h>
int main(int argc, const char *argv[])
{
// 打开数据库
sqlite3 *db = NULL;
if (sqlite3_open("./dict.db", &db) != SQLITE_OK)
{
fprintf(stderr, "sqlite3_open:%s %d\n", sqlite3_errmsg(db), __LINE__);
return 0;
}
// 创建数据表
char *errmsg = NULL;
char sql[128] = "create table if not exists world (english char,china char);";
if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "sqlite3_exec:%s %d\n", sqlite3_errmsg(db), __LINE__);
return 0;
}
FILE *fd = fopen("./dict.txt", "r");
if (fd == NULL)
{
perror("fopen");
return 0;
}
char buf[128] = "";
char world[128] = "";
char china[128] = "";
while (1)
{
int flag = 0;
memset(buf, 0, sizeof(buf));
memset(world, 0, sizeof(world));
memset(china, 0, sizeof(china));
if (fgets(buf, sizeof(buf), fd) == NULL)
break;
buf[strlen(buf) - 1] = '\0';
char *ptr = buf;
char *w = world;
char *c = china;
while (*ptr != ' ')
{
*w = *ptr;
w++;
ptr++;
}
while (*ptr != '\0')
{
*c = *ptr;
c++;
ptr++;
}
memset(sql, 0, sizeof(sql));
sprintf(sql, "insert into world values (\"%s\",\"%s\");", world, china);
char *errmsg = NULL;
if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "sqlite3_exec:%s %d\n", errmsg, __LINE__);
return 0;
}
}
// 关闭数据库
if (sqlite3_close(db) != SQLITE_OK)
{
fprintf(stderr, "sqlite3_close:%s %d\n", sqlite3_errmsg(db), __LINE__);
}
fclose(fd);
return 0;
}
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |