c 建mysql表,如何在C ++中创建一个简单的数据库

Hello everyone,

I'm working on a project about simulating a database in c++ and this is what I have to do:

-create/delete a new table (CREATE TABLE ...) - table data can have the following types: INT, DOUBLE, STRING, BOOL, DATE;

-modify a table by adding/removing columns (ALTER TABLE ...)

-select rows (SELECT ... FROM ... WHERE ...)

-delete rows (DELETE ...)

-adding new rows in a table (INSERT INTO ... VALUES ...)

-sort table data (SORT BY ... ASC/DESC)

-print database data

I have to use files for backup and STL containers and design patterns.

This is a Database class in which I will store all the tables created.class Database

{

private:

string dname;

static vector

public:

Database(){};

Database(string &s) { dname = s; }

Database(string &, vector

void addTable(string &s) { db.push_back(&Table(s)); };

void addTable(string &s, vector &v) { db.push_back(&Table(s, v)); };

void removeTable(string &);

int searchForTable(string &);

string getDname() { return dname; };

const vector

void setDname(string &s) { dname = s; }

void setDb(vector

};

This is a Table class in which I will store all the elements.

class Table

{

private:

string tname;

vector trows;

public:

Table(){};

Table(string &n) { tname = n; }

Table(string &, vector);

void setTname(string &s) { tname = s; };

void setTrows(vector &);

const string getTname() { return tname; };

const vector & getTrows() { return trows; };

void addElement(Element &e) { trows.push_back(&e); }

void removeElement(Element &);

int searchForElement(Element &);

void outputTable(ostream &, Table &) const;

Table makeTableFromFile(string &);

};

This is an Element class in which I will store each field.

class Element

{

private:

int uid;

vector efields;

public:

Element();

Element(int, vector);

const int& getUid() { return uid; };

const vector & getEfields() { return efields; };

void setUid(int &u) { uid = u; };

void setEfields(vector &);

void outputElement(ostream &, Element &) const;

};

And the Field class.

template

class Field

{

private:

Type value;

public:

void set(Type &x) { this->value = x; };

void outputField(ostream &, Type &) const;

};

I don't really know how to implement this field class because it could have many types and the vector from the Element class can't store more then one type data.

Could you give me some hints about the overall implementation? Is it a good start?

解决方案One possibility is using something similar to the VARIANT[^] datatype, that is struct containing a union of the supported datatypes plus a field containing the actual type stored in the union itself.

You could also have a look at avaliable database implementation sources, like, for instance, SQLite one.

You could have your Field class inherit from a common non-templated base class and only store pointers in your "Element" instances.

class iField {

public:

template

virtual void set(const T& value)=0;

template

virtual void get(T& value) const=0;

};

template

class Field : public iField {

private:

T0 value_;

public:

template

virtual void set(const T& value);

template

virtual void get(T& value) const;

//now specialize the relevant functions:

template <>

virtual void set(const T0& value) { value_ = value; }

template <>

virtual void get(T0& value) const { value = value_; }

};

Of course such an approach has further implications - I haven't really thought this through to the end. But then I guess that is the task that was set to you in the first place. ;)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值