借助Qt实现向MSSQL中快速插入数据(qt向mssql插入数据)

Having to perform data insertion in MSSQL in a high frequency can be a daunting job. In order to quickly and accurately insert data, Qt provides an efficient way for that.

Qt has long been a leading platform for cross-platform graphical user interface (GUI) application development. Among its wide range of libraries, Qt also provides tools that orientates to relational database access. Qt library SQL provides feature class QSqlQuery which plays an important role in data insertion in MSSQL. Through the a QSqlQuery instance, developers do not have to bother writting any SQL scripts, but rather preparing a statement and setting a series of parameters. After that, commit the changes you have made and the data will be inserted into the database.

Let us take a look at a demostration.

To begin with, we will create a database, named as “test” in a MSSQL database,

CREATE TABLE “test” (

“name” VARCHAR(25),

“age” INT

)

Then, should look up the parameters of the database, such as database name, user name and password.

Next, with Qt, the database can be connected via QSqlDatabase::addDatabase()

QSqlDatabase db = QSqlDatabase::addDatabase(“QODBC3”);

db.setDatabaseName(“DRIVER=MSSQL;SERVER=127.0.0.1;DATABASE=test;User=;Password=;”);

Now, choose a database, and start to create a database query.

QSqlQuery query;

Now, prepare the statement and set private parameters. Notice the “?” mark next to parameters, it provides us with a way to set parameters line by line, instead of writting the statement again and again.

query.prepare(“INSERT INTO test (name,age) values (?,?)”);

query.addBindValue(“Bob”);

query.addBindValue(23);

Finally, execute the query and commit the changes to finish the data insertion.

query.exec();

db.commit();

To conclude, Qt provides us a way to quickly and accurately insert data into MSSQL. This is a convenient process for performing data insertion.


数据运维技术 » 借助Qt实现向MSSQL中快速插入数据(qt向mssql插入数据)