IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> QT odbc mysql -> 正文阅读

[大数据]QT odbc mysql

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //显示可用驱动
    //qDebug()<<"available drivers:";
    //QStringList drivers = QSqlDatabase::drivers();
    //foreach(QString driver, drivers)
    //    qDebug()<<driver;

    //使用ODBC连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("127.0.0.1");
    db.setPort(3306);
    db.setDatabaseName("mysql");
    db.setUserName("root");
    db.setPassword("123456");
    bool ok = db.open();
    if (ok){
       qDebug()<<"open databease sucess";
    }
    else {
        qDebug()<<"error open database because"<<db.lastError().text();
    }


    QSqlQuery query1 = QSqlQuery(db);
    //创建数据库
    //query1.exec("create database if not exists tmysql");
    //删除数据库
    //if(query1.exec("drop database if exists tmysql;")) qDebug()<<"drop database success";
    //使用数据库
    query1.exec("use tmysql;");

    //创建表
    //if(query1.exec("create table if not exists stu \
    //            (stuid int primary key,\
    //            stuname varchar(50) not null,\
    //            studept varchar(20));"))
    //{
    //    qDebug()<<"create table success";
    //}

    //删除表
    //if(query1.exec("drop table student;"))
    //{
    //   qDebug()<<"drop table success";
    //}

    //修改表名
    //query1.exec("alter table stu rename student;");

    //添加属性列
    //query1.exec("alter table student add sex varchar(10) not null;");

    //更改属性列名
    //query1.exec("alter table student change sex stusex varchar(10) not null;");

    //修改属性列的数据类型
    //query1.exec("alter table student modify stusex varchar(20) not null;");

    //删除表的属性
    //query1.exec("alter table student drop stusex;");

    //删除表
    //query1.exec("drop table student;");

    //插入表数据
    //if(query1.exec("insert into stu (stuid,stuname,studept) values (20211,'小赵','cs');"))
    //{
    //    qDebug()<<"insert table data sucess";
    //}

    //if(query1.exec("insert into stu (stuid,stuname,studept) values (20212,'小王','kj');"))
    //{
    //    qDebug()<<"insert table data sucess";
    //}

    //if(query1.exec("insert into stu (stuid,stuname,studept) values (20213,'小李','cf');"))
    //{
    //    qDebug()<<"insert table data sucess";
    //}

    //if(query1.exec("insert into stu (stuid,stuname,studept) values (20214,'小刘','gc');"))
    //{
    //    qDebug()<<"insert table data sucess";
    //}

    //查找表数据
    //if(query1.exec("select * from stu where stuid = 20211"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString();
    //    }
    //}

    //like语句
    //if(query1.exec("select * from stu where studept like '%c';"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString();
    //    }
    //}

    //union语句
    //if(query1.exec("select * from stu where stuId=20211 union select * from stu where stuId=20214;"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString();
    //    }
    //}


    //if(query1.exec("select * from stu;"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString();
    //    }
    //}

    //修改表数据
    //if(query1.exec("update stu set stuname = '小魏' where stuid = 20211;"))
    //{
    //    qDebug()<<"update table success";
    //}

    //删除表数据
    //if(query1.exec("delete from stu where stuid = 20213;"))
    //{
    //     qDebug()<<"delete sucess";
    //}

    //order by
    //if(query1.exec("select * from stu order by stuId desc;"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString();
    //    }
    //}

    //if(query1.exec("select studept, count(*) from stu group by studept;" ))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toString()
    //                <<query1.value(1).toInt();
    //    }
    //
    //}

    //内连接
    //if(query1.exec("select stuId,deptId,stu.studept,dept.deptname from stu inner join dept on stu.studept = dept.deptname;"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString()
    //                <<query1.value(3).toString();
    //    }
    //}

    //右外连接
    //if(query1.exec("select stuId,deptId,stu.studept,dept.deptname from stu right join dept on stu.studept = dept.deptname;"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString()
    //                <<query1.value(3).toString();
    //    }
    //}

    //右外连接
    //if(query1.exec("select stuId,deptId,stu.studept,dept.deptname from stu right join dept on stu.studept = dept.deptname;"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toInt()
    //                <<query1.value(1).toString()
    //                <<query1.value(2).toString()
    //                <<query1.value(3).toString();
    //    }
    //}

    //值为NULL
    //if(query1.exec("select * from record where netCount is null"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toString()
    //                <<query1.value(1).toInt();
    //    }
    //}


    //值不为NULL
    //if(query1.exec("select * from record where netCount is not null"))
    //{
    //    while(query1.next())
    //    {
    //        qDebug()<<query1.value(0).toString()
    //                <<query1.value(1).toInt();
    //    }
    //}

    //事务
    //query1.exec("begin;");

    //query1.exec("insert into record (netName,netCount) values ('www.motus.com',5);");

    //query1.exec("rollback;");

    //query1.exec("commit;");

    db.close();
}

MainWindow::~MainWindow()
{
    delete ui;
}

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-12-24 18:32:57  更:2021-12-24 18:35:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/20 10:25:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码