您的位置:首页 > 编程语言 > Qt开发

QT SQL 变量 条件查询 插入数据

2013-06-03 22:00 567 查看
source: http://blog.csdn.net/liang890319/article/details/7079388
QT SQL 變量值  條件查詢  插入數據
(本文只是总结网络上的教程)
在操作数据库时SQL语句中难免会用到变量比如
在條件值已知的情況下
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
SELECT * FROM Persons WHERE [code]FirstName='Bush'
[/code]
在条件值是變量的情況下
INSERT INTO table_name (列1, 列2,...) VALUES (變量1, 變量2,....)
SELECT * FROM Persons WHERE [code]FirstName='
變量
'
[/code]
或者SELECT * FROM Persons WHERE [code]變量
='
變量
'
[/code]
 
 
方法据我所知有两种
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  转 QStringint a=10;QString b;b=QString::number(a)QString 转intQString 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)

查詢:
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery(“select * from student”);
插入:
 

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

查询
     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字符串法)

查詢:
插入:
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sqlite
相关文章推荐