PDA

View Full Version : Qsql and connection



smemamian
4th April 2013, 19:45
hi
I do not understand line 7 and 11,Please explain :(

#include <QCoreApplication>
#include <QtSql>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString servername = "LOCALHOST\\SQLEXPRESS" ;
QString dbname = "test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Native Client);SERVER=%1;DATABASE=%2;Trusted_Connection=Y es;").arg(servername).arg(dbname);
db.setDatabaseName(dsn);

return a.exec();
}

tnx

alizadeh91
5th April 2013, 07:19
Simply you are filling properties of db. servername is address or name of your server. because your computer is your own server so you have to use LOCALHOST or 127.0.0.1 for server name.

ChrisW67
5th April 2013, 10:10
The string you are constructing at Line 11 is a Data Source Name (DSN) which encapsulates a bunch of settings that you could, alternatively, set up in the Windows ODBC Manager. You should have a driver named "SQL Native Client" in the ODBC Manager.

Also, the line should read:


QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Y es;").arg(servername).arg(dbname);

Note a ')' became a '}'

smemamian
5th April 2013, 10:18
thanks a lot .

Why do I get the error message when I compile ?

QsqlDatabase: QODBC driver not loaded
QSqlDataase : available drivers:QSQLITE

my code :


#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString servername = "LOCALHOST\\SQLEXPRESS" ;
QString dbname = "test";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Y es;").arg(servername).arg(dbname);
db.setDatabaseName(dsn);

if(db.open())
{
qDebug() << "Opene.." ;
}

else
qDebug() << "Not Opene.." << db.lastError();

return a.exec();
}

Microsoft SQL Server 2008
Windows 7

alizadeh91
5th April 2013, 11:12
It seems that you have not built QODBC driver. You have to build it first (find its source in Src\qtbase\src\plugins\sqldrivers)

Added after 6 minutes:

See this :

http://qt-project.org/doc/qt-4.8/sql-driver.html#how-to-build-the-odbc-plugin-on-windows

for how to build odbc plugin on windows

smemamian
5th April 2013, 12:09
i open cmd and i write :


cd C:\Qt\Qt5.0.0\5.0.0\Src\qtbase\src\plugins\sqldriv ers\odbc
qmake odbc.pro
I get the error message :

'qmake' is not recognized as a internal or external command ,
operable program of batch file

alizadeh91
5th April 2013, 12:18
Did u add qmake to your environment variables???

smemamian
5th April 2013, 12:26
no, how ? :(

alizadeh91
5th April 2013, 12:35
1.RightClick on mycomputer
2. click properties
3. go to "change settings" in the link at bottom right corner of window
4. go to "advance" tab
5. Press environment variables
6. from system variables list, double click on "path"
7. First of all put a ";" at end of "variable value" then paste address of your qmake (e.g C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin) out there after the simicolon (";")
8. reopen the cmd then type "qmake -v" to see if you made it right or not!

smemamian
5th April 2013, 12:45
this is correct ?


variable value : C:\TCWIN45\BIN;C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin\qm ake

alizadeh91
5th April 2013, 12:51
just don't remove any character in "variable value" field. just put a simicolon at end and paste qmake address (C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin don't put qmake at end of address)

smemamian
5th April 2013, 13:05
Tnx , Hooray\:d/


variable value : C:\TCWIN45\BIN;C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin;

Added after 12 minutes:


nmake

'nmake' is not recognized as a internal or external command ,
operable program of batch file

i hasn't nmake in C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin address for add to environment variables :((

BalaQT
5th April 2013, 14:46
hi,

i hasn't nmake in C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin address for add to environment variables

just find nmake using a search explorer and set the path

hope it helps,
Bala

alizadeh91
5th April 2013, 15:43
Instead of cmd you can use "visual studio command prompt" (just search it in start menu and open it). It recognize nmake by itself ;)

smemamian
5th April 2013, 18:05
hi,


just find nmake using a search explorer and set the path

hope it helps,
Bala

No items match your search

Added after 4 minutes:


Instead of cmd you can use "visual studio command prompt" (just search it in start menu and open it). It recognize nmake by itself ;)

Very thanks .

alizadeh91
5th April 2013, 18:05
use visual studio command prompt