mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Added a simple Database::exec() methode to execute simple (multiple) SQL statement
- This can be used when no results are returned, in Data Manipulation Language SQL statements (like CREATE, INSERT, UPDATE, DROP)
This commit is contained in:
parent
b17ea542d4
commit
c2003d0bd1
@ -67,5 +67,23 @@ void Database::unregisterStatement(Statement& aStatement) // throw(SQLite::Excep
|
||||
}
|
||||
}
|
||||
|
||||
// Shorcut to execute one or multiple SQL statements without results.
|
||||
int Database::exec(const char* apQueries) // throw(SQLite::Exception);
|
||||
{
|
||||
int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
|
||||
check(ret);
|
||||
|
||||
// Return the number of changes made by thoses SQL statements
|
||||
return sqlite3_changes(mpSQLite);
|
||||
}
|
||||
|
||||
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
|
||||
void Database::check(const int aRet) const // throw(SQLite::Exception)
|
||||
{
|
||||
if (SQLITE_OK != aRet)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
|
||||
}
|
||||
}
|
||||
|
||||
}; // namespace SQLite
|
||||
|
@ -68,6 +68,17 @@ public:
|
||||
*/
|
||||
void unregisterStatement(Statement& aStatement); // throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief Shorcut to execute one or multiple statements without results.
|
||||
*
|
||||
* This is usefull for Data Manipulation Language SQL statements like CREATE, INSERT, UPDATE, DROP
|
||||
*
|
||||
* @see also Statement class for handling queries with resultats
|
||||
*
|
||||
* @param[in] apQueries one or multiple UTF-8 encoded, semicolon-separate SQL statements
|
||||
*/
|
||||
int exec(const char* apQueries); // throw(SQLite::Exception);
|
||||
|
||||
/**
|
||||
* @brief Filename used to open the database
|
||||
*/
|
||||
@ -90,6 +101,11 @@ private:
|
||||
Database(const Database&);
|
||||
Database& operator=(const Database&);
|
||||
|
||||
/**
|
||||
* @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
|
||||
*/
|
||||
void check(const int aRet) const; // throw(SQLite::Exception);
|
||||
|
||||
private:
|
||||
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
|
||||
std::string mFilename; //!< UTF-8 filename used to open the database
|
||||
|
@ -147,10 +147,7 @@ bool Statement::isColumnNull(const int aIndex) const // throw(SQLite::Exception)
|
||||
return (SQLITE_NULL == sqlite3_column_type(mpStmt, aIndex));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
|
||||
*/
|
||||
// @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
|
||||
void Statement::check(const int aRet) const // throw(SQLite::Exception)
|
||||
{
|
||||
if (SQLITE_OK != aRet)
|
||||
@ -159,6 +156,7 @@ void Statement::check(const int aRet) const // throw(SQLite::Exception)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Implementation of the inner class Statement::Column
|
||||
//
|
||||
|
@ -55,7 +55,7 @@ private:
|
||||
|
||||
int main (void)
|
||||
{
|
||||
// Basic example (1/2) :
|
||||
// Basic example (1/3) :
|
||||
try
|
||||
{
|
||||
// Open a database file
|
||||
@ -63,7 +63,7 @@ int main (void)
|
||||
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
|
||||
|
||||
// Compile a SQL query, containing one parameter (index 1)
|
||||
SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?");
|
||||
SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?;SELECT id FROM test");
|
||||
std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n";
|
||||
// Bind the integer value 6 to the first parameter of the SQL query
|
||||
query.bind(1, 6);
|
||||
@ -98,7 +98,7 @@ int main (void)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Object Oriented Basic example (2/2) :
|
||||
// Object Oriented Basic example (2/3) :
|
||||
try
|
||||
{
|
||||
// Open the database and compile the query
|
||||
@ -114,5 +114,29 @@ int main (void)
|
||||
std::cout << "SQLite exception: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Simple batch queries example (3/3) :
|
||||
try
|
||||
{
|
||||
// Open a database file
|
||||
SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
|
||||
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
|
||||
|
||||
db.exec("DROP TABLE IF EXISTS test");
|
||||
|
||||
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;
|
||||
|
||||
db.exec("DROP TABLE test");
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "SQLite exception: " << e.what() << std::endl;
|
||||
}
|
||||
std::remove("test.db3");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user