• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

QT-sqlite创建表整体流程

武飞扬头像
雨季不再来day
帮助1

一、数据库操作流程

1、在项目管理文件中(.pro文件)中添加数据模块

QT =sql

2、连接和打开数据库

创建based.h数据库驱动文件

  1.  
    //basedb.h
  2.  
     
  3.  
    #ifndef BASEDB_H
  4.  
    #define BASEDB_H
  5.  
    #include <QSqlDatabase>
  6.  
    #include <QSqlQuery>
  7.  
    #include <QDebug>
  8.  
    #include <QSqlError>
  9.  
    class basedb
  10.  
    {
  11.  
    public:
  12.  
    //数据库对象
  13.  
    QSqlDatabase db;
  14.  
    basedb();
  15.  
    ~basedb();
  16.  
    };
  17.  
     
  18.  
    #endif // BASEDB_H
  19.  
     
  20.  
    //basedb.cpp
  21.  
    #include "basedb.h"
  22.  
     
  23.  
    basedb::basedb()
  24.  
    {
  25.  
    //加载sqlite数据库驱动
  26.  
    db = QSqlDatabase::addDatabase("QSQLITE");
  27.  
    //生成数据库文件
  28.  
    db.setDatabaseName("form.db");
  29.  
    //开启数据库
  30.  
    db.open();
  31.  
    }
  32.  
     
  33.  
    basedb::~basedb()
  34.  
    {
  35.  
     
  36.  
    }
学新通

3、继承basedb.h创建表

  1.  
    //score.h
  2.  
     
  3.  
    #ifndef SCORE_H
  4.  
    #define SCORE_H
  5.  
    #include "basedb.h"
  6.  
     
  7.  
    class score:public basedb
  8.  
    {
  9.  
    public:
  10.  
    int id;
  11.  
    QString name;
  12.  
    QString uclass;
  13.  
    int math;
  14.  
    int chinese;
  15.  
    int English;
  16.  
    int physics;
  17.  
    int chemics;
  18.  
    int biology;
  19.  
    score();
  20.  
    void insert_score(int id,QString name,QString uclass,int math,int chinese,int English,int physics,int chemics,int biology);
  21.  
    void delete_row(int id);
  22.  
    void select_score();
  23.  
    void update(int id,QString name);
  24.  
    };
  25.  
     
  26.  
    //score.cpp
  27.  
    #include "score.h"
  28.  
     
  29.  
    //创建表
  30.  
    score::score()
  31.  
    {
  32.  
    QSqlQuery sql_query;
  33.  
    QString create_table = "create table if not exists score (id INTEGER PRIMARY KEY,name TEXT NOT NULL,uclass TEXT,math INTEGER,chinese INTEGER,English INTEGER,physics INTEGER,chemics INTEGER,biology INTEGER)";
  34.  
    sql_query.prepare(create_table);
  35.  
    if(!sql_query.exec())
  36.  
    {
  37.  
    qDebug() << "Error: Fail to create table." << sql_query.lastError();
  38.  
    }
  39.  
    else
  40.  
    {
  41.  
    qDebug() << "Table created!";
  42.  
    }
  43.  
     
  44.  
    }
  45.  
    //插入
  46.  
    void score::insert_score(int id,QString name,QString uclass,int math,int chinese,int English,int physics,int chemics,int biology)
  47.  
    {
  48.  
    QSqlQuery sql_query;
  49.  
    sql_query.prepare("insert into score (id, name,uclass,math,chinese,English,physics,chemics,biology) "
  50.  
    "values(:id,:name,:uclass,:math,:chinese,:English,:physics,:chemics,:biology)");
  51.  
    sql_query.addBindValue(id);
  52.  
    sql_query.addBindValue(name);
  53.  
    sql_query.addBindValue(uclass);
  54.  
    sql_query.addBindValue(math);
  55.  
    sql_query.addBindValue(chinese);
  56.  
    sql_query.addBindValue(English);
  57.  
    sql_query.addBindValue(physics);
  58.  
    sql_query.addBindValue(chemics);
  59.  
    sql_query.addBindValue(biology);
  60.  
     
  61.  
    if(sql_query.exec())
  62.  
    {
  63.  
    qDebug() << "insert success" << endl;
  64.  
    }else{
  65.  
    qDebug() << "insert error" << sql_query.lastError() << endl;
  66.  
    }
  67.  
    }
  68.  
     
  69.  
    //筛选
  70.  
    void score::select_score()
  71.  
    {
  72.  
    QSqlQuery sql_query;
  73.  
    QString sql = "SELECT * FROM score WHERE English>120";
  74.  
    sql_query.prepare(sql);
  75.  
    if(sql_query.exec())
  76.  
    {
  77.  
    qDebug() << "query a row success" << endl;
  78.  
    while(sql_query.next())
  79.  
    {
  80.  
    int id = sql_query.value(0).toInt();
  81.  
    QString name = sql_query.value(1).toString();
  82.  
    QString uclass = sql_query.value(2).toString();
  83.  
    int math = sql_query.value(3).toInt();
  84.  
    int chinese = sql_query.value(4).toInt();
  85.  
    int English= sql_query.value(5).toInt();
  86.  
    int physics = sql_query.value(6).toInt();
  87.  
    int chemics = sql_query.value(7).toInt();
  88.  
    int biology = sql_query.value(8).toInt();
  89.  
    qDebug()<<id<<name<<uclass<<math<<chinese<<English<<physics<<chemics<<biology;
  90.  
    }
  91.  
    }
  92.  
    }
  93.  
    //删除
  94.  
    void score::delete_row(int id)
  95.  
    {
  96.  
    QSqlQuery sql_query;
  97.  
    QString delete_sql = "DELETE FROM score WHERE id = ?";
  98.  
    sql_query.prepare(delete_sql);
  99.  
    sql_query.addBindValue(id);
  100.  
    if(sql_query.exec()){
  101.  
    qDebug()<< "delete success" << endl;
  102.  
    }else{
  103.  
    qDebug()<< "delete error" << sql_query.lastError() << endl;
  104.  
    }
  105.  
    }
  106.  
    //修改
  107.  
    void score::update(int id,QString name)
  108.  
    {
  109.  
    QSqlQuery sql_query;
  110.  
    QString update_sql = "UPDATE score SET name=? WHERE id=?";
  111.  
    sql_query.prepare(update_sql);
  112.  
    sql_query.addBindValue(name);
  113.  
     
  114.  
    if(sql_query.exec()){
  115.  
    qDebug() << "update success" << endl;
  116.  
    }else{
  117.  
    qDebug() << "update error" << sql_query.lastError() << endl;
  118.  
    }
  119.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhehec
系列文章
更多 icon
同类精品
更多 icon
继续加载