SQLiteC++  0.5.0
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
 All Classes Namespaces Files Functions Friends Macros
Database.cpp
Go to the documentation of this file.
1 /**
2  * @file Database.cpp
3  * @brief Management of a SQLite Database Connection.
4  *
5  * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6  *
7  * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
8  * or copy at http://opensource.org/licenses/MIT)
9  */
10 #include "Database.h"
11 
12 #include "Statement.h"
13 
14 namespace SQLite
15 {
16 
17 // Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags.
18 Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) : // throw(SQLite::Exception)
19  mpSQLite(NULL),
20  mFilename(apFilename)
21 {
22  int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, NULL);
23  if (SQLITE_OK != ret)
24  {
25  std::string strerr = sqlite3_errmsg(mpSQLite);
26  sqlite3_close(mpSQLite); // close is required even in case of error on opening
27  throw SQLite::Exception(strerr);
28  }
29 }
30 
31 // Close the SQLite database connection.
32 Database::~Database(void) throw() // nothrow
33 {
34  int ret = sqlite3_close(mpSQLite);
35  // Never throw an exception in a destructor
36  //std::cout << sqlite3_errmsg(mpSQLite) << std::endl;
37  SQLITE_CPP_ASSERT (SQLITE_OK == ret);
38 }
39 
40 // Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT...).
41 int Database::exec(const char* apQueries) // throw(SQLite::Exception);
42 {
43  int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
44  check(ret);
45 
46  // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE)
47  return sqlite3_changes(mpSQLite);
48 }
49 
50 // Shortcut to execute a one step query and fetch the first column of the result.
51 // WARNING: Be very careful with this dangerous method: you have to
52 // make a COPY OF THE result, else it will be destroy before the next line
53 // (when the underlying temporary Statement and Column objects are destroyed)
54 // this is an issue only for pointer type result (ie. char* and blob)
55 // (use the Column copy-constructor)
56 Column Database::execAndGet(const char* apQuery) // throw(SQLite::Exception)
57 {
58  Statement query(*this, apQuery);
59  query.executeStep();
60  return query.getColumn(0);
61 }
62 
63 // Shortcut to test if a table exists.
64 bool Database::tableExists(const char* apTableName) // throw(SQLite::Exception)
65 {
66  Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?");
67  query.bind(1, apTableName);
68  query.executeStep();
69  int Nb = query.getColumn(0);
70  return (1 == Nb);
71 }
72 
73 // Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
74 void Database::check(const int aRet) const // throw(SQLite::Exception)
75 {
76  if (SQLITE_OK != aRet)
77  {
78  throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
79  }
80 }
81 
82 } // namespace SQLite