2014-11-05 64 views
0

我想使用Zeoslib组件在Windows 7上通过我的Delphi程序以编程方式创建数据库和表格。从目前为止我在网上找到的信息来看,Zeoslib希望在使用它之前创建数据库。如果是这样,有没有办法使用Zeoslib工具创建数据库和表格。使用ZeosLib的Delphi SQLite3,如何创建数据库和表?

回答

3

正常这个问题将被关闭,因为你没有显示什么你试过至今。

随着ZeosLib很容易

安全注意事项:
当然,你应该使用参数化查询。仅仅是为了简化程序,已在这里省略

创建数据库

procedure TForm1.CreateClick(Sender: TObject); 
begin 
    ZConnection1.Protocol:='sqlite-3'; 
    ZConnection1.Database:='F:\Programme\stack\SQLite\Database.sqlite'; 
    ZConnection1.Connect; 
    ZConnection1.Disconnect; 
end; 

创建一个表并插入

procedure TForm1.CreateInsertClick(Sender: TObject); 
begin 
    ZQuery1.SQL.Text := 'CREATE TABLE hardware (id INTEGER PRIMARY KEY, compname VARCHAR(30), username VARCHAR(30), model VARCHAR(30))'; 
    ZQuery1.ExecSQL; 
    ZQuery1.SQL.Text := 'CREATE INDEX sHardware ON hardware(compname)'; 
    ZQuery1.ExecSQL; 
    ZQuery1.SQL.Text := 'INSERT INTO hardware(id, compname, username, model) VALUES (1, "AMD8537", "OMonge", "Gigabyte");'; 
    ZQuery1.ExecSQL; 
end; 

要看到价值重新连接

procedure TForm1.ConnectClick(Sender: TObject); 
begin 
    ZConnection1.Connect; 
end; 

显示值

procedure TForm1.OpenClick(Sender: TObject); 
begin 
    ZQuery1.SQL.Text := 'SELECT id, compname FROM hardware'; 
    ZQuery1.Open; 
end; 

形式

enter image description here

运行

enter image description here

1

如果数据库文件不存在 - SQLite在连接上创建它。 下面是一个非常简单的,但作用的例子:

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ZConnection1.Protocol := 'sqlite-3'; 
    ZConnection1.Database := 'foo.s3db'; 
    if not FileExists('foo.s3db') then 
    begin 
     ZConnection1.Connect; 
     ZConnection1.ExecuteDirect('create table foo (bar integer)'); 
    end 
    else 
     ZConnection1.Connect; 
    ZConnection1.Disconnect; 
end; 
相关问题