这样使用tquery添加记录为何错误?(100分)

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

syw

Unregistered / Unconfirmed
GUEST, unregistred user!
tjquery.Insert;
tjquery.RequestLive:=true;
tjquery.SQL.Clear;
tjquery.SQL.Add('insert into workers'+'(编号,姓名,性别,职位,工资,出生年月,电话,住址)'
+'value'+'(:bid,:name,:sex,:job,:salary,:birth,:phone,:address)');
tjquery.ParamByName('bid').AsString:=tjedit1.Text;
tjquery.ParamByName('name').AsString:=tjedit2.Text;
tjquery.ParamByName('sex').AsString:=tjcombobox.Text;
tjquery.ParamByName('job').AsString:=tjedit4.Text;
tjquery.ParamByName('salary').AsInteger:=strtoint(tjedit5.Text);
tjquery.ParamByName('birth').AsDate:=strtodate(tjmaskedit.Text);
tjquery.ParamByName('phone').AsString:=tjedit7.Text;
tjquery.ParamByName('address').AsString:=tjedit8.Text;
tjquery.Post;
错误类型:cannot modify a read-only dataset
 
改成如下应该可以的。。
tjquery.close;
tjquery.SQL.Clear;
tjquery.SQL.Add('insert into workers'+'(编号,姓名,性别,职位,工资,出生年月,电话,住址)'
+'value'+'(:bid,:name,:sex,:job,:salary,:birth,:phone,:address)');
tjquery.ParamByName('bid').AsString:=tjedit1.Text;
tjquery.ParamByName('name').AsString:=tjedit2.Text;
tjquery.ParamByName('sex').AsString:=tjcombobox.Text;
tjquery.ParamByName('job').AsString:=tjedit4.Text;
tjquery.ParamByName('salary').AsInteger:=strtoint(tjedit5.Text);
tjquery.ParamByName('birth').AsDate:=strtodate(tjmaskedit.Text);
tjquery.ParamByName('phone').AsString:=tjedit7.Text;
tjquery.ParamByName('address').AsString:=tjedit8.Text
tjquery.prepared;
tjquery.execsql;
 
看看你的那个表的属性,如果是dbf表,看看是不是只读的
 
你使有的是SQL语言增加数据,没有使用到数据更新。请修改:
基本与 cbdiy 相同

tjquery.Insert;//删除
tjquery.Post;//改为tjquery.execsql;

 
稍微说一下你的错误吧。
1、下面的两个语句使用颠倒,只有在RequireLive=True的时候才能修改Query数据集,否则会出异常的。
你颠倒了这两句话,就等于没有设置RequireLive就插入数据,就出现了你的错误。
tjquery.Insert;
tjquery.RequestLive:=true;
2、你将SQL语言和Query本身的Insert混淆了。
如果你要向数据集里面插入数据,可以使用SQL语言的Insert,也可以使用Query的Insert。
对于SQL语言的Insert,不需要设置RequireList等,直接执行就可以了。
对于Query的Insert,需要设置Requirelive,同时插入的时候是使用
Query1.FieldByName('col1').AsString:='sadfas'
来实现数据的增加的。
 
改 tjquery.Post;
为 tjquery.execsql;
 
又出问题了:
这一次是+'value'+'(:bid,:name,.....,:address)')出问题
错误类型:token:value(?,?,?,?,?,?,...,?,?)
不知sql添加语句是否写错?
 
是Values而不是value
 
运行中又有 paramterc错误:
类型:invalid parameter.'.process stopped
代码:
tjquery.close;
tjquery.SQL.Clear;
tjquery.SQL.Add('insert into workers'+'(±àºÅ£¬ÐÕÃû£¬ÐÔ±ð£¬Ö°Î»£¬¹¤×Ê£¬³öÉúÄêÔ£¬µç»°£¬×¡Ö·)'
+'values'+'(:bid,:name,:sex,:job,:salary,:birth,:phone,:address)');
tjquery.ParamByName('bid').AsString:=tjedit1.Text;
tjquery.ParamByName('name').asstring:=tjedit2.text;
tjquery.ParamByName('sex').AsString:=tjcombobox.Text;
tjquery.ParamByName('job').AsString:=tjedit3.Text;
tjquery.ParamByName('salary').AsInteger:=strtoint(tjedit4.Text);
tjquery.ParamByName('birth').AsDate:=strtodate(tjmaskedit.Text);
tjquery.ParamByName('phone').AsString:=tjedit5.Text;
tjquery.ParamByName('address').AsString:=tjedit6.Text;
if (tjquery.Prepared=false) then tjquery.Prepared:=true ;
tjquery.execsql;
end;
 
在我修改后,下列代码还有问题:
cannot read-only dataset;
query1.Insert;
query1.RequestLive:=true;
query1.FieldByName('񅧏').AsString:=edit1.Text;
query1.FieldByName('ÐÕÃû').AsString:=edit2.Text;
query1.FieldByName('ÐÔ±ð').AsString:=combobox1.Text;
query1.FieldByName('Ö°Îñ').AsString:=edit3.Text;
query1.FieldByName('¹¤×Ê').AsInteger:=strtoint(edit4.Text);
query1.FieldByName('³öÉúÄêÔÂ').AsDateTime:=strtodate(maskedit1.Text);
query1.FieldByName('סַ').AsString:=edit5.Text;
query1.FieldByName('µç»°').AsString:=edit6.Text;
query1.ExecSQL;
 
后退
顶部