qt access 数据库

这一生的挚爱 提交于 2019-12-03 06:27:56

#include <QCoreApplication>

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QtDebug>
#include <QSqlError>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
//    QSqlDatabase db;
//    db = QSqlDatabase::addDatabase("QODBC","test");
//    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=E:/test.mdb");
//    if (db.open()) {
//        qDebug() << "xxxxxxxxxxxxxxxx";
//    }
//    else {
//        qDebug() << db.lastError();
//    }
//    return a.exec();\  QCoreApplication a(argc, argv);
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QODBC","test");
    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=testacc64");
    if (db.open()) {
        qDebug() << "xxxxxxxxxxxxxxxx";
    }
    else {
        qDebug() << db.lastError();
    }
    return a.exec();
}
int createSQLServerConnection()
{
    loadPlugin();

    QString strHost = "10.211.55.4";
    int port = 3306;
    QString strDbName = "SQLData";
    QString strUserName = "test";
    QString strUserPwd = "123321";
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    QString strconn = QString("Driver={sql server};SERVER=%1;PORT=%2;DATABASE=%3;UID=%4;PWD=%5;")
                          .arg(strHost)
                          .arg(port)
                          .arg(strDbName)
                          .arg(strUserName)
                          .arg(strUserPwd);
    db.setDatabaseName(strconn);
    if (!db.open())
    {
        qDebug() <<"error_SqlServer:" << db.lastError().text();
        return 1201;
    }

    return 0;
}

access 
"Driver={microsoft access driver(*.mdb)};dbq=*.mdb;uid=admin;pwd=pass;"

dBase
 "Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=***;"

oracle 
"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"

MSSQL server 
"Driver={sql server};server=servername;database=dbname;uid=sa;pwd=pass;"

MS text 
"Driver={microsoft text driver(*.txt; *.csv)};dbq=**;extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"

Visual Foxpro
 "Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"

MySQL 
"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"

SQLite 
"Driver={SQLite3 ODBC Driver};Database=D:\SQLite\*.db"

PostgreSQL 
"Driver={PostgreSQL ANSI};server=127.0.0.1;uid=admin;pwd=pass;database=databaseName"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!