简体   繁体   中英

Delphi code will not save to Mysql Database

Ok i come from a C# background. This did not save to the Mysql database Table. I struggled to have it communicate with the database, but its not inserting to the database Table. it just shows the messagebox, but when i refresh, nothing shows up in the database. Code looks like this

unit mysqlConn;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sqldb, mysql56conn, Forms, Controls, Graphics,
  Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    MySQL56Connection1: TMySQL56Connection;
    SQLQuery1: TSQLQuery;
    SQLTransaction1: TSQLTransaction;
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.ComboBox1Change(Sender: TObject);
begin

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SQLQuery1.SQL.Clear;
  SQLQuery1.SQL.Add('insert into people_info(name,email,telephone,gender)');
  SQLQuery1.SQL.Add('values(:name,:email,:telephone,:gender)');
  SQLQuery1.Params.ParamByName('name').AsString:=Edit1.Text;
  SQLQuery1.Params.ParamByName('email').AsString:=Edit2.Text;
  SQLQuery1.Params.ParamByName('telephone').AsString:=Edit3.Text;
  SQLQuery1.Params.ParamByName('gender').AsString:=Edit4.Text;
  SQLQuery1.ExecSQL;
  showmessage('Ok I am saved!');
end;

end.

What Do i appear to be missing ?

I did not add the SQLTransaction1.Commit and hence Saving to the Database was null and Void. Code looks like this now :

unit mysqlConn;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sqldb, mysql56conn, Forms, Controls, Graphics,
  Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    MySQL56Connection1: TMySQL56Connection;
    SQLQuery1: TSQLQuery;
    SQLTransaction1: TSQLTransaction;
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.ComboBox1Change(Sender: TObject);
begin

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SQLQuery1.SQL.Clear;
  SqlQuery1.SQL.text:='insert into people_info (name, email, telephone, gender) values (:name,:email,:telephone,:gender)';
  SqlQuery1.Params.ParamByName('name').AsString := Edit1.text;
  SqlQuery1.Params.ParamByName('email').AsString := Edit2.text;
  SqlQuery1.Params.ParamByName('telephone').AsString := Edit3.text;
  SqlQuery1.Params.ParamByName('gender').AsString := Edit4.text;
  SQLQuery1.ExecSQL;
  SQLTransaction1.Commit; // This saved to the database
  showmessage('Ok I am saved!');
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
end;

end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM