PDA

View Full Version : Showing GUID keys in Standart View Classes



Rus
17th April 2008, 17:10
Hello. Sorry for my pure English (i'm from Russia)

I want to use standart (QSqlQueryModel+QtableView) for working with MSSQL database.
When i trying to get MSSQL "uniqueidentifier" fileds from database, in View it shows as no-readable symbols.
I know that GUID supports in QT as UUid Class. But it not encluded in QVariant::type enum.

I can solve this problem, with subclassing a QSqlQueryModel and reimlplementing a data() - method. But I think that not only me face with this problem. Thank You.

wysota
17th April 2008, 22:31
Could you show us your code used for querying the database?

Rus
18th April 2008, 07:24
This is standart code; In database cdoc field is UNIQUEIDENTIFIER.



#include <QApplication>
#include <QSqlQuery>
#include <QVariant>
#include "MainWidget.h"
#include "iostream"

QSqlDatabase openconnnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName( "DRIVER={SQL Server};Server=DELTA;Database=Test;Trusted_Connect ion=yes;");
db.open();
return db;
};
///////////////////////////////// ** ////////////////////////////////////
int main(int argc, char *argv[]){
QApplication app(argc, argv);
QSqlDatabase db = openconnnection();
QSqlQuery sql("SELECT TOP 1 CDOC, DOCDATE FROM DOCS");
sql.first();

const char* strCDOC = sql.value(0).toString().toLocal8Bit();
std::cout << strCDOC << std::endl;

const char* strDATE = sql.value(1).toString().toLocal8Bit();
std::cout << strDATE << std::endl;
return app.exec();
}


Yesterday, i trying to get field as ByteArray. If I divide standart byte to 2 part 4-bit array in each. Each of new 4bit symbol - are the real symbol of UNIQUEIDENTIFIER.

wysota
18th April 2008, 08:58
Ouch... your code is bad... it's even Bad. First of all you shoould check if the query returned any result at all using QSqlQuery::next() or QSqlQuery::first() (check the return value). Second of all, you musn't store the value returned by toLocal8Bit() (which by the way returns a byte array not a const char*), because the byte array is a temporary object. You're just asking for your application to crash or work incorrectly.

Rus
18th April 2008, 09:21
No, it is not use-able code. It may be very bad. It code only shows that I can't show first field in query. I trying write smallest code for you.

mazurekwrc
18th April 2008, 09:30
check first what return db.open() and sql.first()

Rus
18th April 2008, 09:48
I sure that you don't understand my problem..
If i use qsqlquerymodel - it will be more simple to understand



#include <QApplication>
#include <QSqlQueryModel>
#include <QTableView>
#include "MainWidget.h"
#include "iostream"

QSqlDatabase openconnnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName( "DRIVER={SQL Server};Server=DELTA;Database=Test;Trusted_Connect ion=yes;");
db.open();
return db;
};

///////////////////////////////// ** ////////////////////////////////////
int main(int argc, char *argv[]){
QApplication app(argc, argv);
QSqlDatabase db = openconnnection();
QSqlQueryModel sqlmodel;
sqlmodel.setQuery("SELECT TOP 10 CDOC, DOCDATE FROM DOCS");
QTableView tbl;
tbl.setModel(&sqlmodel);
tbl.show();
return app.exec();
}


Problem step-by-step description:
In my database DOCS table contains more than 3 millions records. Than I started application, I see 10 records in grid, row's first columns is no-readable, second presents a standart date field.

2184

How to present row's first column, like that: {4FEE5BC6-8904-45EC-B830-0000433322D2}

wysota
18th April 2008, 16:35
The data is probably returned as a byte array which you need to decode into string manually.