include iostream include unistd include sqlite3 include cstring define

 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
#include <iostream>
#include <unistd.h>
#include <sqlite3.h>
#include <cstring>
#define print_error(pdb) {\
std::cout << "[ERROR]"<< __LINE__ <<\
"(" << sqlite3_errcode(pdb) << "): " <<\
sqlite3_errmsg(pdb) << std::endl;}
bool init_tables(sqlite3 *pdb)
{
const char * user_query =
"CREATE TABLE 'dict' (\n\
'id' INTEGER NOT NULL PRIMARY KEY,\n\
'word' VARCHAR(128) NOT NULL,\n\
'transl' VARCHAR(128) NOT NULL,\n\
'word2' VARCHAR(128) NOT NULL);";
if(sqlite3_exec(pdb, user_query, NULL, NULL, NULL) == SQLITE_ERROR)
{
print_error(pdb);
return false;
}
return true;
}
char * addLin (char*, char*);
int SizeLine (char*);
int main(int argc, char** argv)
{
sqlite3 *pdb = NULL;
unlink(argv[1]);
char * lin ="./";
char * LaddL= addLin(lin, argv[1]);
if(sqlite3_open(LaddL, &pdb) != SQLITE_OK)
{
std::cout << sqlite3_errmsg(pdb) << std::endl;
return 1;
}
if(init_tables(pdb) == false)
{
std::cout << sqlite3_errmsg(pdb) << std::endl;
return 1;
}
sqlite3_close(pdb);
delete [] LaddL;
return 0;
}
int SizeLine (char* lin)
{
int siz=0;
while(lin[siz] !='\0')
{
siz++;
}
return siz;
}
char * addLin (char* lin1, char* lin2)
{
int siz1, siz2;
siz1= SizeLine(lin1);
siz2= SizeLine(lin2);
char * temp = new char [siz1+siz2+1];
std::memcpy(temp, lin1, siz1);
std::memcpy(temp+siz1, lin2, siz2);
temp[siz1+siz2]= '\0';
return temp;
}