QT's SQL database

Source: Internet
Author: User
Tags bulk insert

----------------------------

http://blog.csdn.net/reborntercel/article/details/6991147

http://blog.csdn.net/fzu_dianzi/article/details/6959268

--------------------------------

Note that when the Query.exec ("SELECT * FROM student") has just been executed, query is pointing to the result set, and we can take advantage of query.next (), when the code is executed for the first time, Query points to the first record in the result set. Of course, we can also use the Seek (0) function or the first () function to point the query to a result set of record one. However, in order to save memory overhead, the recommended method is to Query.exec ("SELECT * from Student"), this line of code is preceded by Query.setforwardonly (true), this code, then only use Next () and Seek () Function.

#include <QtSql>
QT + = SQL
The Qsqldatabase class implements the operation of the database connection
Qsqlquery class Execution SQL statement
Qsqlrecord class encapsulates database all records

Qsqldatabase class

[CPP]View Plaincopyprint?
  1. Qsqldatabase db = Qsqldatabase::adddatabase ("Qoci");
  2. Db.sethostname ("localhost"); //Database host name
  3. Db.setdatabasename ("Scott"); //Database name
  4. Db.setusername ("Stott"); //database user name
  5. Db.setpassword ("Tiger"); //Database password
  6. Db.open (); //Open database connection
  7. Db.close (); //Release database connection

Create a database file

[CPP]View Plaincopyprint?
    1. Qsqldatabase db = Qsqldatabase::adddatabase ("Qsqlite");
    2. Db.setdatabasename ("database.db");
    3. if (!db.open ())
    4. {
    5. Qdebug ("database cannot be opened");
    6. }
    7. return false;
[CPP]View Plaincopyprint?
    1. Create a table and insert two data after creating a database file
[CPP]View Plaincopyprint? < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Qsqlquery query;
    2. Query.exec ("CREATE TABLE student (ID INTEGER PRIMARY KEY autoincrement,
    3. Name nvarchar (), age int) "); //id Automatic Increase
    4. Query.exec ("INSERT into student values (1, ' xiaoming ', 14)");
    5. Query.exec ("INSERT into student values (2, ' Xiao Wang ', 15)");


Qsqlquery class

Inserting values into a database operation

First, insert directly with SQL statement (refer to above)

Second, using preprocessing method insertion (Oracle syntax and ODBC syntax)

Suitable for inserting multiple records, or to avoid converting values to strings (that is, correctly escaping), call the Prepare () function to specify a query that contains placeholders, and then bind the values to be inserted

Oracle Syntax

[CPP]View Plaincopyprint?
    1. Qsqlquery query;
    2. Query.prepare ("INSERT into T_student (name, age) VALUES (: Name,: Age)"); //Prepare to execute SQL query
    3. Query.bindvalue (": Name", "Xiao Wang"); //Bind the value to be inserted
    4. Query.bindvalue (": Age", 11);
    5. Query.exec ();


ODBC syntax

[CPP]View Plaincopyprint? < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Qsqlquery query;
    2. Query.prepare ("INSERT into T_student (name,age) VALUES (?,?)"); //Prepare to execute SQL query
    3. Query.addbindvalue ("Xiao Wang"); //Bind the value to be inserted
    4. Query.bindvalue (11);
    5. Query.exec ();

Third, BULK INSERT into the database

[CPP]View Plaincopyprint?
  1. Qsqlquery query;
  2. Query.prepare ("INSERT into student values (?,?)");
  3. Qvariantlist names;
  4. Names << "Xiao Wang" << "Xiao Ming" << "Xiao Zhang" << "Xiao Xin"; //If you want to submit an empty string, use Qvariant (qvariant::string) instead of the name
  5. Query.addbindvalue (names);
  6. Qvariantlist ages;
  7. Ages << << << << 11;
  8. Query.addbindvalue (ages);
  9. if (!q.execbatch ()) //batch, output error if error occurs
  10. Qdebug () << q.lasterror ();


Querying database operations

[CPP]View Plaincopyprint?
    1. Qsqlquery query;
    2. Query.exec ("select * from T_student"); //The result of the query may be more than one record, so we call it a result set
    3. while (Query.next ())
    4. {
    5. QString name = Query.value (0). toString (); the results of the 1th field (counting from 0) are recorded in article I
    6. int age = Query.value (0). ToInt (); //Take the result of section I record 2nd field
    7. // ... Working with name,age variable data
    8. }


Seek (int n): query points to the nth record of the result set. Specify the current location

First (): query points to a record in the result set.

Last (): Query points to the final record of the result set.

Next (): query points to the next record, and each time the function is executed, it points to the next record in the adjacent line.

Previous (): query points to the previous record, and each time the function is executed, it points to the previous record.

Record (): Gets the records pointed to now.

Value (int n): Gets the value of the property. where n represents the nth attribute of your query

int rowNum = query.at (); Gets the number of the record that the query points to in the result set

int fieldno = Query.record (). IndexOf ("name"); Returns the column number of "name"

int columnnum = Query.record (). Count (); Get the number of attributes (that is, columns) in each record

Transactional operations

Operation function: Transaction (), commit () Commit, rollback () rollback
Before manipulating a transaction, determine whether the database supports transactional operations. Hasfeature is a qsqldriver class function

[CPP]View Plaincopyprint?
    1. if (qsqldatabase::d atabase (). Driver ()->hasfeature (qsqldriver::transactions)) {...} //   

Insert a record, and then commit the transaction

[CPP]View Plaincopyprint?
      1. Qsqldatabase::d atabase (). transaction ();
      2. Qsqlquery query;
      3. Query.exec ("SELECT id from t_student WHERE class=1");
      4. if (Query.next ())
      5. {
      6. Query.exec ("INSERT into T_student (id,name,age) VALUES (3, ' Xiao Li ', 13)");
      7. }
      8. Qsqldatabase::d atabase (). commit ();

QT's SQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.