· 8 years ago · Apr 10, 2018, 12:48 AM
1//g++ accounting2.cpp -lsqlite3 -o accountingy -Wall -std=c++11
2
3#include <iostream>
4#include <string>
5
6//adding sqlite stuff below
7#include <sqlite3.h>
8using namespace std;
9
10static int callback(void *data, int argc, char **argv, char **azColName){
11 int i;
12 fprintf(stderr, "%s: ", (const char*)data);
13 for(i=0; i<argc; i++){
14 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
15 }
16 printf("\n");
17 return 0;
18}
19
20struct Transaction{
21 double money;
22 char satisfied;
23 string description;
24 } first;
25
26void printit ();
27
28int main(){
29 int entries;
30
31 cout << "How many entries?: " << endl;
32 cin>> entries;
33 for (int i=0; i< entries; i++){
34 cout << "How much did you spend?: " << endl;
35 cin >> first.money;
36 cout << "Satisfied? No, Meh, Yes [N/M/Y]?: " << endl;
37 cin >> first.satisfied;
38 cout << "Description?: " << endl; //wont work
39 cin.ignore(); //stop taking in cin stuff from before
40 getline(cin,first.description);
41 }
42 //SQLITE STUFF
43 string lineExpense = "INSERT INTO purchases(cost, description, satisfied) VALUES ('" + to_string(first.money) + "', "
44 ""+ first.description + ", " + to_string(first.satisfied) + ")"; //OOPS?
45
46
47 const char *statement = lineExpense.c_str(); //you need to make it turn into a cstring so sql can format/read it!
48
49 sqlite3 *db;
50 char *szErrMsg = 0;
51
52 // open database/save it
53 int rc = sqlite3_open("purchases.sqlite", &db);
54
55 //open db check
56 if (rc) {
57 cout << "Can't open database\n";
58 } else {
59 cout << "Open database successfully\n";
60 }
61
62 //auto run SQL commands
63 const char *pSQL[6];
64 pSQL[0] = "CREATE TABLE IF NOT EXISTS purchases(id INTEGER PRIMARY KEY "
65 "AUTOINCREMENT NOT NULL, logged TIMESTAMP DEFAULT "
66 "CURRENT_TIMESTAMP NOT NULL, cost REAL, description VARCHAR(40), " //OOPS
67 "satisfied VARCHAR(1))";
68
69 pSQL[1] = statement;
70 pSQL[2] = "SELECT * FROM purchases";
71 pSQL[3] = "SELECT sum(satisfied) FROM purchases";
72
73
74
75 // execute sql
76 for (int i = 0; i <= 5; i++) {
77 rc = sqlite3_exec(db, pSQL[i], callback, 0, &szErrMsg);
78
79 if (rc != SQLITE_OK) {
80 cout << "SQL Error: " << szErrMsg << endl;
81 sqlite3_free(szErrMsg);
82 break;
83 }
84 }
85
86 // close database
87 if (db) {
88 sqlite3_close(db);
89 }
90
91 printit();
92
93return 0;
94}
95
96void printit(){
97
98}