mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Added binding function, and a simple example with an integer value
This commit is contained in:
parent
047cbcf637
commit
abc1f7bfa3
BIN
example.db3
BIN
example.db3
Binary file not shown.
@ -16,9 +16,9 @@ namespace SQLite
|
||||
{
|
||||
|
||||
// Open the provided database UTF-8 filename.
|
||||
Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) throw (SQLite::Exception) :
|
||||
Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) throw(SQLite::Exception) :
|
||||
mpSQLite(NULL),
|
||||
mFilename (apFilename)
|
||||
mFilename(apFilename)
|
||||
{
|
||||
int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, NULL);
|
||||
if (SQLITE_OK != ret)
|
||||
@ -30,7 +30,7 @@ namespace SQLite
|
||||
}
|
||||
|
||||
// Close the SQLite database connection.
|
||||
Database::~Database(void) throw () // nothrow
|
||||
Database::~Database(void) throw() // nothrow
|
||||
{
|
||||
// check for undestroyed statements
|
||||
std::vector<Statement*>::iterator iStatement;
|
||||
@ -50,19 +50,19 @@ Database::~Database(void) throw () // nothrow
|
||||
}
|
||||
|
||||
// Register a Statement object (a SQLite query)
|
||||
void Database::registerStatement (Statement& aStatement) throw (SQLite::Exception)
|
||||
void Database::registerStatement(Statement& aStatement) throw(SQLite::Exception)
|
||||
{
|
||||
mStatementList.push_back (&aStatement);
|
||||
mStatementList.push_back(&aStatement);
|
||||
}
|
||||
|
||||
// Unregister a Statement object
|
||||
void Database::unregisterStatement (Statement& aStatement) throw (SQLite::Exception)
|
||||
void Database::unregisterStatement(Statement& aStatement) throw(SQLite::Exception)
|
||||
{
|
||||
std::vector<Statement*>::iterator iStatement;
|
||||
iStatement = std::find (mStatementList.begin(), mStatementList.end(), &aStatement);
|
||||
iStatement = std::find(mStatementList.begin(), mStatementList.end(), &aStatement);
|
||||
if (mStatementList.end() != iStatement)
|
||||
{
|
||||
mStatementList.erase (iStatement);
|
||||
mStatementList.erase(iStatement);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ public:
|
||||
* @param[in] apFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter)
|
||||
* @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...
|
||||
*/
|
||||
explicit Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY) throw (SQLite::Exception);
|
||||
explicit Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY) throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief Close the SQLite database connection.
|
||||
@ -53,17 +53,17 @@ public:
|
||||
* All SQLite statements must have been finalized before,
|
||||
* so all Statement objects must have been unregistered.
|
||||
*/
|
||||
virtual ~Database(void) throw (); // nothrow
|
||||
virtual ~Database(void) throw(); // nothrow
|
||||
|
||||
/**
|
||||
* @brief Register a Statement object (a SQLite query)
|
||||
*/
|
||||
void registerStatement (Statement& aStatement) throw (SQLite::Exception);
|
||||
void registerStatement(Statement& aStatement) throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief Unregister a Statement object
|
||||
*/
|
||||
void unregisterStatement (Statement& aStatement) throw (SQLite::Exception);
|
||||
void unregisterStatement(Statement& aStatement) throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief Filename used to open the database
|
||||
|
@ -16,7 +16,7 @@ namespace SQLite
|
||||
{
|
||||
|
||||
// Compile and register the SQL query for the provided SQLite Database Connection
|
||||
Statement::Statement(Database &aDatabase, const char* apQuery) throw (SQLite::Exception) :
|
||||
Statement::Statement(Database &aDatabase, const char* apQuery) throw(SQLite::Exception) :
|
||||
mpStmt(NULL),
|
||||
mDatabase(aDatabase),
|
||||
mQuery(apQuery),
|
||||
@ -31,7 +31,7 @@ Statement::Statement(Database &aDatabase, const char* apQuery) throw (SQLite::Ex
|
||||
}
|
||||
|
||||
// Finalize and unregister the SQL query from the SQLite Database Connection.
|
||||
Statement::~Statement(void) throw () // nothrow
|
||||
Statement::~Statement(void) throw() // nothrow
|
||||
{
|
||||
int ret = sqlite3_finalize(mpStmt);
|
||||
if (SQLITE_OK != ret)
|
||||
@ -43,7 +43,7 @@ Statement::~Statement(void) throw () // nothrow
|
||||
}
|
||||
|
||||
// Reset the statement to make it ready for a new execution
|
||||
void Statement::reset (void) throw (SQLite::Exception)
|
||||
void Statement::reset(void) throw(SQLite::Exception)
|
||||
{
|
||||
mbDone = false;
|
||||
int ret = sqlite3_reset(mpStmt);
|
||||
@ -53,8 +53,69 @@ void Statement::reset (void) throw (SQLite::Exception)
|
||||
}
|
||||
}
|
||||
|
||||
// Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
void Statement::bind(const int aIndex, const int& aValue) throw(SQLite::Exception)
|
||||
{
|
||||
int ret = sqlite3_bind_int(mpStmt, aIndex, aValue);
|
||||
if (SQLITE_OK != ret)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
// Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
void Statement::bind(const int aIndex, const sqlite3_int64& aValue) throw(SQLite::Exception)
|
||||
|
||||
{
|
||||
int ret = sqlite3_bind_int64(mpStmt, aIndex, aValue);
|
||||
if (SQLITE_OK != ret)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
// Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
void Statement::bind(const int aIndex, const double& aValue) throw(SQLite::Exception)
|
||||
{
|
||||
int ret = sqlite3_bind_double(mpStmt, aIndex, aValue);
|
||||
if (SQLITE_OK != ret)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
// Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
void Statement::bind(const int aIndex, const std::string& aValue) throw(SQLite::Exception)
|
||||
{
|
||||
int ret = sqlite3_bind_text(mpStmt, aIndex, aValue.c_str(), aValue.size(), SQLITE_TRANSIENT);
|
||||
if (SQLITE_OK != ret)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
// Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
void Statement::bind(const int aIndex, const char* apValue) throw(SQLite::Exception)
|
||||
{
|
||||
int ret = sqlite3_bind_text(mpStmt, aIndex, apValue, -1, SQLITE_TRANSIENT);
|
||||
if (SQLITE_OK != ret)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
// Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
void Statement::bind(const int aIndex) throw(SQLite::Exception)
|
||||
{
|
||||
int ret = sqlite3_bind_null(mpStmt, aIndex);
|
||||
if (SQLITE_OK != ret)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
// Execute a step of the query to fetch one row of results
|
||||
bool Statement::executeStep (void) throw (SQLite::Exception)
|
||||
bool Statement::executeStep(void) throw(SQLite::Exception)
|
||||
{
|
||||
bool bOk = false;
|
||||
|
||||
@ -67,7 +128,7 @@ bool Statement::executeStep (void) throw (SQLite::Exception)
|
||||
}
|
||||
else if (SQLITE_DONE == ret)
|
||||
{
|
||||
bOk = true;
|
||||
bOk = false;
|
||||
mbDone = true;
|
||||
}
|
||||
else
|
||||
|
@ -32,24 +32,47 @@ public:
|
||||
*
|
||||
* Exception is thrown in case of error, then the Statement object is NOT constructed.
|
||||
*/
|
||||
explicit Statement(Database &aDatabase, const char* apQuery) throw (SQLite::Exception);
|
||||
explicit Statement(Database &aDatabase, const char* apQuery) throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief Finalize and unregister the SQL query from the SQLite Database Connection.
|
||||
*/
|
||||
virtual ~Statement(void) throw (); // nothrow
|
||||
virtual ~Statement(void) throw(); // nothrow
|
||||
|
||||
/**
|
||||
* @brief Reset the statement to make it ready for a new execution.
|
||||
*/
|
||||
void reset (void) throw (SQLite::Exception);
|
||||
void reset(void) throw(SQLite::Exception);
|
||||
|
||||
// TODO bind
|
||||
/**
|
||||
* @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
*/
|
||||
void bind(const int aIndex, const int& aValue) throw(SQLite::Exception);
|
||||
/**
|
||||
* @brief Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
*/
|
||||
void bind(const int aIndex, const sqlite3_int64& aValue) throw(SQLite::Exception);
|
||||
/**
|
||||
* @brief Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
*/
|
||||
void bind(const int aIndex, const double& aValue) throw(SQLite::Exception);
|
||||
/**
|
||||
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
*/
|
||||
void bind(const int aIndex, const std::string& aValue) throw(SQLite::Exception);
|
||||
/**
|
||||
* @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
*/
|
||||
void bind(const int aIndex, const char* apValue) throw(SQLite::Exception);
|
||||
/**
|
||||
* @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
|
||||
*/
|
||||
void bind(const int aIndex) throw(SQLite::Exception); // bind NULL value
|
||||
|
||||
/**
|
||||
* @brief Execute a step of the query to fetch one row of results.
|
||||
*/
|
||||
bool executeStep (void) throw (SQLite::Exception);
|
||||
bool executeStep(void) throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief UTF-8 SQL Query.
|
||||
|
@ -3,25 +3,26 @@
|
||||
|
||||
int main (void)
|
||||
{
|
||||
std::cout << "Hello SQLite.hpp\n";
|
||||
try
|
||||
{
|
||||
// Open a database file
|
||||
SQLite::Database db("example.db3");
|
||||
std::cout << db.getFilename().c_str() << " onpened\n";
|
||||
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
|
||||
|
||||
SQLite::Statement stmt(db, "SELECT * FROM test");
|
||||
std::cout << "statement created\n";
|
||||
// Compile a SQL query, containing one parameter (index 1)
|
||||
SQLite::Statement query(db, "SELECT * FROM test WHERE size>?");
|
||||
// Bind an integer value "6" to the first parameter of the SQL query
|
||||
query.bind(1, 6);
|
||||
|
||||
while (stmt.executeStep())
|
||||
while (query.executeStep())
|
||||
{
|
||||
std::cout << "executeStep\n";
|
||||
}
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "SQLite exception: " << e.what() << std::endl;
|
||||
std::cout << "SQLite exception: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << "Bye SQLite.hpp\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user