什么是 sqlite 的好的 OO C++ 包装器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/120295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a good OO C++ wrapper for sqlite
提问by Foo42
I'd like to find a good object oriented C++ (as opposed to C) wrapper for sqlite. What do people recommend? If you have several suggestions please put them in separate replies for voting purposes. Also, please indicate whether you have any experience of the wrapper you are suggesting and how you found it to use.
我想为sqlite找到一个好的面向对象的C++(而不是C)包装器。人们推荐什么?如果您有几个建议,请将它们放在单独的回复中以供投票之用。另外,请说明您是否对所建议的包装器有任何经验以及如何使用它。
回答by Johan Kotlinski
This is really inviting down-votes, but here goes...
这真的很吸引人的反对票,但是这里......
I use sqlite directly from C++, and don't see any value with an added C++ abstraction layer. It's quite good (and efficient) as is.
我直接从 C++ 使用 sqlite,并没有看到添加 C++ 抽象层的任何值。它非常好(而且高效)。
回答by jk.
Another good wraper for databases in C++ is SOCI. It's not very OO, but the more Modern C++.
另一个很好的 C++ 数据库包装器是SOCI。它不是很面向对象,而是更现代的 C++。
It supports Oracle, PostgreSQL and MySQL. A SQLite backend is in the CVS.
它支持 Oracle、PostgreSQL 和 MySQL。一个SQLite的后端是在CVS。
回答by amin
I read this post and tried some of the libraries mentioned in the answers ,
But none of them was easy enough for me ( i am a lazy programmer ! ).
我阅读了这篇文章并尝试了答案中提到的一些库,
但对我来说它们都不够简单(我是一个懒惰的程序员!)。
So i wrote my own wrapper : sqlite modern cpp
所以我写了我自己的包装器:sqlite modern cpp
database db("dbfile.db");
// executes the query and creates a 'user' table if not exists
db << "create table if not exists user ("
" age int,"
" name text,"
" weight real"
");";
// inserts a new user and binds the values to '?' marks
db << "insert into user (age,name,weight) values (?,?,?);"
<< 20
<< "bob"
<< 83.0;
// slects from table user on a condition ( age > 18 ) and executes
// the lambda for every row returned .
db << "select age,name,weight from user where age > ? ;"
<< 18
>> [&](int age, string name, double weight) {
cout << age << ' ' << name << ' ' << weight << endl;
};
// selects the count(*) of table user
int count = 0;
db << "select count(*) from user" >> count;
Have fun !
玩得开心 !
回答by markshiz
Here's one that hasn't been updated in a while, but compiles and runs on Mac OS GCC 4.3. It's also released under the MIT License, so you can use it in a commercial project, no problems. http://code.google.com/p/sqlite3pp/
这里有一段时间没有更新,但可以在 Mac OS GCC 4.3 上编译和运行。它也是在 MIT 许可证下发布的,因此您可以在商业项目中使用它,没有问题。 http://code.google.com/p/sqlite3pp/
The usage is boost-ified and very clean:
用法是增强的并且非常干净:
sqlite3pp::database db("test.db");
sqlite3pp::transaction xct(db);
{
sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)");
cmd.bind(":user", "Mike");
cmd.bind(":phone", "555-1234");
cmd.execute();
}
xct.rollback();
回答by zaharpopov
Use Qt - it has great binding for SQLite that fits well into its overall design
使用 Qt - 它对 SQLite 具有很好的约束力,非常适合其整体设计
回答by burner
I also wasn't pleased with what I could find. Now you can write:
我也对我能找到的东西不满意。现在你可以写:
class Person {
public:
Person() {}
static SqlTable<Person>& table() {
static SqlTable<Person> tab = SqlTable<Person>::sqlTable("Person",
SqlColumn<Person>("Firstname", makeAttr(&Person::firstname)),
SqlColumn<Person>("Lastname", makeAttr(&Person::lastname)),
SqlColumn<Person>("Age", makeAttr(&Person::age)),
return tab;
}
std::string firstname;
std::string lastname;
int age;
};
SqliteDB db("testtable.db");
auto sel(db.select<Person>("Firstname=\"Danny\" and Lastname=\"Zeckzer\""));
std::for_each(sel.first, sel.second, [](const Person& p) {
...
Person me;
db.insert<Person>(me);
...
std::vector<Person> everybody;
db.insert<Person>(everybody.begin(), everybody.end());
The table method is all you need to write as long as you stick to the sqlite3 data types. As everything is a template not much abstraction layer code remains after -O. Natural joins require a result class similar to the Person class. The implementation is a single header with less than 500 lines. License is LGPL. Source
只要您坚持使用 sqlite3 数据类型,table 方法就是您需要编写的全部内容。由于一切都是模板,-O 之后没有多少抽象层代码。自然连接需要一个类似于 Person 类的结果类。实现是一个少于 500 行的单个标题。许可证是 LGPL。来源
回答by edam
I wasn't pleased with any I could find either, so I wrote my own: sqlite3cc.
我对我能找到的任何东西都不满意,所以我自己写了:sqlite3cc。
Here's a code example:
这是一个代码示例:
sqlite::connection db( filename );
sqlite::command c( db, "UPDATE foo SET bar = ? WHERE name = ?" );
c << 123 << name << sqlite::exec;
sqlite::query q( db, "SELECT foo FROM bar" );
for( sqlite::query::iterator i = q.begin(); i != q.end(); i++ )
std::cout << i->column< std::string >( 0 ) << "\n";
回答by Luca Davanzo
Everyone have given good advice on what to use: I'll tell you what instrument NOTuse.
每个人都对使用什么给出了很好的建议:我会告诉你不使用什么工具。
My experience is terrible.
I'm just doing some reasearch on what orm use, and I'm testing a lot of it.
我的经历太可怕了。
我只是在研究 orm 的用途,我正在测试很多。
Weaknesses:
弱点:
- no documentation
- no explanatory README
- no explanation on prerequisites
- do not compile due to a lot of bug(isn't true, isn't fixed in v0.3.17)
- 没有文件
- 没有解释性的自述文件
- 没有先决条件的解释
- 由于很多错误而无法编译(不是真的,在 v0.3.17 中未修复)
回答by Luca Davanzo
http://www.codeproject.com/KB/database/CppSQLite.aspxis just fantastic, it is very easy to port, I had it working on bcb5 (omg) in half an hour or so. It is about as thin as you can get and easy to understand. There are a goodly number of examples that cover just about every thing you need to know. It uses exceptions for error handling - I modified it to provide return codes in a mater of minutes. Only tricky issue is to create your own lib file none are provided.
http://www.codeproject.com/KB/database/CppSQLite.aspx太棒了,它很容易移植,我在半小时左右的时间里让它在 bcb5 (omg) 上工作。它尽可能地薄且易于理解。有很多示例几乎涵盖了您需要了解的所有内容。它使用异常进行错误处理 - 我修改它以在几分钟内提供返回代码。唯一棘手的问题是创建自己的 lib 文件,但没有提供。
try
{
CppSQLite3DB db;
db.open(asFileName.c_str());
db.execDML("Update data set hrx = 0");
} // try
catch (...)
{
} // catch
Could not be much simpler than this.....
没有比这更简单的了......
回答by Brad Bruce
I've used this one http://www.codeproject.com/KB/database/CppSQLite.aspxbut I've moved to C#, so there may be newer/better ones now
我已经使用了这个http://www.codeproject.com/KB/database/CppSQLite.aspx但我已经转移到 C#,所以现在可能有更新/更好的