-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertOnTable.cpp
More file actions
65 lines (48 loc) · 1.6 KB
/
InsertOnTable.cpp
File metadata and controls
65 lines (48 loc) · 1.6 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
#include <iostream>
#include <sqlite3.h>
#include <string>
using namespace std;
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char **argv)
{
sqlite3* DB;
char *messaggeError;
int exit = sqlite3_open("example.db", &DB);
string query = "SELECT * FROM PERSON;";
cout << "STATE OF TABLE BEFORE INSERT" << endl;
sqlite3_exec(DB, query.c_str(), callback, NULL, NULL);
string sql("INSERT INTO PERSON VALUES(1, 'STEVE', 'GATES', 30, 'PALO ALTO', 1000.0);" \
"INSERT INTO PERSON VALUES(2, 'BILL', 'ALLEN', 20, 'SEATTLE', 300.22);" \
"INSERT INTO PERSON VALUES(3, 'PAUL', 'JOBS', 24, 'SEATTLE', 9900.0);");
exit = sqlite3_exec(DB, sql.c_str(), NULL, 0, &messaggeError);
if(exit != SQLITE_OK)
{
std::cerr << "Error Insert" << std::endl;
sqlite3_free(messaggeError);
}
else
std::cout << "Records created Successfully!" << std::endl;
cout << "STATE OF TABLE AFTER INSERT" << endl;
sqlite3_exec(DB, query.c_str(), callback, NULL, NULL);
sql = "DELETE FROM PERSON WHERE ID = 2;";
exit = sqlite3_exec(DB, sql.c_str(), NULL, 0, &messaggeError);
if(exit != SQLITE_OK)
{
std::cerr << "Error DELETE" << std::endl;
sqlite3_free(messaggeError);
}
else
std::cout << "Record deleted Successfully!" << std::endl;
cout << "STATE OF TABLE AFTER DELETE OF ELEMENT" << endl;
sqlite3_exec(DB, query.c_str(), callback, NULL, NULL);
sqlite3_close(DB);
return(0);
}