[英]Delphi SQLite3 using ZeosLib, how to create a database and a table?
I want create a database and a table programmatically with my Delphi Program on Windows 7 using Zeoslib component. 我想在Windows 7上使用Zeoslib组件在Delphi程序中以编程方式创建数据库和表。 From what I have found online so far, Zeoslib is expecting database to be created before using it. 根据到目前为止我在网上发现的信息,Zeoslib期望在使用数据库之前先创建它。 If so, is there a way to create a database and a table using Zeoslib tools. 如果是这样,那么有一种方法可以使用Zeoslib工具创建数据库和表。
Normal this question will be closed because you did not show what have you tried so far. 正常情况下,此问题将被关闭,因为您到目前为止尚未展示您尝试过的内容。
With ZeosLib it's easy 借助ZeosLib,这很容易
Safety Note: 安全注意事项:
Of course you should use parameterized queries. 当然,您应该使用参数化查询。 only in order to simplify the procedure, it has been omitted here 仅是为了简化程序,此处已省略
Create the Database 创建数据库
procedure TForm1.CreateClick(Sender: TObject);
begin
ZConnection1.Protocol:='sqlite-3';
ZConnection1.Database:='F:\Programme\stack\SQLite\Database.sqlite';
ZConnection1.Connect;
ZConnection1.Disconnect;
end;
Create a Table and Insert 创建表并插入
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;
To see Values Connect again 再次看价值连接
procedure TForm1.ConnectClick(Sender: TObject);
begin
ZConnection1.Connect;
end;
Show Values 显示值
procedure TForm1.OpenClick(Sender: TObject);
begin
ZQuery1.SQL.Text := 'SELECT id, compname FROM hardware';
ZQuery1.Open;
end;
Form 形成
Running 跑步
If database file does not exists - SQLite creates it on connect. 如果数据库文件不存在-SQLite在连接时创建它。 Below is a very simple but functioning example: 以下是一个非常简单但有效的示例:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.