Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Old-School-Game/db.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
94 lines (58 sloc)
1.56 KB
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
#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 |