导航
  • 主页
  • 语言基础/算法/系统设计
  • 数据库相关
  • 图形处理/多媒体
  • 网络通信/分布式开发
  • VCL组件开发及应用
  • Windows SDK/API
  • 问答

新手求教,help!

newmankind 2004-04-02 01:49:16
一个向SQL数据库自动添加数据的程序
与数据库显示都很没有问题,可是为什么我的
SQL语句老出错,搞了两天了
郁闷
请大家指教,谢谢!!
interface

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

type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k,m,n:Integer;
const
ho:array[0..4] of pchar=('红','绿','黄','白','蓝');
pe:array[0..4] of pchar=('狗','鸟','鱼','马','猫');
dr:array[0..4] of pchar=('茶','咖啡','牛奶','啤酒','水');
ci:array[0..4] of pchar=('PALL','DUN','BLEN','BLUE','PRIN');
na:array[0..4] of pchar=('英国','瑞典','丹麦','挪威','德国');

begin
for i:=0 to 4 do begin
if i=5 then break;
for j:=0 to 4 do begin
if j=5 then break;
for k:=0 to 4 do begin
if k=5 then break;
for m:=0 to 4 do begin
if m=5 then break;
for n:=0 to 4 do begin
if n=5 then
break;
with ADOQuery1 do
begin
if Active then Close;
SQL.Clear;
SQL.Add('INSERTINTO northwind.dbo.problem (house,pet,drink,cigarette,nationality) VALUES (" "," "," "," "," ")');
ExecSQL; Open 。
end;
end;
end;
end;
end;
end;
end;
end.

...全文
8 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
insert2003 2004-04-02
密码就是你的登录密码:P
回复
pandarus 2004-04-02
打开你的贴子/点管理/输入密码给分!
回复
newmankind 2004-04-02
如何结贴呢?
回复
newmankind 2004-04-02
万分感谢!
大哥
回复
cll007 2004-04-02
建议Pchar改成String类型
回复
cll007 2004-04-02
//测试所用表
create table problem
(
house varchar (20),
pet varchar (20),
drink varchar (20),
cigarette varchar (20),
nationality varchar (20),

)
select * from problem
---------
程序代码
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls;
const
ho:array[0..4] of pchar=('红','绿','黄','白','蓝');
pe:array[0..4] of pchar=('狗','鸟','鱼','马','猫');
dr:array[0..4] of pchar=('茶','咖啡','牛奶','啤酒','水');
ci:array[0..4] of pchar=('PALL','DUN','BLEN','BLUE','PRIN');
na:array[0..4] of pchar=('英国','瑞典','丹麦','挪威','德国');
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection; //要先指定其ConnectiongString属性
ADOQuery1: TADOQuery;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k,m,n:Integer;
begin
for i:=0 to 4 do
for j:=0 to 4 do
for k:=0 to 4 do
for m:=0 to 4 do
for n:=0 to 4 do
with ADOQuery1 do
begin
if Active then Close;
SQL.Clear;

SQL.Add('INSERT INTO problem (house,pet,drink,cigarette,nationality) ');
SQL.Add('VALUES ('+QuotedStr(ho[i])+','+QuotedStr(pe[j])+','+
QuotedStr(dr[k])+','+QuotedStr(ci[m])+','+
QuotedStr(na[n])+')');
ExecSQL;
end;
end;

end.

回复
newmankind 2004-04-02
源码是这样的
刚才发的错了
大哥能帮忙改一下么,贴出来么,
unit zhili;

interface

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

type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k,m,n:Integer;
const
ho:array[0..4] of pchar=('红','绿','黄','白','蓝');
pe:array[0..4] of pchar=('狗','鸟','鱼','马','猫');
dr:array[0..4] of pchar=('茶','咖啡','牛奶','啤酒','水');
ci:array[0..4] of pchar=('PALL','DUN','BLEN','BLUE','PRIN');
na:array[0..4] of pchar=('英国','瑞典','丹麦','挪威','德国');

begin
for i:=0 to 4 do begin
if i=5 then break;
for j:=0 to 4 do begin
if j=5 then break;
for k:=0 to 4 do begin
if k=5 then break;
for m:=0 to 4 do begin
if m=5 then break;
for n:=0 to 4 do begin
if n=5 then
break;
with ADOQuery1 do
begin
if Active then Close;
SQL.Clear;
SQL.Add('INSERT INTO northwind.dbo.problem (house,pet,drink,cigarette,nationality) VALUES (ho[i],pe[j],dr[k],ci[m],na[n])');
ExecSQL;
end;
end;
end;
end;
end;
end;
end;
end.
回复
cll007 2004-04-02
if j=5 then break; //首先,几个 i,j,k,m,n都没用
use northwind

INSERT INTO northwind.dbo.table1 (d,begindate,pay) VALUES (34,'2004-01-01',32)
select * from table1 //insert into 之间需要空格
回复
发动态
发帖子
Delphi
创建于2007-08-02

4813

社区成员

Delphi 开发及应用
申请成为版主
社区公告
暂无公告