(本文只是总结网络上的教程)

在操作数据库时

SQL语句中难免会用到变量

比如

在條件值已知的情況下
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
SELECT * FROM Persons WHERE FirstName='Bush'
在条件值是變量的情況下
INSERT INTO table_name (列1, 列2,...) VALUES (變量1, 變量2,....)
SELECT * FROM Persons WHERE FirstName='變量'
或者SELECT * FROM Persons WHERE 變量='變量'
 
 
方法据我所知有两种
1:用query的绑定特性来做
2:利用字符串特性,来组合SQL字符串(+,&,arg())详情可参考qstring,string用法
(提示:qstring在执行sql语句时相当有用,当深入研究之
http://developer.qt.nokia.com/doc/qt-4.8/qstring.html
http://www.kuqin.com/qtdocument/qstring.html
http://caterpillar.onlyfun.net/Gossip/Qt4Gossip/QString.html
http://ibeyond.blog.51cto.com/1988404/373948
百度文库也有个很好的官方ppt  讲的QT 数据类型  也可以参考之
)

第一步:
简单介绍下qstring常用操作

1-----------------組合1-----

把str添加到字符串中并且返回结果的引用。

string = "Test";

string.append( "ing" );       // string == "Testing"

等于operator+=()。

2-------------------組合2-------

QString firstName( "Joe" );

QString lastName( "Bloggs" );

QString fullName;

fullName = QString( "First name is '%1', last name is '%2'" )

.arg( firstName )

.arg( lastName );

// fullName == First name is 'Joe', last name is 'Bloggs'

QString str;

str = QString( "Decimal 63 is %1 in hexadecimal" )

.arg( 63, 0, 16 );

// str == "Decimal 63 is 3fin hexadecimal"

3------------------組合3--------+,+=

QString str = "1234";

cout << &str << endl;

str += str;

cout << qPrintable(str) <<endl;

str = str + "5678";

cout << qPrintable(str) <<endl;

4----------------檢測是否為空

QString a( "" );

a.isEmpty();        // 真

a.isNull();         // 假

QString b;

b.isEmpty();        // 真

b.isNull();         // 真

如果它不是零字符串,返回真,否则返回假。

QString name =getName();

if ( !name )

name = "Rodney";

5------------------转换
int  转 QString
int a=10;
QString b;
b=QString::number(a) QString 转int
QString a="120"
int b;
b=a.toInt()


第二步:
回顧一下QT下操作,顯示數據庫的方法
底層數據庫:SQLITE,MYSQL,MSSQL,ACCESS,ORACLE…….
中間層處理:QUERY,QUERYMODEL,TABLEMODEL,RetinoalTABLEMODEL
頂層顯示:DEBUG(MSGBOX),TABLEVIEW,TREEVIEW,TABLEWIDGET,COMBOX
 

第三步:


 
下面總結下在不同中間層的情況下,待變量SQL語句的執行方法
這裡只介紹查詢和插入的方法,關於更新方法類似
1----------------------使用query做中間層
SQL查詢:
SQL插入:
2----------------------使用querymodel做中間層
查詢:
插入:
 
3----------------------使用tablemodel做中間層
查詢:
插入:
 
4----------------------使用T-tablemodel做中間層
查詢:
插入:

1----------------------使用query做中間層(绑定或组合SQL字符串)

SQL查詢:(主要是用的綁定,其他捆綁方法可查詢幫助文檔,或者參考插入的sql語句格式)
QSqlQuery query; 
query.prepare("select name from student where id = ?"); 
int id = ui->linetext->value(); //从界面获取id的值 
query.addBindValue(id); //将id值进行绑定 
query.exec();
SQL插入:
(ODBC)
QSqlQuery query; 
query.prepare("insert into student (id, name) values (:id, :name)"); 
query.bindValue(0, 5); 
query.bindValue(1, "sixth"); 
query.exec();
或者用名稱進行索引
query.prepare("insert into student (id, name) values (:id, :name)"); 
query.bindValue(":id", 5); 
query.bindValue(":name", "sixth"); 
query.exec();
(ORACLE)
query.prepare("insert into student (id, name) values (?, ?)");
query.bindValue(0, 5);
query.bindValue(1, "sixth"); 
query.exec();
或者省去索引
query.prepare("insert into student (id, name) values (?, ?)"); 
query.addBindValue(5); 
query.addBindValue("sixth"); 
query.exec();
批量插入:
QSqlQuery q; 
q.prepare(“insert into student values (?, ?)”); 
QVariantList ints; ints << 10 << 11 << 12 << 13; 
q.addBindValue(ints); 
 
QVariantList names; names << “xiaoming” << “xiaoliang” << “xiaogang” << 
QVariant(QVariant::String); //最后一个是空字符串,应与前面的格式相同 
q.addBindValue(names); 
 
if (!q.execBatch()) //进行批处理,如果出错就输出错误 
qDebug() << q.lastError();



2----------------------使用querymodel做中間層(组合SQL字符串法可参考3)

    QString name = userNameLine->text();
    QString passwd = userPwdLine->text();
    QString sql = "select name, password from users where name = '"
        + name + "'and password ='" + passwd + "'";
查詢:
QSqlQueryModel *model = new QSqlQueryModel; 
model->setQuery(“select * from student”);
插入:
 

3----------------------使用tablemodel做中間層(组合sql字符串法)

    QString name = userNameLine->text();
    QString passwd = userPwdLine->text();
    QString sql = "select name, password from users where name = '"
        + name + "'and password ='" + passwd + "'";
查询
     QSqlTableModel *model = new QSqlTableModel;
     model->setTable("employee");
     model->setEditStrategy(QSqlTableModel::OnManualSubmit);
     model->select();
     model->removeColumn(0); // don't show the ID
     model->setHeaderData(0, Qt::Horizontal, tr("Name"));
     model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
 
     QTableView *view = new QTableView;
     view->setModel(model);
     view->show();
條件查詢:
QString name = ui->lineEdit->text(); 
model->setFilter(QObject::tr(“name = ‘%1′”).arg(name)); //根据姓名进行筛选 
model->select(); //显示结果
分類
model->setSort(0,Qt::DescendingOrder); 
model->select();
插入:
int rowNum = model->rowCount(); //获得表的行数 
int id = 10; model->insertRow(rowNum); //添加一行 
model->setData(model->index(rowNum,0),id);


4----------------------使用Relationaltablemodel做中間層(组合SQL字符串法)

查詢:
插入:
 

SQL 变量 条件查询 插入数据的更多相关文章

  1. C# 将Access中时间段条件查询的数据添加到ListView中

    C# 将Access中时间段条件查询的数据添加到ListView中 一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Col ...

  2. util-C# 复杂条件查询(sql 复杂条件查询)查询解决方案

    ylbtech-funcation-util:  C# 复杂条件查询(sql 复杂条件查询)查询解决方案 C# 复杂条件查询(sql 复杂条件查询)查询解决方案 1.A,Ylbtech.Model返回 ...

  3. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  4. 使用变量向SQL Server 2008中插入数据

    QT通过ODBC连接数据库SQL Server 2008,进行数据插入时遇到的问题: 先把数据存入变量中,如何使用变量进行插入?插入语句该怎么写? QSqlQuery query(db); query ...

  5. SQl server 关于重复插入数据的测试

    最近发布的脚本,有那种防止重复插入数据(包括存在时更新,不存在是插入的处理,判断的方向可能与下面的示例相反) 使用类似下面的 SQL declare @id int, @value int if no ...

  6. SQL Server 2008 批量插入数据时报错

    前几天在SQL Server 2008同步产品数据时,总是提示二进制文本被截断的错误,但是经过检查发现数据都符合格式要求. 百思不得其解,单独插入一条条数据则可以插入,但是批量导入则报错. 批量导入代 ...

  7. 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中

    一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Columns集合中添加表头中的文字. 二.利用代码给ListView添加 ...

  8. 解决pl/sql developer中数据库插入数据乱码问题

    最近学习SSM项目开发,用到oracle数据库, 使用管理软件PL/sql developer往数据库表中插入数据时记录乱码.  结果如下: 可以看到中文数据都乱码成了???????问号, 看了网上各 ...

  9. SQL多条件查询安全高效比较

    ALTER PROCEDURE _tmp @ID VARCHAR(50), @PN VARCHAR(50), @Type INT AS BEGIN /************************* ...

随机推荐

  1. 第130天:移动端-rem布局

    一.关于布局方案 当拿到设计师给的UI设计图,前端的首要任务就是布局和样式,相信这对于大部分前端工程师来说已经不是什么难题了.移动端的布局相对PC较为简单,关键在于对不同设备的适配.之前介绍了一篇关于 ...

  2. ZOJ3529_A Game Between Alice and Bob

    题目的意思是给你若干个数字,两个游戏者轮流操作,每次可以将该数变为一个小于当前的一个约数,无法操作的游戏者fail. 和其他的博弈题目大同小异吧. 不同点有两个,逐一分析吧. 一.每次改变一个数只能改 ...

  3. hive表信息查询:查看表结构、表操作等--转

    原文地址:http://www.aboutyun.com/forum.PHP?mod=viewthread&tid=8590&highlight=Hive 问题导读:1.如何查看hiv ...

  4. 【刷题】HDU 4966 GGS-DDU

    Problem Description Do you think this is a strange problem name? That is because you don't know its ...

  5. Classical Binary Search

    Find any position of a target number in a sorted array. Return -1 if target does not exist. 与题目 Firs ...

  6. Dirichlet 卷积学习笔记

    Dirichlet 卷积学习笔记 数论函数:数论函数亦称算术函数,一类重要的函数,指定义在正整数集上的实值或复值函数,更一般地,也可把数论函数看做是某一整数集上定义的函数. 然而百科在说什么鬼知道呢, ...

  7. bzoj1912【Apio2010】patrol 巡逻

    题解: 显然需要分类讨论了,首先理解k==0即原图时按照dfs序来说 , 每条边至少走两次: k==1,相当于可以省去dfs回溯时第二次走过某条路径的浪费,所以答案是k==0的答案-直径 : k==2 ...

  8. Kubernetes Job配置

    我们知道使用kubernetes的rc或者rs创建的pod,kubernetes会实时监控其健康状态,如果发现pod挂掉以后,会自动启动一个新的,让pod的数量始终保持在指定的replicas上.那么 ...

  9. 第4章-Vue.js 交互及实例的生命周期

    一.学习目标 了解实例生命周期的过程 理解钩子函数的作用 掌握Vue.js过滤器的使用方法 (重点) 能够使用网络请求进行前后端交互 (重点.难点) 二.交互的基本概念 2.1.前端和后端的概念 说明 ...

  10. tf.session.run()单函数运行和多函数运行区别

    tf.session.run()单函数运行和多函数运行区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me problem instruction sess.run([a,b]) # (1)同 ...