Added binding function, and a simple example with an integer value

This commit is contained in:
Sebastien Rombauts 2012-03-31 17:33:27 +02:00
parent 047cbcf637
commit abc1f7bfa3
6 changed files with 114 additions and 29 deletions

Binary file not shown.

View File

@ -53,6 +53,67 @@ 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)
{
@ -67,7 +128,7 @@ bool Statement::executeStep (void) throw (SQLite::Exception)
}
else if (SQLITE_DONE == ret)
{
bOk = true;
bOk = false;
mbDone = true;
}
else

View File

@ -44,7 +44,30 @@ public:
*/
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.

View File

@ -3,16 +3,18 @@
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";
}
@ -21,7 +23,6 @@ int main (void)
{
std::cout << "SQLite exception: " << e.what() << std::endl;
}
std::cout << "Bye SQLite.hpp\n";
return 0;
}