#include <head.h>
int main(int argc, const char *argv[])
{
sqlite3 *db = NULL;
if (sqlite3_open("sql.db", &db) != SQLITE_OK)
{
fprintf(stderr, "line:%d sqlite3_open:%s\n", __LINE__, sqlite3_errmsg(db));
return -1;
}
char sql[128] = "create table if not exists dics (word_s char,explain_s char);";
char *errmsg = NULL;
if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "line:%d sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
FILE *fd = fopen("dict.txt", "r");
if (fd == NULL)
PRINT_ERR("fopen");
fseek(fd, 0, SEEK_END);
int fileLen = ftell(fd);
fseek(fd, 0, SEEK_SET);
char word[40];
char explain[60];
char buf[100];
int i;
int count = 0;
while (1)
{
if (fgets(buf, sizeof(buf), fd) == NULL)
break;
count+=strlen(buf);
buf[strlen(buf) - 1] = 0;
for (i = 0; i < strlen(buf); i++)
{
if (buf[i] == ' ' && buf[i + 1] == ' ')
{
buf[i] = 0;
strcpy(word, buf);
break;
}
}
for (int j = i + 1; j < strlen(buf) + i + 1; j++)
{
if (buf[j] != ' ')
{
strcpy(explain, buf + j);
break;
}
}
sprintf(sql, "INSERT INTO dics VALUES (\"%s\",\"%s\");", word, explain);
if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "line:%d sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("读取中..%%%.1f\r", (float)count /(fileLen/(100)));
fflush(stdout);
}
sqlite3_close(db);
printf("录入成功\n");
return 0;
}
过程要10秒左右