2

I want create a database and a table programmatically with my Delphi Program on Windows 7 using Zeoslib component. From what I have found online so far, Zeoslib is expecting database to be created before using it. If so, is there a way to create a database and a table using Zeoslib tools.

2 Answers 2

5

Normal this question will be closed because you did not show what have you tried so far.

With ZeosLib it's easy

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

enter image description here

Running

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

If database file does not exists - SQLite creates it on connect. 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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.