Qt与Sqlite3

Operating procedures: 

(1) Connect to database

(2) Perform add, delete, modify and check operations

(3) Close the database

Example:

Reference:Qt operation SQLite database_qt sqlite database operation_houxian1103's blog-CSDN blog 

Let’s talk about the solution to the problem of QSqlQuery::exec: database not open_qt database not open-CSDN blog

    if (QSqlDatabase::contains("connection"))
    {
        database = QSqlDatabase::database("connection");
    }
    else {
        database = QSqlDatabase::addDatabase("QSQLITE","connection");
        database.setDatabaseName("identifier.sqlite");
    }
    if (!database.open())
    {
        qDebug() << "Error: Failed to connect database." << database.lastError();
    }
    else {
        QSqlQuery query(database);
        query.exec("select * from kmd_menu");
        if (!query.exec())
        {
            qDebug() << "Error:" << query.lastError();
        }
        else {
            while (query.next())
            {
                qDebug() << query.value("name").toString();
            }
        }
        database.close();
    }

 QSqlDatabase class study notes_qsqldatabase::adddatabase-CSDN blog

think:

QSqlDatabase classObject representsa database connection.

Databases in Qt are distinguished by connection names.

    QSqlDatabase b = QSqlDatabase::addDatabase("QSQLITE");
    qDebug() << b.connectionName();

Result: "qt_sql_default_connection"

Note: There is a problem with this program. There is no removeDatabase (connection name) at the end.

Guess you like

Origin blog.csdn.net/weixin_51883798/article/details/134799320