I wanted to download before this sqlite3 but I had a LOT of steps to follow and I noticed that I had winsqlite3 already in visual studio somehow. So i included as suggested by visual studio winsqlite/winsqlite3.h
At first I had 10 linking errors for sqlite3_close and other functions like this, and I played with the library inclusion and now I think I messed it up even more? Now when I try to run it I get LNK1104 cannot open file 'winsqlite3.h'. I checked and it's in the external dependencies file but I don't know what else to do. I'm thinking maybe I need to look at library directories and stuff but last I tried that I got rid of those errors and then went back to inherit from project default.
#include <winsqlite/winsqlite3.h>
void DataBaseRepo::WriteToFile()
{
sqlite3 *db;
int rc = sqlite3_open("database.db", &db);
if (rc != SQLITE_OK)
{
throw FileException("The database could not be opened!");
sqlite3_close(db);
return;
}
createTable(db);
for (auto tc : this->coats)
{
addToDataBase(db, tc);
}
sqlite3_close(db);
}
void DataBaseRepo::ReadFromFile()
{
sqlite3* db;
sqlite3_stmt* stmt;
int rc = sqlite3_open("database.db", &db);
if (rc != SQLITE_OK)
{
throw FileException("The database could not be opened!");
sqlite3_close(db);
return;
}
int r = sqlite3_prepare_v2(db, "SELECT * FROM Coats", -1, &stmt, NULL);
if (r != SQLITE_OK)
{
throw FileException("Error preparing statement");
}
while ((r = sqlite3_step(stmt)) == SQLITE_ROW)
{
string size = (const char*)sqlite3_column_text(stmt, 0);
string color = (const char*)sqlite3_column_text(stmt, 1);
float price = stof((const char*)sqlite3_column_text(stmt, 2));
int quantity = stoi((const char*)sqlite3_column_text(stmt, 3));
string photo = (const char*)sqlite3_column_text(stmt, 4);
TrenchCoat tc = TrenchCoat(size, color, price, quantity, photo);
this->coats.push_back(tc);
}
}
void DataBaseRepo::createTable(sqlite3 *db)
{
const char* sql = "CREATE TABLE Coats("
"CoatSize TEXT NOT NULL"
"CoatColor TEXT NOT NULL"
"Price REAL NOT NULL"
"Quantity INTEGER NOT NULL"
"PhotoLink TEXT NOT NULL"
"CONSTRAINT pk_coatID PRIMARY KEY (CoatSize, CoatColor))";
int result = sqlite3_exec(db, sql, 0, 0, 0);
if(result != SQLITE_OK)
throw FileException("Error creating database in file");
}
void DataBaseRepo::addToDataBase(sqlite3 *db, TrenchCoat tc)
{
const char* sql = "INSERT INTO Coats (CoatSize, CoatColor, Price, Quantity, PhotoLink) VALUES (?,?,?,?,?)";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
throw FileException("Error preparing statement");
//Binding parameters
if (sqlite3_bind_text(stmt, 1, tc.getSize().c_str(), -1, NULL) != SQLITE_OK)
throw StatementException();
if (sqlite3_bind_text(stmt, 2, tc.getColor().c_str(), -1, NULL) != SQLITE_OK)
throw StatementException();
if(sqlite3_bind_double(stmt, 3, tc.getPrice()) != SQLITE_OK)
throw StatementException();
if (sqlite3_bind_int(stmt, 4, tc.getQuantity()) != SQLITE_OK)
throw StatementException();
if (sqlite3_bind_text(stmt, 5, tc.getPhotography().c_str(), -1, NULL) != SQLITE_OK)
throw StatementException();
//Executing the statement
int result = sqlite3_step(stmt);
if (result != SQLITE_DONE)
throw FileException("Error inserting new Coat in the database");
sqlite3_finalize(stmt);
}
I don't know if the code has any relevance but it might help
also the header part:
#include <winsqlite/winsqlite3.h>
class DataBaseRepo : public Repository {
public:
DataBaseRepo(std::string filename, bool read) : Repository(filename, read) {}
private:
string filename = "database.db";
void WriteToFile();
void ReadFromFile();
void createTable(sqlite3 *db);
void addToDataBase(sqlite3* db, TrenchCoat tc);
};