简体   繁体   English

将 Cpp 类成员设置为 sqlite DB 中的列

[英]Set Cpp class members as columns in sqlite DB

I have a requirement to have the class members as DB (sqlite) columns and also define relationships using primary and foreign keys.我需要将类成员作为 DB (sqlite) 列,并使用主键和外键定义关系。 How can we achieve this?我们怎样才能做到这一点? Should we define the relationship ourselves while creating the tables or is there any libraries/extensions I can use.我们应该在创建表时自己定义关系还是有任何我可以使用的库/扩展。

class A
{
    int ID;
    string Name;
}

class B
{
    int ID;
    string Name;
    int AID;
}

class C
{
    int ID;
    string Name;
    int BID;
}

For the above class structure, I need to create DB tables for each classes and each table should have the members as the columns.对于上面的类结构,我需要为每个类创建数据库表,每个表都应该有成员作为列。 In each table, Member variable ID of respective classes should be primary key for each DB and AID (ID of class A) should be foreign key in class B and BID (ID of class B) should be foreign key in class C.在每个表中,各个类的成员变量ID应该是每个DB的主键,AID(A类的ID)应该是B类的外键,BID(B类的ID)应该是C类的外键。

Is there any library/extension which I can use to keep the relationships between the classes or it should be handled manually in code.?是否有任何库/扩展可以用来保持类之间的关系,或者应该在代码中手动处理。?

I have found some extensions for C#.Net but I'd love if something similar is available for C++.我已经找到了 C#.Net 的一些扩展,但如果 C++ 有类似的扩展,我会很高兴。

http://www.dsibinski.pl/2017/05/sqlite-net-extensions-one-to-many-relationships/ http://www.dsibinski.pl/2017/05/sqlite-net-extensions-one-to-many-relationships/

If I were, I would have created XSD or XML schema from my classes and create databases using that schema!.如果是,我会从我的类创建 XSD 或 XML 模式,并使用该模式创建数据库!。 That would work!那行得通!

I have finally found a way to do that which is creating the tables manually than using additional libraries.我终于找到了一种方法,即手动创建表而不是使用其他库。 There is another alternative library which is good and can help in achieve the same.还有另一个很好的替代库,可以帮助实现相同的目标。

https://github.com/paulftw/hiberlite https://github.com/paulftw/hiberlite

https://code.google.com/archive/p/hiberlite/ https://code.google.com/archive/p/hiberlite/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM