Skip to content

Commit 63cc7ed

Browse files
committed
Create user database
1 parent a1483a3 commit 63cc7ed

File tree

6 files changed

+53
-28
lines changed

6 files changed

+53
-28
lines changed

src/dbstore.cpp

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#include "misc.h"
2+
#include "print.h"
13
#include <sqlite3.h>
4+
#include <sstream>
25
#include <string>
36
using namespace std;
47

@@ -20,16 +23,46 @@ int database_test()
2023
return 0;
2124
}
2225

23-
int create_database_table()
26+
static int callback(void* NotUsed, int argc, char** argv, char** azColName)
2427
{
28+
int i;
29+
for (i = 0; i < argc; i++) {
30+
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
31+
}
32+
printf("\n");
33+
return 0;
34+
}
35+
36+
int create_user_database(string player_name)
37+
{
38+
sqlite3* db;
39+
char* zErrMsg = 0;
40+
int rc;
2541
char* sql;
42+
43+
player_name = to_lower_case(player_name);
44+
string databaseName = "save-" + player_name + ".db";
45+
46+
// Open database
47+
rc = sqlite3_open(databaseName.c_str(), &db);
48+
if (rc) {
49+
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
50+
return (0);
51+
}
52+
// Create SQL statement
2653
sql = (char*)"CREATE TABLE INVENTORY("
54+
"ID INT NOT NULL PRIMARY KEY,"
55+
"NAME TEXT NOT NULL,"
56+
"GOT BOOLEAN NOT NULL"
2757
");";
28-
}
2958

30-
int create_database(string user)
31-
{
32-
// Create database called [user].db
59+
// Execute SQL statement
60+
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
61+
if (rc != SQLITE_OK) {
62+
fprintf(stderr, "SQL error: %s\n", zErrMsg);
63+
sqlite3_free(zErrMsg);
64+
}
65+
sqlite3_close(db);
3366
return 0;
3467
}
3568

src/dbstore.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <string>
12
using namespace std;
23

3-
int database_test();
4+
int database_test();
5+
int create_user_database(string player_name);

src/game.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,15 @@ using namespace std;
1010

1111
int main()
1212
{
13-
//database_test();
14-
/*
1513
print("Name: ", "", false);
1614
string name = "";
17-
name = get_name();
15+
name = get_text();
1816
std::cout << "Hello, " << name << "!\n";
19-
*/
2017

21-
/*
22-
print("red test", "red");
23-
print("blue test", "blue");
24-
print("green test", "green");
25-
print("yellow test", "yellow");
26-
print("magenta test", "magenta");
27-
print("cyan test", "cyan");
28-
print();
29-
*/
18+
create_user_database(name);
3019

3120
print("\n" + get_ascii_title(), "cyan");
3221

33-
// Database load/create needs to be implemented
34-
3522
print("Do you want to play Cosmic Horizon? ");
3623
bool choice;
3724
choice = yes_or_no();

src/misc.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
#include <fstream>
2-
#include <iostream>
1+
#include <algorithm>
2+
#include <locale>
33
#include <string>
44
using namespace std;
55

6+
string to_lower_case(string data)
7+
{
8+
transform(data.begin(), data.end(), data.begin(), (int (*)(int))tolower);
9+
return data;
10+
}
11+
612
string get_ascii_title()
713
{
814
return ""

src/misc.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string>
22
using namespace std;
33

4+
string to_lower_case(string original);
45
string get_ascii_title();

src/userin.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ string get_text()
2525
{
2626
string textIn = "";
2727
cin >> textIn;
28-
if (valid_name(textIn) == true) {
29-
return textIn;
30-
} else {
31-
return "";
32-
}
28+
return textIn;
3329
}
3430

3531
bool yes_or_no()

0 commit comments

Comments
 (0)