Help the beginner please. Syntax and some questions

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
SanyaXYZ
Posts: 11
Joined: Wed 20 Oct 2010 17:22

Help the beginner please. Syntax and some questions

Post by SanyaXYZ » Sat 18 Jun 2011 12:11

Hi all,
my english is bad, i'm sorry.
I download PgDAC demo.
I want to use these components in the future,
but I the beginning programmer and hope for your help.
I download demo project, but this software is very difficult for me.
I suffer some months and is compelled to address for the help.

I have Delphi 2010, postgreSQL 9.0


I have:

added Button1, Button2, Button3 on the Form
added PgConnection1 on the Form

{ Create Database -------------------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
begin
PgConnection1.Server:= 'localhost';
PgConnection1.Port:=5432;//<-- your port number
PgConnection1.Username:= 'postgres';
PgConnection1.Password:= '11111';
PgConnection1.Connect;
PgConnection1.ExecSQL('CREATE DATABASE mybase WITH OWNER = postgres ENCODING = ''UTF8''',[null]);
end;
{ Create Database ===========================}


{Connect --------------------------------------------------------}
procedure TForm1.Button2Click(Sender: TObject);
begin
PgConnection1.username:='postgres';
PgConnection1.password:='11111';
PgConnection1.database:='mybase';
PgConnection1.connected:=true;
end;
{Connect ================================}

{Disconnect ------------------------------------------------------}
procedure TForm1.Button3Click(Sender: TObject);
begin
PgConnection1.connected:=false;
end;
{Disconnect ==============================}


Help me please:

How to create the table?
How to add a strings in the table?
How to read from the table of a strings and add in TMemo (for example).
What I should use components?
Set an example please.

I hope for your help :oops:

SanyaXYZ
Posts: 11
Joined: Wed 20 Oct 2010 17:22

Post by SanyaXYZ » Sun 19 Jun 2011 14:04

pgQuery1.SQL.text:='CREATE TABLE `Products` (prod_id CHAR (10) NOT NULL)';
pgQuery1.ExecSQL;


One problem left :oops:

SanyaXYZ
Posts: 11
Joined: Wed 20 Oct 2010 17:22

Post by SanyaXYZ » Sun 19 Jun 2011 15:43

for mySQL, for postgresql I didn't try

ss:='dfs';
try
MyQuery1.SQL.text:='BEGIN TRANSACTION';
for I := 1 to 10 do begin
MyQuery1.SQL.Text:='insert into `Products` (prod_id) values("'+ss+'")';
MyQuery1.ExecSQL;
end;
MyQuery1.SQL.Text:='COMMIT';

except
MyQuery1.SQL.Text:='Rollback';
end;




{------------------------------------}

MyQuery1.SQL.Text:='select prod_id from Products';
MyQuery1.ExecSQL;

MyQuery1.Open;
while not MyQuery1.Eof
do begin
Memo1.Lines.Add(MyQuery1.FieldValues['prod_id']);
MyQuery1.Next;
end;

I didn't sleep two days :wink:

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Mon 20 Jun 2011 07:34

Hello,

Below is a small sample demonstrating creation of database/table and writing/reading data from the created table:

Code: Select all

procedure TForm1.btnCreateDBClick(Sender: TObject);
begin
  if PgConnection1.Connected then
    PgConnection1.Disconnect;

    PgConnection1.Server:= 'db';
    PgConnection1.Port:= 5438;
    PgConnection1.Username:= 'postgres';
    PgConnection1.Password := 'postgres';
    try
      PgConnection1.Connect;
      PgConnection1.ExecSQL('create database mydatabase with owner = postgres encoding = ''UTF8''',[]);
    finally
      PgConnection1.Disconnect;
    end;
end;

procedure TForm1.btnConnectClick(Sender: TObject);
begin
  if PgConnection1.Connected then
    PgConnection1.Disconnect;

  PgConnection1.username:='postgres';
  PgConnection1.password:='postgres';
  PgConnection1.database:='mydatabase';
  PgConnection1.Connect;
  PgQuery1.SQL.Text := 'select prod_id from products';
end;

procedure TForm1.btnDisconnectClick(Sender: TObject);
begin
  if PgConnection1.Connected then
    PgConnection1.Disconnect;
end;

procedure TForm1.btnCreateTableClick(Sender: TObject);
begin
  if not PgConnection1.Connected then
    btnConnectClick(self);

  PgConnection1.ExecSQL('create table products (prod_id char(10) not null)',[]);
end;

procedure TForm1.btnFillTableClick(Sender: TObject);
var
  i: integer;
begin
  if not PgConnection1.Connected then
    btnConnectClick(self);

  PgConnection1.StartTransaction;
  try
    PgQuery1.Open;
    for i:= 0 to 10 do
    begin
      PgQuery1.Insert;
      PgQuery1.FieldByName('prod_id').AsString:='test_'+IntToStr(i);
      PgQuery1.Post;
    end;
    PgConnection1.Commit;
    PgQuery1.Close;
  Except
    PgQuery1.RestoreUpdates;
    PgConnection1.Rollback;
    raise;
  end;
end;

procedure TForm1.btnGetDataClick(Sender: TObject);
begin
  if not PgConnection1.Connected then
    btnConnectClick(self);

  memo1.Lines.Clear;
  PgQuery1.Open;
  while not PgQuery1.Eof do
  begin
    memo1.Lines.Add(PgQuery1.FieldByName('prod_id').AsString);
    PgQuery1.Next;
  end;
  PgQuery1.Close;
end;

SanyaXYZ
Posts: 11
Joined: Wed 20 Oct 2010 17:22

Post by SanyaXYZ » Mon 20 Jun 2011 10:46

Many thanks AlexP !!!
You have very much helped !!! ^^

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Mon 20 Jun 2011 11:00

Hello,

If you have any other questions, feel free to contact us.

Post Reply