zoukankan      html  css  js  c++  java
  • mys_qt

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    QT       += core gui  sql
    #include <QMainWindow>
    #include <QtSql>
    #include <QVariant>
    #include <QDebug>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
    private slots:
        void on_listButton_clicked();
    
    
        void on_add_clicked();
        
    private:
        Ui::MainWindow *ui;
        //数据库句并
        QSqlDatabase db;
    
    
    };
    
    
    #endif // MAINWINDOW_H

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_listButton_clicked()
    {
        this->db = QSqlDatabase::addDatabase("MYSQL");
        this->db.setHostName("localhost");
        this->db.setUserName("root");
        this->db.setPassword("selen4955");
        this->db.setDatabaseName("student");
        bool ok = db.open();
        if(ok)
            {
    
    
            
        }
        else
            {
    
    
            qDebug()<<"error open database because"<<this->db.lastError().text();
        }
        //执行命令
        QSqlQuery query;
        query.exec("select * from student");
       //返回一个结构集
        while(query.next())
            {
            // query.value  是QVariant类型
            int id=query.value(0).toInt();
            QString name=query.value(1).toString();
            qDebug()<<id<<name;
        }
        this->db.close();
    }
    
    
    void MainWindow::on_add_clicked()
    {
       if(this->db.isOpen())
           {
           
           qDebug()<<"db is open";
       }
       else
           {//错误信息
           
            qDebug()<<"error open database because"<<this->db.lastError().text();
       }
       this->db = QSqlDatabase::addDatabase("MYSQL");
       this->db.setHostName("localhost");
       this->db.setUserName("root");
       this->db.setPassword("selen4955");
       this->db.setDatabaseName("student");
       bool ok = db.open();
       if(ok)
           {
           QSqlQuery query;
           //先匹配在绑定
           
           
           
           
                 query.prepare("INSERT INTO person (id, name) "
                               "VALUES (:id, :name)");
                 query.bindValue(":id",ui->lineEdit->text());
                 query.bindValue(":forename", ui->lineEdit_2->text());
                
                 bool ok =query.exec();
                 if(ok)
                     {
                     
                     qDebug()<<"insert ok";
                 }
                 else
                     {
                     
                     qDebug()<<"insert error";
                 }
           
           
       }
       else
           {
           
           qDebug()<<"error open database because"<<this->db.lastError().text();
       }
       
    }


  • 相关阅读:
    【WP开发】记录屏幕操作
    【.NET深呼吸】清理对象引用,有一个问题容易被忽略
    【WP开发】JSON数据的读与写
    【WP8.1开发】RenderTargetBitmap类的特殊用途
    【WP 8.1开发】How to 图像处理
    【WP8.1开发】用手机来控制电脑的多媒体播放
    【WP 8.1开发】如何动态生成Gif动画
    【WP8.1开发】基于应用的联系人存储
    使用awk处理文本
    PHP数组和字符串的处理函数汇总
  • 原文地址:https://www.cnblogs.com/countryboy666/p/11072725.html
Copyright © 2011-2022 走看看