Здравейте, от скоро се занимавам с Lazarus. Намерих ръководство за връзка с mySQL 5. Работи! Проблема е, че мога да изпълнявам само SELECT запитвания.
Винаги ми казва dataset is read-only.
unit utrymysql;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
Buttons, ComCtrls, sqldb, mysql50conn,db, DbCtrls;
type
{ TFormTryMySQL }
TFormTryMySQL = class(TForm)
DBEdit1: TDBEdit;
MySQLConnection1: TMySQL50Connection;
OpenQueryButton: TButton;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
ConnectButton: TButton;
ExitButton: TButton;
CommandEdit: TEdit;
CommandLabel: TLabel;
Memo1: TMemo;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure ConnectButtonClick(Sender: TObject);
procedure ExitButtonClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure OpenQueryButtonClick(Sender: TObject);
private
{ private declarations }
procedure ClearSettings;
procedure CloseConnection(Sender: TObject);
procedure ShowString(const S: String);
public
{ public declarations }
end;
var
FormTryMySQL: TFormTryMySQL;
implementation
uses
UShowQuery;
{ TFormTryMySQL }
procedure TFormTryMySQL.ConnectButtonClick(Sender: TObject);
begin
// Check if we have an active connection. If so, let's close it.
if MySQLConnection1.Connected then begin
SQLTransaction1.Active := False;
MySQLConnection1.Close;
end;
// Set the connection parameters.
MySQLConnection1.HostName := 'localhost';
MySQLConnection1.UserName := 'user';
MySQLConnection1.Password := 'pass';
MySQLConnection1.DatabaseName := 'mysql'; // MySQL is allways there!
ShowString('Opening a connection to server: localhost'

'>;
MySQLConnection1.Open;
MySQLConnection1.DatabaseName := 'baza';
// First lets get a list of available databases.
if MySQLConnection1.Connected then begin
ShowString('Connected to server: localhost'

'>;
ShowString('Retreiving list of available databases.'

'>;
SQLQuery1.SQL.Text := 'show databases';
SQLQuery1.Open;
// Go to the first record [modified by Arwen]
if SQLQuery1.Active then begin
SQLQuery1.First;
ShowString('Query ACTIVE and at the first record'

'>;
end
else
ShowString('Query NOT ACTIVE'

'>;
// Read DATABASES
while not SQLQuery1.EOF do begin
SQLQuery1.Next;
end;
SQLQuery1.Close;
ShowString('List of databases received!'

'>;
end;
end;
procedure TFormTryMySQL.Button1Click(Sender: TObject);
begin
ShowQueryForm := TSHowQueryForm.Create(nil);
try
ShowQueryForm.ShowModal;
finally
ShowQueryForm.Free;
end;
end;
procedure TFormTryMySQL.ExitButtonClick(Sender: TObject);
begin
Close;
end;
procedure TFormTryMySQL.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
// Make sure our connection is closed, when we close our application.
if MySQLConnection1.Connected then CloseConnection(Sender);
CanClose := True;
end;
procedure TFormTryMySQL.FormCreate(Sender: TObject);
begin
ShortTimeFormat := 'hh:nn:ss.zzz';
end;
procedure TFormTryMySQL.OpenQueryButtonClick(Sender: TObject);
begin
ShowQueryForm := TShowQueryForm.Create(nil);
try
ShowQueryForm.DataSource1.DataSet := SQLQuery1;
SQLQuery1.SQL.Text := CommandEdit.Text;
SQLQuery1.Post;
SQLQuery1.ApplyUpdates;
SQLTransaction1.CommitRetaining;
ShowQueryForm.ShowModal;
finally
ShowQueryForm.Free;
SQLQuery1.Close;
end;
end;
procedure TFormTryMySQL.ClearSettings;
begin
OpenQueryButton.Enabled := Enabled;
end;
procedure TFormTryMySQL.CloseConnection(Sender: TObject);
begin
// The SQLTransaction1 gets activated automatically, but before we can close
// the connection we have to set the SQLTransaction1.Active to false.
SQLTransaction1.Active := False;
MySQLConnection1.Close;
// Information about the list of tables will propably needs refresh
// and the fields list of course as well.
// OpenQueryButton should be disabled anyhow.
OpenQueryButton.Enabled := False;
// If this is called from anything else then from SelectDBButtonClick
// the SelectDBButton should be disabled as well.
end;
procedure TFormTryMySQL.ShowString(const S: String);
begin
Memo1.Lines.Add(TimeToStr(now) + ': ' + S);
end;
initialization
{$I utrymysql.lrs}
end.
Някакви идеи?
Lazarus 0.9.22 beta по windows