Hi all,
I tried to run an app with QPSQL, but I found that it doesn't support UNICODE:
qt1.png
and from psql shell:
postgresql2.png
I inserted first row with psql shell therefore you can see unicode is supported inside postgresql.
As postgresql docs says (link):
"Programs using libpq directly need to be aware of this, but not much else does."
So I'm trying to run a test app with QODBC, even if qt docs says that it's better to use specific driver (QPSQL) link:
Note: You should use native drivers in preference to the ODBC driver where they are available. ODBC support can be used as a fallback for compliant databases if no native drivers are available.
I installed driver manager too (following this thread instructions):
posgresql1.png
then compiled and installed qt odbc wrapper:
qt0.jpg
this is my code:
//main.cpp:
#include <QCoreApplication>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDriver>
#include <QDebug>
class Database
{
public:
explicit Database();
~Database();
bool connection();
private:
};
Database::~Database()
{
db.close();
}
Database::Database() :
//db(QSqlDatabase::addDatabase("QODBC3"))//tried even with this
{
db.setDatabaseName("Driver={PostgreSQL};Server=localhost;Port=5432;Database=prova;Uid=postgres;Pwd=*******;");
//db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3");//tried even with this
}
bool Database::connection()
{
if (!db.open()) {
qDebug
() <<
QString("Unable to establish a database connection.\n" "This example needs %1 support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit.").arg(driverName);
return false;
}
return true;
}
bool insertTest()
{
q.prepare("INSERT INTO p(d) VALUES(:d)");
q.
bindValue(":d",
QString("Questa è un altra prova"));
return q.exec();
}
int main(int argc, char **argv)
{
const QScopedPointer<Database> database(new Database());
database->connection();
if(insertTest())
{
qDebug() << "query executed successfully";
}else{
qDebug() << "insert query error";
}
q.exec("SELECT * FROM p");
while(q.next())
{
qDebug() << "id=" << r.value("id").toInt();
qDebug() << "d=" << r.value("d").toString() << '\n';
}
return app.exec();
}
//main.cpp:
#include <QCoreApplication>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDriver>
#include <QDebug>
class Database
{
public:
explicit Database();
~Database();
bool connection();
private:
QString driverName;
QSqlDatabase db;
};
Database::~Database()
{
db.close();
}
Database::Database() :
db(QSqlDatabase::addDatabase("QODBC"))
//db(QSqlDatabase::addDatabase("QODBC3"))//tried even with this
{
driverName = QString("QODBC");
db.setDatabaseName("Driver={PostgreSQL};Server=localhost;Port=5432;Database=prova;Uid=postgres;Pwd=*******;");
//db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3");//tried even with this
}
bool Database::connection()
{
if (!db.open()) {
qDebug() << QString("Unable to establish a database connection.\n"
"This example needs %1 support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit.").arg(driverName);
return false;
}
return true;
}
bool insertTest()
{
QSqlQuery q;
q.prepare("INSERT INTO p(d) VALUES(:d)");
q.bindValue(":d",QString("Questa è un altra prova"));
return q.exec();
}
int main(int argc, char **argv)
{
QCoreApplication app(argc,argv);
const QScopedPointer<Database> database(new Database());
database->connection();
if(insertTest())
{
qDebug() << "query executed successfully";
}else{
qDebug() << "insert query error";
}
QSqlQuery q;
q.exec("SELECT * FROM p");
while(q.next())
{
QSqlRecord r = q.record();
qDebug() << "id=" << r.value("id").toInt();
qDebug() << "d=" << r.value("d").toString() << '\n';
}
return app.exec();
}
To copy to clipboard, switch view to plain text mode
and this is my .pro file:
QT += core gui sql widgets
TARGET = ScopedPointer
CONFIG += console
TEMPLATE = app
sql-plugins += odbc
SOURCES += main3.cpp
OBJECTS_DIR = build/o
MOC_DIR = build/moc
UI_DIR = build/ui
RCC_DIR = build/rcc
DESTDIR = bin
QT += core gui sql widgets
TARGET = ScopedPointer
CONFIG += console
TEMPLATE = app
sql-plugins += odbc
SOURCES += main3.cpp
OBJECTS_DIR = build/o
MOC_DIR = build/moc
UI_DIR = build/ui
RCC_DIR = build/rcc
DESTDIR = bin
To copy to clipboard, switch view to plain text mode
I added sql-plugins directive following this thread
Anyway i get this error message:
"QODBCResult::exec: No statement handle available" Error: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
"QODBCResult::exec: No statement handle available" Error: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
To copy to clipboard, switch view to plain text mode
as you can see here:
error.png
I'm using Qt 5.2.0. If you need any other information please ask.
Any help would be appreciated, thanks!
Alberto
Bookmarks