剛學delphi不久,小白一枚。
Delphi7中query添加了所有欄位,添加sql陳述句時只搜索顯示某些欄位,為嘛其它不需要的欄位老報錯,提示找不到?
是不是添加欄位都必須顯示的呢?
然后洗掉到只剩一個欄位,能顯示了,但搜索的其他欄位又看不到了!如何解決?
uj5u.com熱心網友回復:
query 的查詢陳述句, 可以將查詢的所有欄位自動添加好。你不需要手工添加。
將query 內添加的欄位,全部洗掉,就OK了。
uj5u.com熱心網友回復:
我想實作某布爾欄位的ongettext事件,就必須添加某一個布爾欄位,如果只添加這個布爾欄位,搜索的時候就只顯示這個布爾欄位,其他欄位就顯示不了。如果洗掉,這個布爾欄位就只顯示false或者ture了。所以很糾結。我想了解的是除了ongettext事件外,如何才能實作布爾型別欄位不顯示false或true,而顯示對或錯的自定義文字呢?
或者query添加了欄位,如何動態添加其他需要顯示的欄位,因為后面搜索顯示的欄位有多有少。我之前的方法是添加幾個要查的欄位,然后不同搜索的時候洗掉多余欄位,用destroy的方法或free;但能查了,不報錯,但繼續點擊搜索按鍵就報錯了。不能連續查。
uj5u.com熱心網友回復:
query中的欄位要和SQL陳述句結果集的欄位要一致,比如QL陳述句是:select 編號,名稱 from 表
那么query的欄位中就要有編號,名稱這些欄位
uj5u.com熱心網友回復:
procedure TForm1.Button1Click(Sender: TObject);
begin
query1.DatabaseName:=ExtractFilePath(opendialog1.FileName);
if memo1.Text='' then
begin
showmessage('請選擇查詢檔案!') ;
end
else if listbox1.ItemIndex=-1 then
begin
showmessage('請選擇查詢類別!') ;
end
else if (edit1.Text='') then
begin
showmessage('請輸入數值!') ;
end
else if listbox1.Items[listbox1.ItemIndex]='區段限速' then
begin
query1.Fields[5].FieldName:='起點公里標';
query1.Fields[6].FieldName:='終點公里標';
query1.fields[7].Destroy; //此行造成了報錯,錯誤資訊:query1:cannot perform this operation on an open dataset.
query1.close;
query1.sql.clear;
query1.sql.add('select JLH 交路號,SXX 行別,XXLX 類別代碼,CZM 類別,CZH 車站號,BH 起點公里標,GLB 終點公里標');
query1.sql.add('from '+ExtractFileName(opendialog1.FileName)+'');
query1.sql.add('where xxlx="31" OR (BH=:BH1 and xxlx="83") or (GLB=:BH1 and xxlx="83")'); //查詢車站資訊或者數值且是區段限速型別
query1.params[0].AsInteger:=strtoint(edit1.text);
query1.prepare;
query1.open;
query1.Filtered:=false;
DBgrid1.Columns[0].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[0].Alignment:=taCenter;//內容居中
DBgrid1.Columns[1].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[1].Alignment:=taCenter;//內容居中
DBgrid1.Columns[2].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[2].Alignment:=taCenter;//內容居中
DBgrid1.Columns[3].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[3].Alignment:=taCenter;//內容居中
DBgrid1.Columns[4].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[4].Alignment:=taCenter;//內容居中
end
end;
uj5u.com熱心網友回復:
procedure TForm1.Button1Click(Sender: TObject);
begin
query1.DatabaseName:=ExtractFilePath(opendialog1.FileName);
if memo1.Text='' then
begin
showmessage('請選擇查詢檔案!') ;
end
else if listbox1.ItemIndex=-1 then
begin
showmessage('請選擇查詢類別!') ;
end
else if (edit1.Text='') then
begin
showmessage('請輸入數值!') ;
end
else if listbox1.Items[listbox1.ItemIndex]='區段限速' then
begin
query1.Fields[5].FieldName:='起點公里標';
query1.Fields[6].FieldName:='終點公里標';
query1.fields[7].Destroy; //此行造成了報錯,錯誤資訊:query1:cannot perform this operation on an open dataset.
query1.close;
query1.sql.clear;
query1.sql.add('select JLH 交路號,SXX 行別,XXLX 類別代碼,CZM 類別,CZH 車站號,BH 起點公里標,GLB 終點公里標');
query1.sql.add('from '+ExtractFileName(opendialog1.FileName)+'');
query1.sql.add('where xxlx="31" OR (BH=:BH1 and xxlx="83") or (GLB=:BH1 and xxlx="83")'); //查詢車站資訊或者數值且是區段限速型別
query1.params[0].AsInteger:=strtoint(edit1.text);
query1.prepare;
query1.open;
query1.Filtered:=false;
DBgrid1.Columns[0].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[0].Alignment:=taCenter;//內容居中
DBgrid1.Columns[1].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[1].Alignment:=taCenter;//內容居中
DBgrid1.Columns[2].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[2].Alignment:=taCenter;//內容居中
DBgrid1.Columns[3].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[3].Alignment:=taCenter;//內容居中
DBgrid1.Columns[4].Title.Alignment:=taCenter;//標題居中
dbgrid1.Columns[4].Alignment:=taCenter;//內容居中
end
end;
目前使用的代碼,欄位里面添加所有需要查的欄位,然后查不同資料的時候洗掉多余欄位,首次點擊沒問題,能查到。但再次點擊就報錯。query1.fields[7].Destroy; //此行造成了報錯,錯誤資訊:query1:cannot perform this operation on an open dataset.洗掉多余欄位,洗掉這條陳述句就沒問題,請問如何解決呢?
uj5u.com熱心網友回復:
query沒有必要加欄位上去的,都是自己加載的uj5u.com熱心網友回復:
最簡單的方法, 在sql 查詢中就處理了。
比如將查詢陳述句寫成:
select *, case [布爾欄位] when 1 then '對' else '錯' end as [布爾欄位1] from tbl
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80259.html
標籤:數據庫相關
上一篇:關于mapx
