• Qt mysql客户端,测试


    Qt mysql客户端,测试

    创建数据库和表

    ubuntu 20.04
    打开终端,登录mysql

    mysql -u root -p
    
    • 1

    回车,然后输入密码,进入mysql

    命令创建
    数据库 testdb

    create DATABASE testdb;
    
    • 1

    数据表 wmxtb

    CREATE TABLE IF NOT EXISTS `wmxtb`(
       `id` INT UNSIGNED AUTO_INCREMENT,
       `user` VARCHAR(100) NOT NULL,
       `password` VARCHAR(40) NOT NULL,
       `date` DATE,
       PRIMARY KEY ( `id` )
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    插入测试数据

    insert into `wmxtb`
    (`id`,`user`,`password`,`date`)
    values
    (1,'user1','u1','2022/8/1');
    
    insert into `wmxtb`
    (`id`,`user`,`password`,`date`)
    values
    (2,'user2','u2','2022/3/1');
    
    
    insert into `wmxtb`
    (`id`,`user`,`password`,`date`)
    values
    (3,'wmx','wmx123','2022/3/26');
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    查看结果:
    在这里插入图片描述

    QT 程序测试:

    sql.h

    #ifndef SQL_H
    #define SQL_H
    
    #include 
    
    #include 
    #include 
    #include 
    
    #include 
    
    // ubuntu host
    extern QString IP;
    extern int PORT;
    extern QString dbName;
    extern QString tbName;
    
    
    namespace  NS_SQL
    {
    
        /*!
         * \brief connectDb
         * \return
         */
        int connectDb();
    
        /*!
         * \brief queryTb
         */
        void queryTb();
    
        /// \brief validUser
        /// \param user
        bool validUser(const QString &user,const QString & password);
    
    };
    
    #endif // SQL_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    sql.cpp

    #include "sql.h"
    
    
    // ubuntu host
    QString IP= "192.168.2.100";
    int PORT = 3306;
    QString dbName = "testdb";
    QString tbName = "wmxtb";
    
    
    int NS_SQL::connectDb()
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName(IP);
        db.setPort(PORT);
        db.setDatabaseName(dbName);
        db.setUserName("root");
        db.setPassword("xxx"); //你自己的 mysql root登录密码, 就是 mysql -u root -p 输入的密码
        bool ok = db.open();
        if (ok){
            qDebug()<<"open db success";
        }
        else {
            qDebug()<<"open db error : "<<db.lastError().text();
            return -1;
        }
    
        return 0;
    
    }
    
    
    void NS_SQL::queryTb()
    {
        QSqlQuery query;
        QString cmd = "select *from " + tbName;
        qDebug()<<"cmd : "<<cmd;
        query.exec(cmd);
        while(query.next())   //遍历完为false
        {
            qDebug()<<query.value("id").toInt();
            qDebug()<<query.value("user").toString();
            qDebug()<<query.value("password").toString();
            qDebug()<<query.value("date").toString();
        }
    }
    
    
    bool NS_SQL::validUser(const QString &user,const QString & password)
    {
        QSqlQuery query;
        QString cmd = QString("select * from %1 where user='%2' and password='%3'").arg(tbName).arg(user).arg(password);
        qDebug()<<"cmd : "<<cmd;
        query.exec(cmd);
        bool find = false;
        while(query.next())
        {
            QString pswd = query.value("password").toString();
            if(pswd == password)
            {   find = true;
                break;
            }
        }
    
        if(find)
        {
            qDebug()<<"id"<<query.value("id").toInt();
            qDebug()<<"user"<<query.value("user").toString();
            qDebug()<<"password"<<query.value("password").toString();
            qDebug()<<Q_FUNC_INFO<<"login success!!!";
            return true;
        }
    
        qDebug()<<Q_FUNC_INFO<<"user or password error!!!";
        return false;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    python字符串格式化函数 - format
    Apache Ranger 是什么?
    dockerfile基于apline将JDK20打包成镜像
    【JVM技术专题】深入分析内存布局及GC原理分析「上卷」
    Himall商城- web私有方法
    leetcode136,137,260:只出现一次的数字 | || |||
    【概率论与数理统计】
    XCTF1-web disabled_button weak_auth view_source cookie backup
    Activiti可视化流程管理器
    OpenCV图像特征提取学习一,Harris图像角点检测算法
  • 原文地址:https://blog.csdn.net/WMX843230304WMX/article/details/126909174