Repository navigation
Expand file tree
/
Copy pathcsv_sqlite.cc
More file actions
112 lines (91 loc) · 2.26 KB
/
Copy pathcsv_sqlite.cc
File metadata and controls
112 lines (91 loc) · 2.26 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "csv.hh"
#include "clock.hh"
#include <iostream>
#include "sqlite3.h"
int csv_to_sqlite(const std::string &name);
/////////////////////////////////////////////////////////////////////////////////////////////////////
//usage
/////////////////////////////////////////////////////////////////////////////////////////////////////
void usage()
{
std::cout << "usage: ./csv_sqlite -f CSV" << std::endl;
std::cout << "-f CSV: CSV file name" << std::endl;
exit(0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//main
/////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
std::string file_name;
if (argc < 2)
{
usage();
}
for (int i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case 'h':
usage();
break;
case 'f':
file_name = argv[i + 1];
i++;
break;
default:
usage();
}
}
else
{
usage();
}
}
if (file_name.empty())
{
return 1;
}
if (csv_to_sqlite(file_name) < 0)
{
return 1;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//dc311_csv_to_sqlite
//SQLite does not have a storage class set aside for storing dates and/or times.
//Instead, the built-in Date And Time Functions of SQLite are capable of
//storing dates and times as TEXT, REAL, or INTEGER values:
/////////////////////////////////////////////////////////////////////////////////////////////////////
int csv_to_sqlite(const std::string &name)
{
clock_gettime_t timer;
read_csv_t csv;
size_t nbr_rows = 0;
std::vector<std::string> row;
std::string sql;
sqlite3 *db = 0;
char *msg = 0;
if (csv.open(name) < 0)
{
std::cout << "Cannot open file " << name.c_str() << std::endl;
return -1;
}
timer.start();
std::string fname(name);
fname += ".sqlite";
if (sqlite3_open(fname.c_str(), &db) != SQLITE_OK)
{
sqlite3_errmsg(db);
sqlite3_close(db);
return -1;
}
sqlite3_close(db);
std::cout << "processed " << nbr_rows << " rows" << std::endl;
timer.now();
timer.stop();
return 0;
}