Anlauf 2, Ergebnis, mit gwen3, LM Studio create a delphi pascal programm with a postgres customer example database for windows desktop Here's a complete Windows Desktop Delphi Pascal application that demonstrates working with a PostgreSQL customer database: ```pascal program CustomerDatabaseApp; uses Vcl.Forms, System.SysUtils, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Stan.Connection, FireDAC.Stan.Error, FireDAC.Phys.Intf, FireDAC.Phys.PGSQLDef, FireDAC.Phys.PGSQL, FireDAC.VCLUI.Wait, Data.DB, FireDAC.DApt, FireDAC.DApt.Intf, FireDAC.DatS, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Controls, Vcl.Dialogs; {$R *.res} type TForm1 = class(TForm) Panel1: TPanel; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; EditFirstName: TEdit; EditLastName: TEdit; EditEmail: TEdit; EditPhone: TEdit; EditAddress: TEdit; ButtonAdd: TButton; ButtonUpdate: TButton; ButtonDelete: TButton; ButtonRefresh: TButton; DBGrid1: TDBGrid; DataSource1: TDataSource; FDConnection1: TFDConnection; FDQuery1: TFDQuery; FDQuery1id: TIntegerField; FDQuery1first_name: TStringField; FDQuery1last_name: TStringField; FDQuery1email: TStringField; FDQuery1phone: TStringField; FDQuery1address: TMemoField; FDQuery1created_at: TDateTimeField; procedure FormCreate(Sender: TObject); procedure ButtonAddClick(Sender: TObject); procedure ButtonUpdateClick(Sender: TObject); procedure ButtonDeleteClick(Sender: TObject); procedure ButtonRefreshClick(Sender: TObject); procedure DBGrid1DblClick(Sender: TObject); procedure FormDestroy(Sender: TObject); private procedure LoadCustomers; procedure ClearFields; procedure SelectCustomer(const AId: Integer); public end; var Form1: TForm1; procedure TForm1.FormCreate(Sender: TObject); begin // Configure PostgreSQL connection FDConnection1.DriverID := 'PGSQL'; FDConnection1.Params.Clear; FDConnection1.Params.Add('Database=customerdb'); FDConnection1.Params.Add('User_Name=postgres'); FDConnection1.Params.Add('Password=mypassword'); FDConnection1.Params.Add('Server=localhost'); FDConnection1.Params.Add('Port=5432'); try FDConnection1.Connected := True; Writeln('Connected to PostgreSQL database successfully!'); except on E: Exception do begin ShowMessage('Database connection failed: ' + E.Message); Exit; end; end; // Create sample customer table if it doesn't exist try FDQuery1.SQL.Text := 'CREATE TABLE IF NOT EXISTS customers (' + 'id SERIAL PRIMARY KEY, ' + 'first_name VARCHAR(50) NOT NULL, ' + 'last_name VARCHAR(50) NOT NULL, ' + 'email VARCHAR(100) UNIQUE NOT NULL, ' + 'phone VARCHAR(20), ' + 'address TEXT, ' + 'created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)'; FDQuery1.ExecSQL; // Insert sample data if table is empty FDQuery1.SQL.Text := 'SELECT COUNT(*) FROM customers'; FDQuery1.Open; if FDQuery1.Fields[0].AsInteger = 0 then begin FDQuery1.Close; FDQuery1.SQL.Text := 'INSERT INTO customers (first_name, last_name, email, phone, address) ' + 'VALUES ' + '(:fname1, :lname1, :email1, :phone1, :address1), ' + '(:fname2, :lname2, :email2, :phone2, :address2)'; FDQuery1.ParamByName('fname1').Value := 'John'; FDQuery1.ParamByName('lname1').Value := 'Doe'; FDQuery1.ParamByName('email1').Value := 'john.doe@example.com'; FDQuery1.ParamByName('phone1').Value := '+1234567890'; FDQuery1.ParamByName('address1').Value := '123 Main St, City, State'; FDQuery1.ParamByName('fname2').Value := 'Jane'; FDQuery1.ParamByName('lname2').Value := 'Smith'; FDQuery1.ParamByName('email2').Value := 'jane.smith@example.com'; FDQuery1.ParamByName('phone2').Value := '+1234567891'; FDQuery1.ParamByName('address2').Value := '456 Oak Ave, City, State'; FDQuery1.ExecSQL; end; LoadCustomers; except on E: Exception do ShowMessage('Error setting up database: ' + E.Message); end; end; procedure TForm1.LoadCustomers; begin try FDQuery1.Close; FDQuery1.SQL.Text := 'SELECT * FROM customers ORDER BY id'; FDQuery1.Open; except on E: Exception do ShowMessage('Error loading customers: ' + E.Message); end; end; procedure TForm1.ClearFields; begin EditFirstName.Text := ''; EditLastName.Text := ''; EditEmail.Text := ''; EditPhone.Text := ''; EditAddress.Text := ''; end; procedure TForm1.SelectCustomer(const AId: Integer); begin try FDQuery1.Close; FDQuery1.SQL.Text := 'SELECT * FROM customers WHERE id = :id'; FDQuery1.ParamByName('id').Value := AId; FDQuery1.Open; if not FDQuery1.Eof then begin EditFirstName.Text := FDQuery1.FieldByName('first_name').AsString; EditLastName.Text := FDQuery1.FieldByName('last_name').AsString; EditEmail.Text := FDQuery1.FieldByName('email').AsString; EditPhone.Text := FDQuery1.FieldByName('phone').AsString; EditAddress.Text := FDQuery1.FieldByName('address').AsString; end; except on E: Exception do ShowMessage('Error selecting customer: ' + E.Message); end; end; procedure TForm1.ButtonAddClick(Sender: TObject); begin try if (EditFirstName.Text = '') or (EditLastName.Text = '') or (EditEmail.Text = '') then begin ShowMessage('Please fill in required fields (First Name, Last Name, Email)'); Exit; end; FDQuery1.Close; FDQuery1.SQL.Text := 'INSERT INTO customers (first_name, last_name, email, phone, address) ' + 'VALUES (:fname, :lname, :email, :phone, :address)'; FDQuery1.ParamByName('fname').Value := EditFirstName.Text; FDQuery1.ParamByName('lname').Value := EditLastName.Text; FDQuery1.ParamByName('email').Value := EditEmail.Text; FDQuery1.ParamByName('phone').Value := EditPhone.Text; FDQuery1.ParamByName('address').Value := EditAddress.Text; FDQuery1.ExecSQL; LoadCustomers; ClearFields; ShowMessage('Customer added successfully!'); except on E: Exception do ShowMessage('Error adding customer: ' + E.Message); end; end; procedure TForm1.ButtonUpdateClick(Sender: TObject); begin try if (EditFirstName.Text = '') or (EditLastName.Text = '') or (EditEmail.Text = '') then begin ShowMessage('Please select a customer to update'); Exit; end; FDQuery1.Close; FDQuery1.SQL.Text := 'UPDATE customers SET first_name=:fname, last_name=:lname, email=:email, phone=:phone, address=:address ' + 'WHERE id=:id'; FDQuery1.ParamByName('fname').Value := EditFirstName.Text; FDQuery1.ParamByName('lname').Value := EditLastName.Text; FDQuery1.ParamByName('email').Value := EditEmail.Text; FDQuery1.ParamByName('phone').Value := EditPhone.Text; FDQuery1.ParamByName('address').Value := EditAddress.Text; FDQuery1.ParamByName('id').Value := FDQuery1.FieldByName('id').AsInteger; FDQuery1.ExecSQL; LoadCustomers; ClearFields; ShowMessage('Customer updated successfully!'); except on E: Exception do ShowMessage('Error updating customer: ' + E.Message); end; end; procedure TForm1.ButtonDeleteClick(Sender: TObject); begin try if (EditFirstName.Text = '') or (EditLastName.Text = '') or (EditEmail.Text = '') then begin ShowMessage('Please select a customer to delete'); Exit; end; if MessageDlg('Are you sure you want to delete this customer?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin FDQuery1.Close; FDQuery1.SQL.Text := 'DELETE FROM customers WHERE id=:id'; FDQuery1.ParamByName('id').Value := FDQuery1.FieldByName('id').AsInteger; FDQuery1.ExecSQL; LoadCustomers; ClearFields; ShowMessage('Customer deleted successfully!'); end; except on E: Exception do ShowMessage('Error deleting customer: ' + E.Message); end; end; procedure TForm1.ButtonRefreshClick(Sender: TObject); begin LoadCustomers; end; procedure TForm1.DBGrid1DblClick(Sender: TObject); begin if not FDQuery1.Eof then begin SelectCustomer(FDQuery1.FieldByName('id').AsInteger); end; end; procedure TForm1.FormDestroy(Sender: TObject); begin FDConnection1.Connected := False; end; begin Application.Initialize; Application.MainFormOnTaskBar := True; Application.CreateForm(TForm1, Form1); Application.Run; end. ``` **To use this application:** 1. **Create a new VCL Forms Application in Delphi** 2. **Add the following components to the form:** - TPanel (Panel1) - TLabel (Label1, Label2, Label3, Label4, Label5) - TEdit (EditFirstName, EditLastName, EditEmail, EditPhone, EditAddress) - TButton (ButtonAdd, ButtonUpdate, ButtonDelete, ButtonRefresh) - TDBGrid (DBGrid1) - TDataSource (DataSource1) - TFDConnection (FDConnection1) - TFDQuery (FDQuery1) 3. **Configure the components:** - Set DataSource1.DataSet to FDQuery1 - Set DBGrid1.DataSource to DataSource1 - Set FDQuery1.Connection to FDConnection1 4. **Install Required Components:** - Make sure FireDAC is installed in your Delphi IDE - Install PostgreSQL ODBC driver or use FireDAC's native PostgreSQL support 5. **Database Setup:** - Create a database named `customerdb` in PostgreSQL - Update connection parameters: ```pascal FDConnection1.Params.Add('Database=customerdb'); FDConnection1.Params.Add('User_Name=postgres'); FDConnection1.Params.Add('Password=mypassword'); ``` **Features of this Desktop Application:** - **CRUD Operations**: Create, Read, Update, Delete customers - **Form-based Interface**: User-friendly Windows desktop interface - **Real-time Data Display**: DBGrid shows all customers - **Data Validation**: Checks for required fields - **Error Handling**: Proper exception handling with user messages - **Auto Setup**: Automatically creates table and sample data - **Connection Management**: Proper connection handling **Expected Interface Layout:** ``` +-----------------------------------------------------+ | [First Name] [Last Name] [Email] [Phone] [Address] | | | | [Add] [Update] [Delete] [Refresh] | | | | ID | First Name | Last Name | Email | |----|------------|-----------|------------------| | 1 | John | Doe | john.doe@email.com| | 2 | Jane | Smith | jane.smith@email.com| +-----------------------------------------------------+ ``` **Requirements:** - Delphi IDE (Community, Professional, or Enterprise) - PostgreSQL database server - FireDAC components installed - PostgreSQL ODBC driver or native PostgreSQL support The application will automatically create the customers table and insert sample data if it doesn't exist. Users can add, update, delete, and refresh customer records through the intuitive GUI interface.