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?
asciiGame/sql.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (35 sloc)
1.21 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
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sqlite3.h> | |
#include <vector> | |
using namespace std; | |
std::vector<std::string> pdata; | |
bool vformat; | |
static int callback(void* x, int columns, char** data, char** column) // callback acts as the cursor | |
{ | |
for(int i = 0; i < columns; i++) // columns is the number of columns selected and allows iteration | |
{ | |
std::string a = column[i]; // column[i] is the name of the column | |
std::string b = data[i]; // data[i] is the actual data in that column | |
if (vformat == true) | |
{ | |
pdata.push_back(a + "=" + b); // this line adds the column name, an equals and the data in that column to the vector | |
} | |
else if (vformat == false) | |
{ | |
pdata.push_back(b); // this line only adds the data if the user needs a clean vector | |
} | |
} | |
return 0; | |
} | |
std::vector<std::string> sql_call(std::string query, bool format = true) // the argument is the sql query and if the vector is to be formatted or not | |
{ | |
vformat = format; | |
sqlite3 *db; | |
sqlite3_open("data.db", &db); // this opens the specified database | |
//query = ""; | |
sqlite3_exec(db, query.c_str(), callback, 0, NULL); // this line executes the given query | |
sqlite3_close(db); | |
return pdata; | |
} |