前段時間在將原先Linux環境下的程序移到unixware下時發現了一些問題,主要是涉及操作數據庫時直接將表中的一條記錄select到與之對應的結構中時預編譯器報錯。下面是用於測試的程序和現象。
測試程序:
aaa.cp
[code:1:8e4514ce85]
#include
#include
EXEC SQL include sqlca;
long SQLCODE;
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL WHENEVER SQLWARNING CONTINUE;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
#ifndef SQLMESSAGE
#define SQLMESSAGE sqlca.sqlerrm.sqlerrmc
#endif
EXEC SQL BEGIN DECLARE SECTION;
struct aaa {
int i;
char s[5];
double d;
};
EXEC SQL END DECLARE SECTION;
main()
{
EXEC SQL BEGIN DECLARE SECTION;
struct aaa aaa;
EXEC SQL END DECLARE SECTION;
if( ConnectDB( "sa", "", NULL ) < 0 )
{
printf( "connect DB error!\n" );
exit( -1 );
}
memset( &aaa, 0, sizeof(aaa) );
EXEC SQL select * into :aaa from aaa;
if( SQLCODE != 0 )
{
printf( "select error! SQLCODE[%d] %s\n", SQLCODE, SQLMESSAGE );
CloseDB();
exit( -1 );
}
printf( "i=[%d]\n", aaa.i );
printf( "s=[%s]\n", aaa.s );
printf( "d=[%.2lf]\n", aaa.d );
CloseDB();
}
[/code:1:8e4514ce85]
其中ConnectDB()、CloseDB()是外部自定義的函數。
對以上代碼編譯、執行,無任何問題。
下面是使用多個結構的情況,對結構定義部分進行了修改,增加了一個新的結構定義。其它代碼未動
[code:1:8e4514ce85]
EXEC SQL BEGIN DECLARE SECTION;
struct aaa {
int i;
char s[5];
double d;
};
struct bbb{
int a;
char b[5];
char c[30];
double d;
};
EXEC SQL END DECLARE SECTION;
[/code:1:8e4514ce85]
再次編譯,問題出現了!
cc -c -g -I/home/Sybase/include aaa.c
UX:acomp: WARNING: "aaa.cp", line 68: improper member use: a
UX:acomp: WARNING: "aaa.cp", line 68: improper member use: b
UX:acomp: WARNING: "aaa.cp", line 68: improper member use: c
將兩個結構定義更換位置:
[code:1:8e4514ce85]
EXEC SQL BEGIN DECLARE SECTION;