-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencsv.c
More file actions
53 lines (45 loc) · 1.51 KB
/
opencsv.c
File metadata and controls
53 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/include/sqlite3.h"
int opencsv(char *datafile) {//csvファイルからテストデータ展開
//x_location,y_location,score
//1000x1000,100
sqlite3 *conn = NULL;
sqlite3_stmt *stmt = NULL;
FILE *fp;
int x_location, y_location, score;
int ret = 0;
const char *insert_to_point_table_sql = "insert into point_table(x_location, y_location, score) values(?, ?, ?)";
fp = fopen(datafile,"r");//テストデータ展開
if (fp == NULL) {
printf("[%s] is not open!\n",datafile);
return -1;
}else{
printf("[%s] is opened!\n",datafile);
}
ret = sqlite3_open("study.sqlite", &conn);//SQLite3に接続
if(ret != SQLITE_OK){
printf("database is not open!\n");
return -1;
}
sqlite3_exec(conn, "delete from point_table;", NULL, NULL, NULL);//データをリセット
sqlite3_prepare(conn, insert_to_point_table_sql, strlen(insert_to_point_table_sql), &stmt, NULL);
//ファイルの読み込み
while(fscanf(fp,"%d,%d,%d", &x_location, &y_location, &score)!=EOF) {
//printf("%d,%d,%d\n", x_location, y_location, score);
//データベース(point_table)に値を格納
sqlite3_reset(stmt);
//変数を設定
sqlite3_bind_int(stmt, 1, x_location);
sqlite3_bind_int(stmt, 2, y_location);
sqlite3_bind_int(stmt, 3, score);
//SQL文の実行
while(SQLITE_DONE != sqlite3_step(stmt)){}
}
//終了処理
fclose(fp);
sqlite3_finalize(stmt);
sqlite3_close(conn);
return 0;
}