SQL 變量 條件查詢 插入數據


(本文只是總結網絡上的教程)

 

 

在操作數據庫

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

查詢:
插入:
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM