-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.h
More file actions
37 lines (34 loc) · 1.04 KB
/
Copy pathDatabase.h
File metadata and controls
37 lines (34 loc) · 1.04 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
#pragma once
#include <iostream>
#include "storage/lib/sqlite_modern_cpp.h" // importing sqlite db
#include "Product.h"
#include <string>
using std::cin;
using std::cout;
std::optional<sqlite::database> createConnection()
{
try
{
return sqlite::database("storage/inventory.sql"); // opens or creates "inventory.db"
}
catch (std::exception &e)
{
std::cerr << "DB connection failed" << "\n";
return std::nullopt; // to indicate that optional wrapper object does not have value
}
}
bool createStructure()
{
std::optional<sqlite::database> db = createConnection();
if (!db)
{
cout << "Error Establishing (DB) Connection.";
return false;
}
else
{
std::string create_structure_query = "CREATE TABLE IF NOT EXISTS inventory(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, price INTEGER, quantity FLOAT )";
*db << create_structure_query; // as 'db' is optional and to get the actual from optional (wrapper), dereference operator is needed
return true;
}
}