qt数据库

qt中数据库的使用

头文件

cpp 复制代码
#include <QSqlDatabase>
#include <QSqlQuery>

库对象定义,连接库,输出库中表名

cpp 复制代码
  QSqlDatabase db;
 db = QSqlDatabase::addDatabase("QSQLITE");
 // db.setDatabaseName("D:/qtprogrem/kongjian/power.db");//绝对路径
 db.setDatabaseName("../kongjian/power.db");//相对路径
  db.open();
  //输出库中的表名
  QStringList tableList = db.tables();
  foreach (QString tableName, tableList) {
      qDebug() << "Table name:" << tableName;
  }

表的增删改查

//增

cpp 复制代码
//增
  QString name="测试";
 QString SelectString= QString("insert into t_warhead_fusee_type(id,name,introduce) values(%1,'%2','介绍')").arg(5).arg(name.replace("'","''"));
   QSqlQuery query(SelectString,db);
    qDebug()<<SelectString;
//删
    SelectString = QString("delete from t_warhead_fusee_type where id=5");
     query.exec(SelectString);
//改
     SelectString = QString("update t_warhead_fusee_type set name='改' where id=5");
      query.exec(SelectString);
//查
  SelectString = QString("select * from t_warhead_fusee_type");
   query.exec(SelectString);
  while (query.next()) {
      QString name=query.value("name").toString();
      QString id =query.value("id").toString();
      QString introduce=query.value("introduce").toString();
      qDebug()<<id<<name<<introduce;
      comboBox->addItem(name);
  }*
相关推荐
NineData6 小时前
NineData 迁移评估功能正式上线
数据库·dba
NineData11 小时前
数据库迁移总踩坑?用 NineData 迁移评估,提前识别所有兼容性风险
数据库·程序员·云计算
赵渝强老师13 小时前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
blasit14 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
全栈老石18 小时前
拆解低代码引擎核心:元数据驱动的"万能表"架构
数据库·低代码
倔强的石头_2 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou643 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤4 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区5 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1775 天前
《从零搭建NestJS项目》
数据库·typescript