ODBC driver error QODBCResult::data: column out of range
Greetings, I'm sending this query to a third party program database using an ODBC driver
Code:
"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:
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
Code:
{
conectdbisam();
int h;
h=0;
if(db.open())
{
if(qry.exec("SELECT * FROM "+table))
{
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();
}
}
Does anyone see the problem with this code, or maybe a work around?
Thank in advance.
Re: ODBC driver error QODBCResult::data: column out of range
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.
Re: ODBC driver error QODBCResult::data: column out of range
you are so right, just fix the problem by changing the sql comand to
Code:
if(qry.exec("SELECT * FROM "+table+" WHERE "+columna+" = '"+pointer+"'"))
it works now, thank you!
Re: ODBC driver error QODBCResult::data: column out of range
But this solution is worse. Why download anything from the database if you need only one column?
Re: ODBC driver error QODBCResult::data: column out of range
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?
Re: ODBC driver error QODBCResult::data: column out of range
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.
Code:
if(qry.
exec( QString("SELECT * FROM %1 WHERE 1 = 0").
arg(table
) )) // condition stops row return but should allow column info {
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.
Re: ODBC driver error QODBCResult::data: column out of range
BTW the SQL standard guarantees a constant order of the columns in the query "SELECT * ...." ?