----------------------------
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?
- Qsqldatabase db = Qsqldatabase::adddatabase ("Qoci");
- Db.sethostname ("localhost"); //Database host name
- Db.setdatabasename ("Scott"); //Database name
- Db.setusername ("Stott"); //database user name
- Db.setpassword ("Tiger"); //Database password
- Db.open (); //Open database connection
- Db.close (); //Release database connection
Create a database file
[CPP]View Plaincopyprint?
- Qsqldatabase db = Qsqldatabase::adddatabase ("Qsqlite");
- Db.setdatabasename ("database.db");
- if (!db.open ())
- {
- Qdebug ("database cannot be opened");
- }
- return false;
[CPP]View Plaincopyprint?
- 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" >
- Qsqlquery query;
- Query.exec ("CREATE TABLE student (ID INTEGER PRIMARY KEY autoincrement,
- Name nvarchar (), age int) "); //id Automatic Increase
- Query.exec ("INSERT into student values (1, ' xiaoming ', 14)");
- 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?
- Qsqlquery query;
- Query.prepare ("INSERT into T_student (name, age) VALUES (: Name,: Age)"); //Prepare to execute SQL query
- Query.bindvalue (": Name", "Xiao Wang"); //Bind the value to be inserted
- Query.bindvalue (": Age", 11);
- Query.exec ();
ODBC syntax
[CPP]View Plaincopyprint? < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- Qsqlquery query;
- Query.prepare ("INSERT into T_student (name,age) VALUES (?,?)"); //Prepare to execute SQL query
- Query.addbindvalue ("Xiao Wang"); //Bind the value to be inserted
- Query.bindvalue (11);
- Query.exec ();
Third, BULK INSERT into the database
[CPP]View Plaincopyprint?
- Qsqlquery query;
- Query.prepare ("INSERT into student values (?,?)");
- Qvariantlist names;
- 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
- Query.addbindvalue (names);
- Qvariantlist ages;
- Ages << << << << 11;
- Query.addbindvalue (ages);
- if (!q.execbatch ()) //batch, output error if error occurs
- Qdebug () << q.lasterror ();
Querying database operations
[CPP]View Plaincopyprint?
- Qsqlquery query;
- Query.exec ("select * from T_student"); //The result of the query may be more than one record, so we call it a result set
- while (Query.next ())
- {
- QString name = Query.value (0). toString (); the results of the 1th field (counting from 0) are recorded in article I
- int age = Query.value (0). ToInt (); //Take the result of section I record 2nd field
- // ... Working with name,age variable data
- }
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?
- if (qsqldatabase::d atabase (). Driver ()->hasfeature (qsqldriver::transactions)) {...} //
Insert a record, and then commit the transaction
[CPP]View Plaincopyprint?
- Qsqldatabase::d atabase (). transaction ();
- Qsqlquery query;
- Query.exec ("SELECT id from t_student WHERE class=1");
- if (Query.next ())
- {
- Query.exec ("INSERT into T_student (id,name,age) VALUES (3, ' Xiao Li ', 13)");
- }
- Qsqldatabase::d atabase (). commit ();
QT's SQL database