Commit feeb42e7 by Andrew Dahl

Added Unfinished Classes SQLite3 and BookDatabase

parent 25760566
/*
* BookDatabase.cpp
*
* Created on: Apr 27, 2010
* Author: dahlan
*/
#include "BookDatabase.h"
BookDatabase::BookDatabase(string tablename)
{
this->db(tablename);
}
bool BookDatabase::addBook(const Book myBook)
{
}
bool BookDatabase::deleteBook(const string isbn)
{
}
bool BookDatabase::updateBook(const Book myBook)
{
}
Book BookDatabase::findBook(const string isbn)
{
}
vector<Book> BookDatabase::getBooks()
{
}
/*
* BookDatabase.h
*
* Created on: Apr 27, 2010
* Author: Andrew Dahl
*/
#ifndef BOOKDATABASE_H_
#define BOOKDATABASE_H_
#include "SQLite3.h"
#include "Book.h"
#include <string>
using namespace std;
const string CREATE_QUERY="CREATE TABLE books(searchable_isbn TEXT PRIMARY KEY, title TEXT, author1 TEXT, author2 TEXT, author3 TEXT, author4 TEXT, publisher TEXT, isbn TEXT, number_of_authors SMALLINT, publication_year INTEGER, stock INTEGER, price FLOAT);";
class BookDatabase
{
public:
BookDatabase(string tablename="books.db");
bool addBook(const Book myBook);
bool deleteBook(const string isbn);
bool updateBook(const Book myBook);
Book findBook(const string isbn);
vector<Book> getBooks();
private:
SQLite3 db;
};
#endif /* BOOKDATABASE_H_ */
/*
* SQLite3.cpp
*
* Created on: Apr 27, 2010
* Author: Andrew Dahl
*/
#include "SQLite3.h"
SQLite3::SQLite3 (string tablename, string createQuery): zErrMsg(0), rc(0),db_open(0)
{
bool db_exists = false;
fstream file;
file.open(tablename,ios::in);
if(file.is_open())
db_exists = true;
this->ret = sqlite3_open(tablename.c_str(), &this->db);
if( this->ret )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(this->db);
this->db_open=false;
}
else
{
this->db_open=true;
if(!db_exists && createQuery != "")
this->execute(createQuery);
}
}
SQLite3::~SQLITE3()
{
sqlite3_close(this->db);
}
int SQLite3::execute(string s_exe)
{
this->ret = sqlite3_get_table(
this->db, /* An open database */
s_exe.c_str(), /* SQL to be executed */
&this->result, /* Result written to a char *[] that this points to */
&this->numRow, /* Number of result rows written here */
&this->numCol, /* Number of result columns written here */
&this->zErrMsg /* Error msg written here */
);
if(this->column_names.size() > 0)
this->column_names.clear();
if(this->column_data.size()>0)
this->column_data.clear();
if( this->ret == SQLITE_OK )
{
for(int i=0; i < this->numCol; ++i)
this->column_names.push_back(result[i]); /* First row heading */
for(int i=0; i < this->numCol*this->numRow; ++i)
this->volumn_data.push_back(this->result[this->numCol+i]);
}
sqlite3_free_table(this->result);
return this->ret;
}
vector<string> SQLite3::getColumnNames() const
{
return this->column_names;
}
vector<string> SQLite3::getColumnValues() const
{
return this->column_data;
}
/*
* SQLite3.h
*
* Created on: Apr 27, 2010
* Author: Andrew Dahl
*/
#ifndef SQLITE3_H_
#define SQLITE3_H_
#include <sqlite3.h>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class SQLite3
{
public:
SQLite3 (string tablename, string createQuery = "");
~SQLite3();
int execute(string s_exe);
vector<string> getColumnNames() const;
vector<string> getColumnValues() const;
private:
sqlite3 *db;
char *zErrMsg;
char **result;
int ret,
numRow,
numCol;
bool db_open;
vector<string> column_names;
vector<string> column_data;
};
#endif /* SQLITE3_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment