There are many ways to connect to the database using QT. Here we describe the most common and practical method, because this method is very convenient for development programs on Windows, it is also required to remotely connect to the database.
There are three methods to connect to the database:
Refer to the following connection: connect to the ODBC database using QT
Note that the database name used to connect to the database is different from SQLite. Instead of writing the database name directly, it is the DSN name. If you have already set a DSN, you can directly enter the DSN name.
If no, you can use a DSN connection string to directly connect to the ODBC database.
For example:
/* In the following example, connect to the database named databasename of SQL Server 10.0.0.2. Specify the connection name in the program as db1 */qsqldatabase DB = qsqldatabase: adddatabase ("qodbc", "db1"); dB. sethostname ("10.0.0.2"); // If the DSN already contains a server, you can omit this sentence: qstring DSN = qstring ("driver = {SQL Server}; server = 10.0.0.2; database = databasename; uid = username; Pwd = password "); dB. setdatabasename (DSN); If (! DB. open () {qmessagebox: Critical (0, TR ("database error"), DB. lasterror (). Text ());}
If the DSN name is set incorrectly. "[Microsoft] [ODBC driver manager] data source name not found and no default driver specified qodbc3: unable to connect "" [Microsoft] [ODBC driver manager] No data source name is found and default driver qodbc3: Unable to connect is not specified "error.
Modify the parameters configured in the DSN to conform to the specifications.
The following is the connection string between ODBC and oledb:
ODBC connection
Database Type |
DSN configuration format |
Access |
"Driver = {Microsoft Access Driver (*. mdb)}; DBQ = *. mdb; uid = username; Pwd = password ;" |
DBASE |
"Driver = {Microsoft DBASE Driver (*. DBF)}; driverid = 277; DBQ = *. DBF; uid = username; Pwd = password ;" |
Oracle |
"Driver = {Microsoft ODBC for Oracle}; server = oraclesever. World; uid = username; Pwd = password ;" |
MSSQL Server |
"Driver = {SQL Server}; server = servername; database = dbname; uid = username; Pwd = password ;" |
Text MS |
"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 = password; option = 16386 ;" |
SQLite |
"Driver = {sqlite3 ODBC driver}; database = D: \ SQLite \ *. DB ;" |
PostgreSQL |
"Driver = {PostgreSQL ANSI}; server = 127.0.0.1; uid = username; Pwd = password; database = databasename ;" |
Oledb connection
Database Type |
DSN configuration format |
Access |
"Provider = Microsoft. Jet. oledb.4.0; Data Source = your_database_path; user id = username; Password = password ;" |
Oracle |
"Provider = oraoledb. Oracle; Data Source = dbname; user id = username; Password = password ;" |
MSSQL Server |
"Provider = sqloledb; Data Source = machinename; initial catalog = dbname; userid = sa; Password = pass ;" |
Text MS |
"Provider = microsof. Jet. oledb.4.0; Data Source = your_path; extended properties 'text; FMt = delimited'" (not verified) |
Refer:
Http://www.gamedev.net/community/forums/topic.asp? Topic_id = 523765
Http://www.qtcn.org/bbs/read.php? Tid = 12414
Http://www.qtcn.org/bbs/read.php? Tid = 7197 & keyword = SQL % 20 server