Skip to content
Permalink
Browse files
Database connectivity, and all logic surrounding DB -> Beverage object
  • Loading branch information
grantb3 committed Feb 25, 2019
1 parent da223c8 commit d8fd94e87ea1f6dfba58b9e12d3acbcb4db4b5d3
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 281 deletions.

This file was deleted.

This file was deleted.

BIN +72 KB cocktail.db
Binary file not shown.
94 db.h
@@ -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

0 comments on commit d8fd94e

Please sign in to comment.