├── README.md ├── dbmanager.cpp ├── dbmanager.h ├── main.cpp └── sqlite_qt.pro /README.md: -------------------------------------------------------------------------------- 1 | # sql_with_qt 2 | 3 | This repository is a small example of how to set up sqlite database with Qt and perform some basic queries. 4 | 5 | # Instructions 6 | 1. Install [SQLITE](https://www.sqlite.org) 7 | 2. Download this Github repository 8 | 3. Run in QT Creator and watch it connect to the database. 9 | 10 | Visit the main article [here](https://katecpp.wordpress.com/2015/08/28/sqlite-with-qt/) to understand more. 11 | 12 | **IMPORTANT** If you don't see your database file show up in your file directory, you need to go to Projects -> Build & Run -> Run -> Working Directory. This is where your database file will be generated. 13 | 14 | --- 15 | 16 | ## Update History 17 | 18 | Updated October 29, 2016 19 | * Added a function to create tables, which will also automatically create a new database if it doesn't already exist. 20 | -------------------------------------------------------------------------------- /dbmanager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | DbManager::DbManager(const QString &path) 8 | { 9 | m_db = QSqlDatabase::addDatabase("QSQLITE"); 10 | m_db.setDatabaseName(path); 11 | 12 | if (!m_db.open()) 13 | { 14 | qDebug() << "Error: connection with database fail"; 15 | } 16 | else 17 | { 18 | qDebug() << "Database: connection ok"; 19 | } 20 | } 21 | 22 | DbManager::~DbManager() 23 | { 24 | if (m_db.isOpen()) 25 | { 26 | m_db.close(); 27 | } 28 | } 29 | 30 | bool DbManager::isOpen() const 31 | { 32 | return m_db.isOpen(); 33 | } 34 | 35 | bool DbManager::createTable() 36 | { 37 | bool success = false; 38 | 39 | QSqlQuery query; 40 | query.prepare("CREATE TABLE people(id INTEGER PRIMARY KEY, name TEXT);"); 41 | 42 | if (!query.exec()) 43 | { 44 | qDebug() << "Couldn't create the table 'people': one might already exist."; 45 | success = false; 46 | } 47 | 48 | return success; 49 | } 50 | 51 | bool DbManager::addPerson(const QString& name) 52 | { 53 | bool success = false; 54 | 55 | if (!name.isEmpty()) 56 | { 57 | QSqlQuery queryAdd; 58 | queryAdd.prepare("INSERT INTO people (name) VALUES (:name)"); 59 | queryAdd.bindValue(":name", name); 60 | 61 | if(queryAdd.exec()) 62 | { 63 | success = true; 64 | } 65 | else 66 | { 67 | qDebug() << "add person failed: " << queryAdd.lastError(); 68 | } 69 | } 70 | else 71 | { 72 | qDebug() << "add person failed: name cannot be empty"; 73 | } 74 | 75 | return success; 76 | } 77 | 78 | bool DbManager::removePerson(const QString& name) 79 | { 80 | bool success = false; 81 | 82 | if (personExists(name)) 83 | { 84 | QSqlQuery queryDelete; 85 | queryDelete.prepare("DELETE FROM people WHERE name = (:name)"); 86 | queryDelete.bindValue(":name", name); 87 | success = queryDelete.exec(); 88 | 89 | if(!success) 90 | { 91 | qDebug() << "remove person failed: " << queryDelete.lastError(); 92 | } 93 | } 94 | else 95 | { 96 | qDebug() << "remove person failed: person doesnt exist"; 97 | } 98 | 99 | return success; 100 | } 101 | 102 | void DbManager::printAllPersons() const 103 | { 104 | qDebug() << "Persons in db:"; 105 | QSqlQuery query("SELECT * FROM people"); 106 | int idName = query.record().indexOf("name"); 107 | while (query.next()) 108 | { 109 | QString name = query.value(idName).toString(); 110 | qDebug() << "===" << name; 111 | } 112 | } 113 | 114 | bool DbManager::personExists(const QString& name) const 115 | { 116 | bool exists = false; 117 | 118 | QSqlQuery checkQuery; 119 | checkQuery.prepare("SELECT name FROM people WHERE name = (:name)"); 120 | checkQuery.bindValue(":name", name); 121 | 122 | if (checkQuery.exec()) 123 | { 124 | if (checkQuery.next()) 125 | { 126 | exists = true; 127 | } 128 | } 129 | else 130 | { 131 | qDebug() << "person exists failed: " << checkQuery.lastError(); 132 | } 133 | 134 | return exists; 135 | } 136 | 137 | bool DbManager::removeAllPersons() 138 | { 139 | bool success = false; 140 | 141 | QSqlQuery removeQuery; 142 | removeQuery.prepare("DELETE FROM people"); 143 | 144 | if (removeQuery.exec()) 145 | { 146 | success = true; 147 | } 148 | else 149 | { 150 | qDebug() << "remove all persons failed: " << removeQuery.lastError(); 151 | } 152 | 153 | return success; 154 | } 155 | -------------------------------------------------------------------------------- /dbmanager.h: -------------------------------------------------------------------------------- 1 | #ifndef DBMANAGER_H 2 | #define DBMANAGER_H 3 | 4 | #include 5 | 6 | /** 7 | * \class DbManager 8 | * 9 | * \brief SQL Database Manager class 10 | * 11 | * DbManager sets up the connection with SQL database 12 | * and performs some basics queries. The class requires 13 | * existing SQL database. You can create it with sqlite: 14 | * 1. $ sqlite3 people.db 15 | * 2. sqilte> CREATE TABLE people(ids integer primary key, name text); 16 | * 3. sqlite> .quit 17 | */ 18 | class DbManager 19 | { 20 | public: 21 | /** 22 | * @brief Constructor 23 | * 24 | * Constructor sets up connection with db and opens it 25 | * @param path - absolute path to db file 26 | */ 27 | DbManager(const QString& path); 28 | 29 | /** 30 | * @brief Destructor 31 | * 32 | * Close the db connection 33 | */ 34 | ~DbManager(); 35 | 36 | bool isOpen() const; 37 | 38 | /** 39 | * @brief Creates a new 'people' table if it doesn't already exist 40 | * @return true - 'people' table created successfully, false - table not created 41 | */ 42 | bool createTable(); 43 | 44 | /** 45 | * @brief Add person data to db 46 | * @param name - name of person to add 47 | * @return true - person added successfully, false - person not added 48 | */ 49 | bool addPerson(const QString& name); 50 | 51 | /** 52 | * @brief Remove person data from db 53 | * @param name - name of person to remove. 54 | * @return true - person removed successfully, false - person not removed 55 | */ 56 | bool removePerson(const QString& name); 57 | 58 | /** 59 | * @brief Check if person of name "name" exists in db 60 | * @param name - name of person to check. 61 | * @return true - person exists, false - person does not exist 62 | */ 63 | bool personExists(const QString& name) const; 64 | 65 | /** 66 | * @brief Print names of all persons in db 67 | */ 68 | void printAllPersons() const; 69 | 70 | /** 71 | * @brief Remove all persons from db 72 | * @return true - all persons removed successfully, false - not removed 73 | */ 74 | bool removeAllPersons(); 75 | 76 | private: 77 | QSqlDatabase m_db; 78 | }; 79 | 80 | #endif // DBMANAGER_H 81 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Change to any path you wish 6 | static const QString path = "example.db"; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | QCoreApplication a(argc, argv); 11 | DbManager db(path); 12 | 13 | if (db.isOpen()) 14 | { 15 | db.createTable(); // Creates a table if it doens't exist. Otherwise, it will use existing table. 16 | db.addPerson("A"); 17 | db.addPerson("B"); 18 | db.addPerson("C"); 19 | db.printAllPersons(); 20 | db.removePerson("C"); 21 | db.printAllPersons(); 22 | db.removeAllPersons(); 23 | qDebug() << "End"; 24 | } 25 | else 26 | { 27 | qDebug() << "Database is not open!"; 28 | } 29 | 30 | return a.exec(); 31 | } 32 | -------------------------------------------------------------------------------- /sqlite_qt.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2015-08-27T18:41:32 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core sql 8 | 9 | QT -= gui 10 | 11 | TARGET = sqlite_qt 12 | CONFIG += console 13 | CONFIG -= app_bundle 14 | 15 | TEMPLATE = app 16 | 17 | 18 | SOURCES += main.cpp \ 19 | dbmanager.cpp 20 | 21 | HEADERS += \ 22 | dbmanager.h 23 | --------------------------------------------------------------------------------