Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Database connectivity, and all logic surrounding DB -> Beverage object
- Loading branch information
Showing
6 changed files
with
202 additions
and
281 deletions.
There are no files selected for viewing
132
Sample.cpp
This file was deleted.
Oops, something went wrong.
BIN
+72 KB
cocktail.db
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef SQLITE_H | ||
#define SQLITE_H | ||
|
||
|
||
|
||
#include <sqlite3.h> | ||
#include <vector> | ||
#include <string> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Row { | ||
|
||
public: | ||
|
||
Row(vector<string> data){ | ||
columns = data; | ||
} | ||
|
||
vector<string> getColumns(){ | ||
return columns; | ||
} | ||
|
||
private: | ||
vector<string> columns; | ||
}; | ||
|
||
/* | ||
Code written after following tutorial from https://www.dreamincode.net/forums/topic/122300-sqlite-in-c/ | ||
No code was reused or taken, however the information was applied. | ||
*/ | ||
class Database{ | ||
|
||
public: | ||
|
||
sqlite3 *database; | ||
|
||
Database(string dbName){ | ||
|
||
if( sqlite3_open(dbName.c_str(), &database) ) { // should return 0, or false, if run successfully | ||
error = true; | ||
} else{ | ||
error = false; | ||
} | ||
|
||
} | ||
|
||
void close(){ | ||
sqlite3_close(database); | ||
} | ||
|
||
bool hasError(){ | ||
return error; | ||
} | ||
|
||
vector<Row> query(string statementString){ | ||
|
||
sqlite3_stmt *statement; | ||
vector<Row> rows; | ||
if(sqlite3_prepare_v2(database, statementString.c_str(), -1, &statement, 0) == SQLITE_OK){ // sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail | ||
|
||
int columnCount = sqlite3_column_count(statement); | ||
|
||
while(sqlite3_step(statement) == SQLITE_ROW){ | ||
|
||
vector<string> columns; | ||
|
||
for(int column = 0; column < columnCount; column++){ | ||
columns.push_back(string((const char*)sqlite3_column_text(statement, column))); // cast to 'const char*' so we can call string() function | ||
} | ||
|
||
rows.push_back(Row(columns)); | ||
|
||
} | ||
|
||
sqlite3_finalize(statement); | ||
|
||
|
||
|
||
} | ||
|
||
return rows; | ||
|
||
} | ||
|
||
private: | ||
bool error; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.