Help with getting column names with esql
Posted in 2001
Topics: SQL Development & Query Writing, Error Codes & Troubleshooting, Connectivity: ESQL/C, 4GL & Embedded SQL, Platform-Specific Issues
I have written the following c++ function:
/*
@ ---------------------------------------------
@ int importTable::getTableStructureFromInformix();
@ Uses the INFO query to get the column names
@ of the table, and puts them into the tables
@ deque for the object.
@ Returns the number of columns in the table.
@ ---------------------------------------------
*/
int importTable::getTableStructureFromInformix(){
EXEC SQL BEGIN DECLARE SECTION;
char tableName[6] = {"badge"};
char sql[4096] = {'\\0'};
char colName[50] = {'\\0'};
char colType[50] = {'\\0'};
char colNull[50] = {'\\0'};
EXEC SQL END DECLARE SECTION;
// tableName = "badge"; May not need this
sprintf(sql, "INFO COLUMNS FOR %s", tableName);
cout << sql << endl;
EXEC SQL PREPARE SQLQueryStmt FROM :sql;
if(SQLCODE && KEEP_LOG)
cerr << "Error preparing to get Column info, error # " << SQLCODE <<
endl;
EXEC SQL DECLARE cursor CURSOR FOR SQLQueryStmt;
if(SQLCODE && KEEP_LOG)
cerr << "Error declaring cursor, error # " << SQLCODE << endl;
EXEC SQL OPEN cursor;
if(SQLCODE && KEEP_LOG)
cerr << "Error opening cursor, error # " << SQLCODE << endl;
while(SQLCODE == 0){
EXEC SQL FETCH cursor INTO :colName, :colType, :colNull;
cerr << colName << endl;
}
EXEC SQL CLOSE cursor;
EXEC SQL FREE cursor;
EXEC SQL FREE SQLProxUpdStmt;
return 0;
}
and for some reason, after connecting to the database, I get error 201
from this:
-201 A syntax error has occurred.
This general error message indicates mistakes in the form of an SQL
statement. Look for missing or extra punctuation (such as missing or
extra commas, omission of parentheses around a subquery, and so on),
keywords misspelled (such as VALEUS for VALUES), keywords misused (such
as SET in an INSERT statement or INTO in a subquery), keywords out of
sequence (such as a condition of "value IS NOT" instead of "NOT value
IS"), or a reserved word used as an identifier.
Database servers that provide full NIST compliance do not reserve any
words; queries that work with these database servers might fail and
return error -201 when they are used with earlier versions of Informix
database servers.
The cause of this error might be an attempt to use round-robin syntax
with
CREATE INDEX or ALTER FRAGMENT INIT on an index. You cannot useround-robin
indexes.
[mike@sam cimport]$
If I change this to a select statement, such as "SELECT * FROM badge" it
works fine. Why does the "INFO COLUMNS FOR badge" not work?
I am using csdk -v: 2.40.UC1
on informix server v.9 on AIX v4.
The client program is on a linux machine.
As I said, I can connect and run select statements fine, but I can't use
INFO. If this is not the way to do it, how do I get the column
information from a table?
Thanks
Mike Baranski
*********************************
Mike W. Baranski
mike@secmgmt.com
info@secmgmt.com
Web : http://www.secmgmt.com
Phone: 919-788-9200
Fax : 919-510-0037
P.O. box 30099
Raleigh, N.C. USA 27622
*********************************
"Mike W. Baranski" wrote: > I have written the following c++ function: > > /* > @ --------------------------------------------- > @ int importTable::getTableStructureFromInformix(); > @ Uses the INFO query to get the column names > @ of the table, and puts them into the tables > @ deque for the object. > @ Returns the number of columns in the table. > @ --------------------------------------------- > */ > int importTable::getTableStructureFromInformix(){ > [...snip...] > sprintf(sql, "INFO COLUMNS FOR %s", tableName); > cout << sql << endl; > EXEC SQL PREPARE SQLQueryStmt FROM :sql; Look in the manual for INFO and you will see that it is marked as a special feature of DB-Access. It is converted by DB-Access into a very different SELECT statement, and the results are then presented by DB-Access. You will need to PREPARE "SELECT * FROM Table" and then use DESCRIBE to get the actual information... See, for example, the code in SQLCMD available from the IIUG archives -- or Art Kagel's various utilities. > and for some reason, after connecting to the database, I get error 201 > from this: > -201 A syntax error has occurred. It isn't an SQL statement; it is a DB-Access built-in. > [...big snip...] -- Yours, Jonathan Leffler (Jonathan.Leffler@Informix.com) #include <disclaimer.h> Guardian of DBD::Informix v1.00.PC1 -- http://www.perl.com/CPAN "I don't suffer from insanity; I enjoy every minute of it!"