「Qtの基礎 - データベース」の版間の差分

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動
79行目: 79行目:
   
   
  #include <QtSql>
  #include <QtSql>
#include <QString>
#include <QSqlError>
#include <stdexcept>
  #include <QDebug>
  #include <QDebug>
class DatabaseException : public std::runtime_error
{
public:
    DatabaseException(const QString& message) : std::runtime_error(message.toStdString()) {}
};
   
   
  class DatabaseManager
  class DatabaseManager
93行目: 102行目:
     bool updateRecord(int id, const QString &name, int age);
     bool updateRecord(int id, const QString &name, int age);
     bool deleteRecord(int id);
     bool deleteRecord(int id);
     void readAllRecords();
     QList<QMap<QString, QVariant>> readAllRecords();
   
   
  private:
  private:
     QSqlDatabase db;
     QSqlDatabase db;
    void checkConnection();
    void handleDatabaseError(const QString &operation, const QSqlError &error);
  };
  };
  </syntaxhighlight>
  </syntaxhighlight>
123行目: 134行目:
  {
  {
     if (!db.open()) {
     if (!db.open()) {
       qDebug() << "Error: connection with database failed";
       throw DatabaseException("Failed to open database connection: " + db.lastError().text());
      return false;
    }
    else {
      qDebug() << "Database: connection ok";
      return true;
     }
     }
    qDebug() << "Database connection opened successfully";
  }
  }
   
   
135行目: 143行目:
  void DatabaseManager::closeConnection()
  void DatabaseManager::closeConnection()
  {
  {
     db.close();
     if (db.isOpen()) {
      db.close();
      qDebug() << "Database connection closed";
    }
}
// 接続の確認
void DatabaseManager::checkConnection()
{
    if (!db.isOpen()) {
      throw DatabaseException("Database connection is not open");
    }
}
// エラーハンドリング処理
void DatabaseManager::handleDatabaseError(const QString &operation, const QSqlError &error)
{
    QString errorMessage = QString("%1 failed: %2").arg(operation, error.text());
    throw DatabaseException(errorMessage);
  }
  }
   
   
141行目: 167行目:
  bool DatabaseManager::createTable()
  bool DatabaseManager::createTable()
  {
  {
    checkConnection();
     QSqlQuery query;
     QSqlQuery query;
     query.prepare("CREATE TABLE IF NOT EXISTS people (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40) NOT NULL, age INT)");
     if (!query.exec("CREATE TABLE IF NOT EXISTS people (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40) NOT NULL, age INT)")) {
       handleDatabaseError("Create table", query.lastError());
    if (!query.exec()) {
       qDebug() << "Error creating table:" << query.lastError().text();
      return false;
     }
     }
   
   
     return true;
     qDebug() << "Table 'people' created or already exists";
  }
  }
   
   
155行目: 180行目:
  bool DatabaseManager::insertRecord(const QString& name, int age)
  bool DatabaseManager::insertRecord(const QString& name, int age)
  {
  {
    checkConnection();
     QSqlQuery query;
     QSqlQuery query;
     query.prepare("INSERT INTO people (name, age) VALUES (:name, :age)");
     query.prepare("INSERT INTO people (name, age) VALUES (:name, :age)");
161行目: 188行目:
   
   
     if (!query.exec()) {
     if (!query.exec()) {
       qDebug() << "Error inserting record:" << query.lastError().text();
       handleDatabaseError("Insert record", query.lastError());
      return false;
     }
     }
   
   
     return true;
     qDebug() << "Record inserted successfully";
  }
  }
   
   
171行目: 197行目:
  bool DatabaseManager::updateRecord(int id, const QString& name, int age)
  bool DatabaseManager::updateRecord(int id, const QString& name, int age)
  {
  {
    checkConnection();
     QSqlQuery query;
     QSqlQuery query;
     query.prepare("UPDATE people SET name = :name, age = :age WHERE id = :id");
     query.prepare("UPDATE people SET name = :name, age = :age WHERE id = :id");
178行目: 206行目:
   
   
     if (!query.exec()) {
     if (!query.exec()) {
       qDebug() << "Error updating record:" << query.lastError().text();
       handleDatabaseError("Update record", query.lastError());
      return false;
     }
     }
   
   
     return true;
     if (query.numRowsAffected() == 0) {
      throw DatabaseException(QString("No record found with id: %1").arg(id));
    }
    qDebug() << "Record updated successfully";
  }
  }
   
   
// レコードの削除
  bool DatabaseManager::deleteRecord(int id)
  bool DatabaseManager::deleteRecord(int id)
  {
  {
    checkConnection();
     QSqlQuery query;
     QSqlQuery query;
     query.prepare("DELETE FROM people WHERE id = :id");
     query.prepare("DELETE FROM people WHERE id = :id");
     query.bindValue(":id", id);
     query.bindValue(":id", id);
   
    if (!query.exec()) {
      handleDatabaseError("Delete record", query.lastError());
    }
   
   
     if (!query.exec()) {
     if (query.numRowsAffected() == 0) {
       qDebug() << "Error deleting record:" << query.lastError().text();
       throw DatabaseException(QString("No record found with id: %1").arg(id));
      return false;
     }
     }
   
   
     return true;
     qDebug() << "Record deleted successfully";
  }
  }
   
   
  // 全てのレコードを読み込む
  // 全てのレコードを読み込む
  void DatabaseManager::readAllRecords()
  QList<QMap<QString, QVariant>> DatabaseManager::readAllRecords()
  {
  {
    checkConnection();
     QSqlQuery query("SELECT * FROM people");
     QSqlQuery query("SELECT * FROM people");
    QList<QMap<QString, QVariant>> results;
   
    if (!query.exec()) {
      handleDatabaseError("Read all records", query.lastError());
    }
     while (query.next()) {
     while (query.next()) {
       int id = query.value(0).toInt();
       QMap<QString, QVariant> record;
       QString name = query.value(1).toString();
      record["id"] = query.value(0);
       int age = query.value(2).toInt();
       record["name"] = query.value(1);
       qDebug() << "ID:" << id << "Name:" << name << "Age:" << age;
       record["age"] = query.value(2);
       results.append(record);
     }
     }
    return results;
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>
219行目: 267行目:
     DatabaseManager dbManager;
     DatabaseManager dbManager;
   
   
     if (dbManager.openConnection()) {
     try {
      // データーベースの接続
      dbManager.openConnection();
     
       // テーブルの作成
       // テーブルの作成
       dbManager.createTable();
       dbManager.createTable();
228行目: 279行目:
   
   
       // 全てのレコードを読み込む
       // 全てのレコードを読み込む
       dbManager.readAllRecords();
       QList<QMap<QString, QVariant>> records = dbManager.readAllRecords();
      for (const auto& record : records) {
          qDebug() << "ID:" << record["id"].toInt()
                    << "Name:" << record["name"].toString()
                    << "Age:" << record["age"].toInt();
        }
   
   
      // 既存のレコードの更新
        // 既存のレコードの更新
      dbManager.updateRecord(1, "John Doe Updated", 31);
        dbManager.updateRecord(1, "John Doe Updated", 31);
   
   
      // 任意のレコードの削除
        // 任意のレコードの削除
      dbManager.deleteRecord(2);
        dbManager.deleteRecord(2);
   
   
      // 全てのレコードを読み込む
        // 全てのレコードを読み込む
      dbManager.readAllRecords();
        records = dbManager.readAllRecords();
        for (const auto& record : records) {
       // データーベースを切断
          qDebug() << "ID:" << record["id"].toInt()
       dbManager.closeConnection();
                    << "Name:" << record["name"].toString()
                    << "Age:" << record["age"].toInt();
        }
    }
    catch (const DatabaseException &e) {
       // データベース関連のエラー
       qCritical() << "Database error: " << e.what();
    }
    catch (const std::exception &e) {
      // その他のエラー
      qCritical() << "Unexpected error: " << e.what();
     }
     }
   
   

2024年8月14日 (水) 08:17時点における版

概要



MariaDB

以下の例では、MariaDBのデータベースを使用して、以下に示すようなテーブルを作成して基本的なCRUD操作を行っている。
実務では、必要に応じてフィールドを追加、または、データ型や制約を調整したりする必要がある。

  • テーブル名
    people
  • id
    INT型
    制約 1 : AUTO_INCREMENT
    制約 2 : PRIMARY KEY
    各レコードを一意に識別するための自動増分の主キー。
  • name
    VARCHAR(40)型
    制約 : NOT NULL
    名前を格納するフィールドで、最大40文字まで保存可能。
  • age
    INT型
    年齢を格納する整数型のフィールド。


また、使用時にはエラーハンドリングおよびセキュリティ対策 (SQLインジェクション対策等) を適切に実装することを推奨する。

 # CMakeLists.txtファイル
 
 cmake_minimum_required(VERSION 3.21)
 
 project(QtMariaDBProject LANGUAGES CXX)
 
 set(CMAKE_INCLUDE_CURRENT_DIR ON)
 set(CMAKE_AUTOUIC ON)
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)

 set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Sql)
 find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Sql)
 
 # MariaDB Connector/Cを検索
 find_path(MARIADB_INCLUDE_DIR mysql.h
    /usr/include/mysql
 )
 
 find_library(MARIADB_LIBRARY NAMES mariadb
    PATHS
    /usr/lib
    /usr/lib64
 )
 
 if(NOT MARIADB_INCLUDE_DIR OR NOT MARIADB_LIBRARY)
    message(FATAL_ERROR "MariaDB Connector/C not found!")
 endif()
 
 add_executable(QtMariaDBProject
    main.cpp
    DataBaseManager.h
    DataBaseManager.cpp
 )
 
 # QtライブラリとMariaDBライブラリをリンク
 target_link_libraries(QtMariaDBProject PRIVATE 
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Sql
    ${MARIADB_LIBRARY}
 )
 
 # MariaDBのヘッダファイルのディレクトリを追加
 target_include_directories(QtMariaDBProject PRIVATE
    ${MARIADB_INCLUDE_DIR}
 )


 // DataBaseManager.hファイル
 
 #include <QtSql>
 #include <QString>
 #include <QSqlError>
 #include <stdexcept>
 #include <QDebug>
 
 class DatabaseException : public std::runtime_error
 {
 public:
    DatabaseException(const QString& message) : std::runtime_error(message.toStdString()) {}
 };
 
 class DatabaseManager
 {
 public:
    DatabaseManager();
    ~DatabaseManager();
 
    bool openConnection();
    void closeConnection();
    bool createTable();
    bool insertRecord(const QString &name, int age);
    bool updateRecord(int id, const QString &name, int age);
    bool deleteRecord(int id);
    QList<QMap<QString, QVariant>> readAllRecords();
 
 private:
    QSqlDatabase db;
    void checkConnection();
    void handleDatabaseError(const QString &operation, const QSqlError &error);
 };


 // DataBaseManager.cppファイル
 
 #include "DataBaseManager.h"
 
 DatabaseManager::DatabaseManager()
 {
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("<ホスト名またはIPアドレス  例: localhost>");
    db.setDatabaseName("<データベース名  例: sampledb>");
    db.setUserName("<データベースのユーザ名>");
    db.setPassword("<データベースのユーザ名のパスワード>");
 }
 
 DatabaseManager::~DatabaseManager()
 {
    closeConnection();
 }
 
 // データベースの接続
 bool DatabaseManager::openConnection()
 {
    if (!db.open()) {
       throw DatabaseException("Failed to open database connection: " + db.lastError().text());
    }
 
    qDebug() << "Database connection opened successfully";
 }
 
 // データベースの切断
 void DatabaseManager::closeConnection()
 {
    if (db.isOpen()) {
       db.close();
       qDebug() << "Database connection closed";
    }
 }
 
 // 接続の確認
 void DatabaseManager::checkConnection()
 {
    if (!db.isOpen()) {
       throw DatabaseException("Database connection is not open");
    }
 }
 
 // エラーハンドリング処理
 void DatabaseManager::handleDatabaseError(const QString &operation, const QSqlError &error)
 {
    QString errorMessage = QString("%1 failed: %2").arg(operation, error.text());
    throw DatabaseException(errorMessage);
 }
 
 // テーブルの作成
 bool DatabaseManager::createTable()
 {
    checkConnection();
 
    QSqlQuery query;
    if (!query.exec("CREATE TABLE IF NOT EXISTS people (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40) NOT NULL, age INT)")) {
       handleDatabaseError("Create table", query.lastError());
    }
 
    qDebug() << "Table 'people' created or already exists";
 }
 
 // 単一のレコードの追加
 bool DatabaseManager::insertRecord(const QString& name, int age)
 {
    checkConnection();
 
    QSqlQuery query;
    query.prepare("INSERT INTO people (name, age) VALUES (:name, :age)");
    query.bindValue(":name", name);
    query.bindValue(":age", age);
 
    if (!query.exec()) {
       handleDatabaseError("Insert record", query.lastError());
    }
 
    qDebug() << "Record inserted successfully";
 }
 
 // 単一のレコードの更新
 bool DatabaseManager::updateRecord(int id, const QString& name, int age)
 {
    checkConnection();
 
    QSqlQuery query;
    query.prepare("UPDATE people SET name = :name, age = :age WHERE id = :id");
    query.bindValue(":id", id);
    query.bindValue(":name", name);
    query.bindValue(":age", age);
 
    if (!query.exec()) {
       handleDatabaseError("Update record", query.lastError());
    }
 
    if (query.numRowsAffected() == 0) {
       throw DatabaseException(QString("No record found with id: %1").arg(id));
    }
 
    qDebug() << "Record updated successfully";
 }
 
 // レコードの削除
 bool DatabaseManager::deleteRecord(int id)
 {
    checkConnection();
 
    QSqlQuery query;
    query.prepare("DELETE FROM people WHERE id = :id");
    query.bindValue(":id", id);
    
    if (!query.exec()) {
       handleDatabaseError("Delete record", query.lastError());
    }
 
    if (query.numRowsAffected() == 0) {
       throw DatabaseException(QString("No record found with id: %1").arg(id));
    }
 
    qDebug() << "Record deleted successfully";
 }
 
 // 全てのレコードを読み込む
 QList<QMap<QString, QVariant>> DatabaseManager::readAllRecords()
 {
    checkConnection();
 
    QSqlQuery query("SELECT * FROM people");
    QList<QMap<QString, QVariant>> results;
    
    if (!query.exec()) {
       handleDatabaseError("Read all records", query.lastError());
    }
 
    while (query.next()) {
       QMap<QString, QVariant> record;
       record["id"] = query.value(0);
       record["name"] = query.value(1);
       record["age"] = query.value(2);
       results.append(record);
    }
 
    return results;
 }


 // main.cppファイル
 
 int main()
 {
    DatabaseManager dbManager;
 
    try {
       // データーベースの接続
       dbManager.openConnection();
       
       // テーブルの作成
       dbManager.createTable();
 
       // レコードの追加
       dbManager.insertRecord("John Doe", 30);
       dbManager.insertRecord("Jane Smith", 25);
 
       // 全てのレコードを読み込む
       QList<QMap<QString, QVariant>> records = dbManager.readAllRecords();
       for (const auto& record : records) {
           qDebug() << "ID:" << record["id"].toInt() 
                    << "Name:" << record["name"].toString() 
                    << "Age:" << record["age"].toInt();
        }
 
        // 既存のレコードの更新
        dbManager.updateRecord(1, "John Doe Updated", 31);
 
        // 任意のレコードの削除
        dbManager.deleteRecord(2);
 
        // 全てのレコードを読み込む
        records = dbManager.readAllRecords();
        for (const auto& record : records) {
           qDebug() << "ID:" << record["id"].toInt() 
                    << "Name:" << record["name"].toString() 
                    << "Age:" << record["age"].toInt();
        }
    }
    catch (const DatabaseException &e) {
       // データベース関連のエラー
       qCritical() << "Database error: " << e.what();
    }
    catch (const std::exception &e) {
       // その他のエラー
       qCritical() << "Unexpected error: " << e.what();
    }
 
    return 0;
 }



SQLite3

Qtを使用してSQLite3のテーブルにアクセスする場合、まず、QSqlDatabaseクラスを使用してデータベースに接続する。

 #include <QSqlDatabase>
 #include <QSqlQuery>
 
 // SQLiteデータベースに接続する
 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
 db.setDatabaseName("<SQLiteデータベース名>.sqlite");  // データベースファイル名
 
 if (!db.open())
 {
    qDebug() << "データベースに接続できませんでした。";
    return;
 }


次に、QSqlQueryクラスを使用してクエリを実行する。
以下の例では、テーブルのデータを取得している。

 QSqlQuery query;
 query.prepare("SELECT * FROM <SQLiteデータベースのテーブル名>");
 
 if (query.exec())
 {
    while (query.next())
    {  // レコードの処理
       QString column1 = query.value(0).toString(); // 1番目の列
       QString column2 = query.value(1).toString(); // 2番目の列
 
       // 他の列も同様に取得できる
    }
 }
 else
 {
    qDebug() << "クエリの実行に失敗しました.";
 }
 
 // データベース接続を閉じる
 db.close();



SQLインジェクション対策

SQLインジェクション対策では、プレースホルダを使用してクエリを構築することが重要である。

以下の例では、":username"がプレースホルダとなり、ユーザからの入力等が直接クエリに組み込まれることなく、安全にクエリが実行される。
また、ユーザからの入力をクエリに組み込む場合、入力データを適切にサニタイズしてクエリに含めるようにすることにより、悪意のあるSQLコードが挿入されても実行されないようにする。

 QSqlQuery query;
 QString username = "user_input";  // ユーザからの入力等
 
 // プレースホルダを使用して安全にクエリを構築
 query.prepare("SELECT * FROM users WHERE username = :username");
 query.bindValue(":username", username);
 
 if (query.exec())
 {
    while (query.next())
    {  // レコードの処理
       // ...略
    }
 }
 else
 {
    qDebug() << "クエリの実行に失敗しました.";
 }