不知道大哥大姐们是否愿意帮我看看这个问题?(0分)

  • 主题发起人 stonehuang
  • 开始时间
S

stonehuang

Unregistered / Unconfirmed
GUEST, unregistred user!
请看看下面错在哪里呢???
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Grids, DBGrids, DB, DBTables;

type
TForm1 = class(TForm)
Query1: TQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
query1.Close;
try
query1.DatabaseName:=combobox1.Text;
query1.SQL.Clear;
query1.SQL.Add('select*form'+combobox2.Text);
query1.Open;
except
showmessage('not open file');
end;
end;


procedure TForm1.FormActivate(Sender: TObject);
begin
session.GetAliasNames(combobox1.Items);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
session.GetTableNames(combobox1.Text,'*.*',false,false,combobox2.Items);
combobox2.ItemIndex:=0
end;

end.
运行程序时,当打开某个数据库时,便出现如下错误:
project project1.exe raised exception class EDBEnginerError with message'invalid
use of keyword.
Token:formanimals(我打开的数据库名称).
Line Number:1' Process stopped.use step or run continue.
这是怎么的一回事?谢谢!!
 
应该是sql语句有错。运行设断点,ctrl+f7看看sql语句到底是什么
 
是sql语句有问题吧。打开SQL MONITOR,监视SQL 语句的执行情况,
 
运行ctrl+f7之后有如此语句:Expression expected but 'END' found!
运行sql monitor没什么反映!
 
query1.SQL.Add('select*form'+combobox2.Text);
注意空格,可设一变量sSql:string;
sSql:=Format('Select * Form '%s',[combobox2.Text] );//在此加断点
query1.SQL.Add(sSql);
 
sql语句有问题
 
ZZHI,按照你的语句,仍然问题存在,一模一样!!!
 
这是动态sql语句。DELPHI中所谓的动态sql语句仅仅是参数华的SQL语句,
并非真正的动态sql语句。首先要求数据库要支持动态sql语句,其次编程
语言也要支持。SQL SERVER 7.0支持(虽然on-line Help中没有说明),
sybase 12以上支持。 你可以执行一下语句以验证数据库是否支持动态sql语句。
exec( 'select * from xxx') ---xxx 代表实际表名
以上语句我是在不知该如何在DELPHI的query语句中表达,或者说delphi 5.0是否支持
动态sql语句,我不知道。 下面是另一个动态sql语句的例子
declare @tabname varchar(20)
select @tabname='xxxx' ---- 某一表名
exec('select * from '+@tabname)
 
procedure TForm1.BitBtn1Click(Sender: TObject);
var
dbname,tabname,sqlstr:string;
begin
dbname:=combobox1.text;
tabname:=combobox2.text;
query1.Close;
try
query1.DatabaseName:=dbname;/// 老兄阿,数据库想要随心所欲的
//更换,但数据库的驱动一定要相同!!!
query1.SQL.Clear;
sqlstr:=format('select * from %s',[tabname]0);
query1.SQL.Add(sqlstr);
query1.Open;
except
showmessage('not open file');
end;
end;
改过程在delphi 5下调试通过。
另外,你不给分,我抗议!!!
 
sql有问题
query1.SQL.Add('select*form'+combobox2.Text);
该成

query1.SQL.Add('select * from '''+combobox2.Text+'''');//from和form要注意哦:)

 
好好看一下 ('select*form'+combobox2.Text); 他的结果是什么? 在SQL中有这样的语句吗?select*form 加上空格吧!伙计!别省事
 
hcbride,按照你的方法,果然解决问题,也谢谢其他的哥们!!!
 
同意lmxtom的
'select*form'+combobox2.Text 连form都写错了还说.是from
改为:'select * from'+combobox2.Text
肯定行.不想给分就算了.你的分也少得可怜,算了.
 
接受答案了.
 
顶部