PDA

View Full Version : ODBC driver error QODBCResult::data: column out of range



Andrewgaven
13th January 2013, 05:06
Greetings, I'm sending this query to a third party program database using an ODBC driver



"SELECT table FROM Column WHERE DESCRIPCION = 'SOMETHING'"


my funtion should return a Stringlist with the values of column "o" where the value of column "k" match "pointer"

Let's say, for example that my table is like

code name lastname description

if i set o="0" it return the codes fine, but any value diferent that "0" return the error "QODBCResult::data: column # out of range".

This is the same funtion that I use for a Postgres database but for some reason it doesn't work with the ODBC driver


QStringList conecta2::leedb(QString table, int k, int o, QString pointer)
{
conectdbisam();
QString line;
QString line2;
QStringList lista;
int h;
h=0;
if(db.open())
{
QSqlQuery qry;
if(qry.exec("SELECT * FROM "+table))
{
QSqlRecord rec = qry.record();
columna = rec.fieldName(k);
}
if(qry.exec("SELECT "+columna+" FROM "+table+" WHERE "+columna+" = '"+pointer+"'"))
{
while(qry.next())
{
line2 =qry.value(o).toString().simplified();
lista.append(line2);
h++;
if (h==500)
break;
}
}
else
{
qDebug() << "Error =" << db.lastError().text();
}
qry.clear();
}
else
{
qDebug() << "Error =" << db.lastError().text();
}
return QStringList(lista);
}

Does anyone see the problem with this code, or maybe a work around?

Thank in advance.

Lesiok
13th January 2013, 12:18
In line 17 query result always have only one column. So that a value other than 0 for a variable o does not make sense.

Andrewgaven
13th January 2013, 16:39
you are so right, just fix the problem by changing the sql comand to

if(qry.exec("SELECT * FROM "+table+" WHERE "+columna+" = '"+pointer+"'"))

it works now, thank you!

Lesiok
14th January 2013, 08:51
But this solution is worse. Why download anything from the database if you need only one column?

Andrewgaven
15th January 2013, 23:14
I just need the column that match a certain criteria, for example, Let's say my db is like this:

Code name lastname description
001 Andrew Gaven Ingenier
002 Bill Doe CEO
003 Jonh OConnor CEO
.
.

I might just need the names of the CEO so my values should be pointer="CEO", k=3 (witch is my description column) and o=1 (the names column), ¿do you have a better idea?

ChrisW67
16th January 2013, 04:15
This really is quite an odd mechanism you are implementing. Typically you would know the names of the columns of interest without having to look them up using column indexes.

Look up the column name of both columns and use those in your query.


QSqlQuery qry;
if(qry.exec( QString("SELECT * FROM %1 WHERE 1 = 0").arg(table) )) // condition stops row return but should allow column info
{
QSqlRecord rec = qry.record();
columnk = rec.fieldName(k);
columno = rec.fieldName(o);
}
if(qry.exec( QString("SELECT %1 FROM %2 WHERE %3 = '%4' ").arg(columno).arg(table).arg(columnk).arg(pointer ) ))
{
while(qry.next())
{
line2 =qry.value(0).toString().simplified(); // That is a zero
lista.append(line2);
h++;
if (h==500)
break;
}
}

All assumes that o and k indexes are in range and that the table name and "pointer" value have been sanitised to avoid SQL injection issues.

Lesiok
16th January 2013, 09:30
BTW the SQL standard guarantees a constant order of the columns in the query "SELECT * ...." ?