Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
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,98 @@ | ||
#include <iostream> | ||
#include <limits> | ||
using namespace std; | ||
#include "libsqlite.hpp" | ||
|
||
//Insert into Function -- Name | ||
bool insertToCombat(int mageHealth, int mageMana,int enemyHealth) | ||
{ | ||
|
||
try | ||
{ | ||
sqlite::sqlite db("testdb.db"); //Connecting to Database | ||
auto cur = db.get_statement(); //Creates a cursor on this connection | ||
|
||
cur->set_sql("INSERT INTO Combat (MageHP, MageMana, EnemyHP) " | ||
"VALUES (?, ?, ?);"); //sql command | ||
cur->prepare(); //Sends to database | ||
cur->bind(1, mageHealth); | ||
cur->bind(2, mageMana); | ||
cur->bind(3, enemyHealth); | ||
cur->step(); | ||
} | ||
|
||
catch (sqlite::exception e) | ||
{ | ||
cerr << e.what() << endl; | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
void showCombatResults() | ||
{ | ||
sqlite::sqlite db( "testdb.db" ); | ||
auto cur = db.get_statement(); | ||
|
||
cur->set_sql("select *" | ||
"from Combat"); //selects the table in order to print out the results | ||
cur->prepare(); | ||
|
||
|
||
while(cur->step()) | ||
{ | ||
cout << "ID: " << cur->get_int(0)<< " |MageHP: " << | ||
cur->get_int(1) << " |MageMana: " << cur->get_int(2) << " |EnemyHP: "<< | ||
cur->get_int(3)<<endl; | ||
} | ||
} | ||
|
||
bool deleteFromCombat() | ||
{ | ||
try //try statement do this unless error occurs | ||
{ | ||
sqlite::sqlite db("testdb.db"); //Connecting to Database | ||
|
||
char question; | ||
cout<<"What would you like to delete from Combat table?\n"<< | ||
"1) Mage losses \n 2) Enemy losses \n 3) Delete everything"<<endl; | ||
cout<<"Choice: "; | ||
cin>>question; | ||
|
||
|
||
if (question == '1'){ | ||
auto cur = db.get_statement(); //Creates a cursor on this connection | ||
cur->set_sql("DELETE " | ||
"FROM Combat " | ||
"WHERE MageHP = 0;"); //sql command | ||
cur->prepare(); //Sends to database | ||
cur->step(); | ||
} | ||
|
||
else if(question == '2'){ | ||
auto cur = db.get_statement(); //Creates a cursor on this connection | ||
cur->set_sql("DELETE " | ||
"FROM Combat " | ||
"WHERE EnemyHP = 0;"); //sql command | ||
cur->prepare(); //Sends to database | ||
cur->step(); | ||
} | ||
|
||
else{ | ||
auto cur = db.get_statement(); //Creates a cursor on this connection | ||
cur->set_sql("DELETE " | ||
"FROM Combat " | ||
"WHERE id > 0;"); //sql command | ||
cur->prepare(); //Sends to database | ||
cur->step(); | ||
} | ||
|
||
} | ||
catch (sqlite::exception e) //Catching the sql error to avoid program from crashing | ||
{ | ||
cerr << e.what() << endl; | ||
return 1; | ||
} | ||
return 0; | ||
} |