この記事では、Delphi7 が MySQL データベースに接続する実装方法を簡単に紹介します。具体的な手順は次のとおりです。
まずダウンロードします: http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
次に、ダウンロードした dbxopenmysql5_dll.zip を解凍し、dbxopenmysql50.dll と libmysql.dll の両方をプロジェクト フォルダーに配置します。
TSQLConnection、TSQLQuery、TStringGrid、3 つの TButtons、および TLable をフォームに配置します。
次のコードを追加します。
ユニット Unit1; インターフェイスは Windows、メッセージ、SysUtils、バリアント、クラス、グラフィックス、コントロール、フォーム、ダイアログ、DBXpress、FMTBcd、StdCtrls、グリッド、DB、SqlExpr を使用します。タイプ TForm1 = class(TForm) SQLConnection1: TSQLConnection; TSQLQuery1; : TStringGrid ボタン 1: TButton; Button2: TButton; Button3: TLabel; プロシージャ Button1Click(Sender: TObject); var Form1: TForm1 実装 {$R *.dfm} プロシージャTForm1.Button1Click(Sender: TObject);begin SQLConnection1 := TSQLConnection.Create(nil); SQLConnection1.DriverName := 'dbxmysql'; SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50'; SQLConnection1.LibraryName := 'dbxopenmysql50.dll'; SQLConnection1.VendorLib := 'libmysql.dll'; SQLConnection1.LoginPrompt := false; SQLConnection1.Params.Append('User_Name=root'); 'パスワード='); SQLConnection1.Params.Append('HostName=localhost'); SQLConnection1.Connected = true の場合、SQLQuery1.SQLConnection1 := '成功!';失敗しました!';終了; プロシージャ TForm1.Button2Click(Sender: TObject);var i, j: 整数;begin SQLQuery1.SQL.Clear; SQLQuery1.SQL.Add('SELECT * FROM user'); SQLQuery1.eof ではない場合、j := 0 が始まります。 .FieldCount - 1 do StringGrid1.cells[j, i] := SQLQuery1.Fields[j].AsString; SQLQuery1.next; inc(i); 終了; SQLQuery1.Active := false; プロシージャ TForm1.Button3Click(Sender: TObject); SQLConnection1.Connected = true の場合に開始します。
テスト後、通常の接続とクエリが可能になります。