Add data to a Sqlite database

Posted by Rahman M On Friday, August 17, 2012 1 comments

In the previous post I have given way to connect to a sqlite database through delphi.
this time we will enter data into a sqlite database
  
  
made form as shown below.











code as shown below.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ZAbstractRODataset, ZAbstractDataset, ZDataset, ZConnection,
  ComCtrls, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids;

type
  TForm1 = class(TForm)
    ZConnection1: TZConnection;
    Query1: TZQuery;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    dtp1: TDateTimePicker;
    Bevel1: TBevel;
    BitBtn1: TBitBtn;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Query2: TZQuery;
    procedure FormActivate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
//query to insert data
with Query1 do
begin
  Close;
  SQL.Text:='Insert into empl_list(empl_id,empl_name,birthday)values('+QuotedStr(Edit1.Text)+','+QuotedStr(Edit2.Text)+','+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date))+')';
  ExecSQL;
end;
//query to fetching data
with Query2 do
begin
  Close;
  SQL.Text:='Select empl_id,empl_name,birthday from empl_list';
  Open;
end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
//connection properties
ZConnection1.Protocol:='sqlite-3';
ZConnection1.Database:=ExtractFilePath(Application.ExeName) + '\employee.sdb';
ZConnection1.Connect;

//check connection
{if ZConnection1.Connected=true then
begin
  Application.MessageBox('connection successfully done','Checking connection',MB_ICONINFORMATION);
end
else
  Application.MessageBox('connection failed','Checking connection',MB_ICONSTOP); }
end;

end.
do compile and enter data, then click the Add button.
if no errors will appear as shown below.











This can be used also to connect to mysql server with a few changes that you will get the next post.

1 comments:

karthik said...

this is such a nice and useful information for us...i appreciate urs word Free Blogger premium Themes

Post a Comment