mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-05 10:16:01 -04:00
115 lines
3.9 KiB
Markdown
115 lines
3.9 KiB
Markdown
SQLiteC++
|
|
---------
|
|
|
|
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
|
|
|
|
See SQLiteC++ website http://srombauts.github.com/SQLiteCpp on GitHub.
|
|
|
|
### License
|
|
|
|
Copyright (c) 2012 Sébastien Rombauts (sebastien.rombauts@gmail.com)
|
|
|
|
Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
|
or copy at http://opensource.org/licenses/MIT)
|
|
|
|
### The goals of SQLiteC++ are:
|
|
|
|
- to offer the best of existing simple wrappers
|
|
- to use a permissive license like MIT or BSD
|
|
- to be elegantly written with good C++ design, STL, exceptions and RAII idiom
|
|
- to keep dependencies to a minimum (STL and SQLite3)
|
|
- to be well documented, in code with Doxygen, and online with some good examples
|
|
- to be portable
|
|
- to be light and fast
|
|
- to be monothreaded
|
|
- to use API names sticking with those of the SQLite library
|
|
- to be well maintained
|
|
|
|
It is designed with the Resource Acquisition Is Initialization (RAII) idom
|
|
(see http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization),
|
|
and throw exceptions in case of SQLite errors.
|
|
Each SQLiteC++ object must be constructed with a valid SQLite database connection,
|
|
and then is always valid until destroyed.
|
|
|
|
### Depandancies:
|
|
|
|
- a STL implementation (even an old one like VC6/eVC4 should work)
|
|
- exception support (the class Exception inherite from std::runtime_error)
|
|
- the SQLite library, either by linking to it dynamicaly or staticaly,
|
|
or by adding its source file in your project code base.
|
|
|
|
To use it in your project, you only need to add the 6 SQLiteC++ source files
|
|
in your project code base (not the main.cpp example file).
|
|
|
|
## Getting started
|
|
### About SQLite:
|
|
SQLite is a library that implements a serverless transactional SQL database engine.
|
|
http://www.sqlite.org/about.html
|
|
|
|
### First sample demonstrates how to query a database and get results:
|
|
|
|
```C++
|
|
try
|
|
{
|
|
// Open a database file
|
|
SQLite::Database db("example.db3");
|
|
|
|
// Compile a SQL query, containing one parameter (index 1)
|
|
SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?");
|
|
|
|
// Bind the integer value 6 to the first parameter of the SQL query
|
|
query.bind(1, 6);
|
|
|
|
// Loop to execute the query step by step, to get rows of result
|
|
while (query.executeStep())
|
|
{
|
|
// Demonstrate how to get some typed column value
|
|
int id = query.getColumn(0);
|
|
const char* value = query.getColumn(1);
|
|
int size = query.getColumn(2);
|
|
|
|
std::cout << "row: " << id << ", " << value << ", " << size << std::endl;
|
|
}
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cout << "exception: " << e.what() << std::endl;
|
|
}
|
|
```
|
|
|
|
### Second sample shows how to manage a transaction:
|
|
|
|
```C++
|
|
try
|
|
{
|
|
SQLite::Database db("transaction.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
|
|
|
|
db.exec("DROP TABLE IF EXISTS test");
|
|
|
|
// Begin transaction
|
|
SQLite::Transaction transaction(db);
|
|
|
|
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
|
|
|
|
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
|
|
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
|
|
|
|
// Commit transaction
|
|
transaction.commit();
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cout << "exception: " << e.what() << std::endl;
|
|
}
|
|
```
|
|
|
|
## See also
|
|
### Some other simple C++ SQLite wrappers:
|
|
|
|
- [sqdbcpp](http://code.google.com/p/sqdbcpp/): RAII design, simple, no depandencies, UTF-8/UTF-16, new BSD license
|
|
- [sqlite3cc](http://ed.am/dev/sqlite3cc): uses boost, modern design, LPGPL
|
|
- [sqlite3pp](http://code.google.com/p/sqlite3pp/): uses boost, MIT License
|
|
- [SQLite++](http://sqlitepp.berlios.de/): uses boost build system, Boost License 1.0
|
|
- [CppSQLite](http://www.codeproject.com/Articles/6343/CppSQLite-C-Wrapper-for-SQLite/): famous Code Project but old design, BSD License
|
|
- [easySQLite](http://code.google.com/p/easysqlite/): manages table as structured objects, complex
|