qt database connection

#include <QSqlDatabase>
#include <a QMessageBox>

#include <QSQLITE>

    // Add MySql database
    QSqlDatabase DB = QSqlDatabase :: addDatabase ( "QMYSQL");

    // database connection
    db.setHostName ( "192.168.1.20"); // database server IP
    db.setUserName ( "root"); // database user name
    db.setPassword ( "000000"); // password
    db.setDatabaseName ( "test01"); // which database

    // open the database
    if (! db.open ()) // open the database failed
    {
        a QMessageBox :: warning text ()) (the this,, db.lastError ( "database error!");.
        return;
    }
    
// use sqslite

    QSqlDatabase DB = QSqlDatabase :: addDatabase ( "QSQLITE");
    // connect to the database
    db.setDatabaseName ( "test01"); // which database
    // open the database
    if (db.open ()!) // open the database failed
    {
        QMessageBox :: warning (the this, "wrong", db.lastError () text ().);
        return;
    }
    
    / *
    // SQL statement
    
    * in the database Create a table named user
    * Create table user (ID int primary key Not null AUTO_INCREMENT, name VARCHAR (20 is), password VARCHAR (255));
    * // a pRIMARY kEY: constraint uniquely identifies each record in the database table, the primary key It must contain unique values. Primary key column can not contain a NULL value. Each table has one and only one primary key.
    * Insert a data user in
    * INSERT INTO user (ID, name, password) values (VALUE1, string1, string2);  
    *
    * Show user all the data
    * SELECT * from user
    * increases query
    * select "string" from user name = WHERE "string2" like "% VALUE1%";
    *
    * update a data
    *update user set name = 'mike' where id = 2;
    *
    *删除表
    *drop table user;
    *重命名表
    *RENAME TABLE old_table_name TO new_table_name;
    *
    *
    */

Guess you like

Origin www.cnblogs.com/mc-r/p/11815572.html