delphi mysql

Delphi2006 connection Mysql5.1

2. DBExpress + dbxopenmysql50.dll
Many people may be wondering, dbxopenmysql50.dll what stuff? DBExpress database connectivity components is not yet, why add this thing? This is due to poor Delphi2006 in DBExpress support for high version of Mysql, seen from the foreign forum argument seems to be did not even realize, so that although the TSQLConnection component provides Mysql option, but the words are not used directly in ( low version of mysql may be), I encountered the phenomenon is prompt "Unable to Load libmysql.dll", but in fact I have in the system directory System32, Delphi installation bin directory, the development projects are placed in a folder the document, still can not find the dll.
dbxopenmysql50.dll developed by foreigners, but also open source, or foreigners Well, you can go to the following URL to download:
http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
need to use dbxopenmysql50.dll and libmysql .dll are placed in the project folder. Incidentally, libmysql.dll in the installation directory mysql \ lib \ opt directory.
Using two ways, one is to directly modify dbxdrivers.ini \ at Borland \ BDS \ 4.0 dbExpress adjust the parameters on which the mysql.
Another is specified in the program, and now I'm in such a way as an example the development of this method of connection.
TSQLConnection placed on the Form, TSQLQuery, TStringGrid, 3 Ge TButton, TLable. FIG screen as follows.


In the event FormCreate:
to SQLConnection1: = TSQLConnection.Create (nil);
SQLConnection1.DriverName: = 'dbxmysql';
SQLConnection1.GetDriverFunc: = 'getSQLDriverMYSQL50';
SQLConnection1.LibraryName: = 'dbxopenmysql50.dll';
SQLConnection1.VendorLib: = ' libmysql.dll ';
SQLConnection1.LoginPrompt: = to false;
in this arrangement TSQLConnection respective specific parameters, of course, can also modify the properties directly in the panel assembly, or modifying the corresponding parameters in dbxdrivers.ini, a variety of methods.
Connect button event:
SQLConnection1.Params.Append ( 'User Database =');
SQLConnection1.Params.Append ( 'User_Name = MySQL');
SQLConnection1.Params.Append ( 'MySQL Password =');
SQLConnection1.Params.Append ( 'the HostName = localhost');
SQLConnection1.Open;
IF to SQLConnection1.

the else
Label1.Caption: = 'Fail';
the parameters set after configuration database connection, a database connection is opened, and to show whether the connection is successful.
Event Query button:
var
I, J: Integer;
the begin
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add ( 'the FROM UserInfo the SELECT *');
SQLQuery1.Active: = to true;
I: = 0;
SQLQuery1.First;
the while SQLQuery1.eof do Not
the begin
for J: = 0. 1 to SQLQuery1.FieldCount-do
StringGrid1.cells [J, I]: = SQLQuery1.Fields [J] .AsString;
SQLQuery1.next;
inc is (I);
End;
SQLQuery1. Active: = false;
lookup table data and outputs the StringGrid.
Event Disconnect button:
IF = SQLConnection1.Connected to true the then
SQLConnection1.Close;
FormDestroy events:
SQLConnection1.Connected to true the then = IF
SQLConnection1.Close;
SQLConnection1.Free;
run the program, click on the connect button, you can see if the database connection is successful success tips, and then click the query button to query the data in the table.

Delphi control of Query

Done database programming with Delphi friend Query certainly familiar with the controls, this control is the function to execute an SQL statement or a SQL script, very high frequency in our database used in the development. It found that in the years of the course there are two points to make good use of this control is very attention. 

    The first point is: Open method to distinguish between good and ExecSQL method Query control. Both methods can be implemented to execute SQL statements, but are to be used depending on the circumstances. If this SQL statement will return a result set, you must use the Open method, if does not return a result set, you would use ExecSQL method. For example: 

    ...... 

    the Query1: Tquery, 

    Query2: Tquery, 

    ...... 

    Query1.Close; 

    Query1.SQL.Clear; 

    Query1.SQL.Add ( ' SELECT * from AA ' ); 

    Query1.Open; 

    ...... 

    Query2.Close; 

    Query2.SQL. the Clear; 

    Query2.SQL.Add ( ' Delete AA ' ); 

    Query2.ExecSQL; 

    ......

    In the above example, the Query1 executed SQL statement returns a result set, it is necessary to use the Open method; and Query2 performed is to delete a record table statement does not return a result set, so a ExecSQL method. 

    The second point is: If the Open Query controls execution method SQL statements, SQL statements to access and is used in one or a few frequently used tables, after executing SQL statements, SQL certain FetchAll method to invoke, can greatly to reduce the probability of occurrence of deadlock. For example: 

    ...... 

    the Query1: Tquery, 

    ...... 

    Query1.Close; 

    Query1.SQL.Clear; 

    Query1.SQL.Add ( ' SELECT * from AA ' ); 

    Query1.Open; 

    Query1.FetchAll; 

    ...... 

In the above example, if the AA is a table that is frequently accessed, while executing a select statement on this table, if you happen to have someone else perform this table delete or update operation, it is possible deadlock. Query1.FetchAll this statement is the function of the release of AA plus lock on the table, the probability of occurrence of this deadlock can be greatly reduced. Avoid deadlock, the future of our large-scale database development is particularly important. "
View Code

mysql detailed steps connection

You see what components are used, and now mysql database connection using Delphi, Delphi is the most simple mysql connection of a good use of tripartite control box ,, Delphi and Delphi download on many sites have downloaded 

you meet with me the problem is, I only solve this problem today 
with ADOconnection, but ADO does not drive the mysql, you're going to download a mysql.dll, placed in the bin directory, Connection direct use   String 

TADOConnection of the ConnectionString = ' dRIVER = {MySQL ODBC 3.51 
Driver}; sERVER = the MySQL database server; dATABASE = name of the database; uSER = user 
name; pASSWORD = password; the OPTION = . 3 ; ' the DRIVER = {the MySQL the ODBC 3.51 
Driver}; sERVER = 192.168 . 1.22 ; dATABASE = rule; uSER = WJH; PASSWORD = 123456 ; OPTION = 3 ; 

you can connect to, and so I was not even on!
View Code

delphi libmysql.dll connection using MySQL

LIBMYSQL.DLL------------You can also access a MySQL server using the libmysql.dll library. Thisfile doesn't come with the standard MySQL installation, but you candownload it from: http://www.fichtner.net/delphi/mysql.delphi.phtmlYou should also download the mysql.pas file which is a Delphi a unitthat contains the type declarations for using this library. You need toinclude this unit in the uses clause of any unit from which you want tocall the functions contained in the library.The following example opens a connection to MySql server, opens adatabase, performs a query and stores the result in a StringGrid, andfinally closes the connection. uses ..., mysql; procedure TForm1.Button1Click(Sender: TObject); var mysqlcon: TMySQL; // MySQL-connection structure presults: pmysql_res; // Pointer to a results structure prow: pmysql_row; // Pointer to a row structure pfields: PMYSQL_FIELDS; // Pointer to a fields array i, j: Integer; // Counters begin // Connect to the server mysql_connect(@mysqlcon, 'localhost', 'root', ''); if mysqlcon.net.last_errno <> 0 then begin ShowMessage (Trim(mysqlcon.net.last_error)); exit; end; // Open the mysql database if mysql_select_db(@mysqlcon, 'mysql') <> 0 then begin mysql_close(@mysqlcon); // Disconnect ShowMessage('Couldn''t open mysql database'); exit; end; presults:= nil; try // Send the query to the server and get the results mysql_query(@mysqlcon, 'SELECT * FROM user'); presults := mysql_store_result(@mysqlcon); // Set the size of the grid StringGrid1.ColCount := presults^.field_count; StringGrid1.RowCount := presults^.row_count + 1; // Fill the grid header with the fields' names pfields := presults^.fields; for j := 0 to presults^.field_count -1 do StringGrid1.Cells[j, 0] := pfields^[j].name; // Fill the grid for i := 1 to presults^.row_count do begin prow := mysql_fetch_row(presults); for j := 0 to presults^.field_count -1 do StringGrid1.Cells[j, i]:= prow^[j]; end; finally mysql_free_result(presults); // Release memory mysql_close(@mysqlcon); // Disconnect end; end;As you can see, it's quite complicated, and this constitutes the main(and I would say only, but very important) disadvantage of using thelibmysql.dll library. The advantages are that you have full control,that it's faster than using ODBC, and that it doesn't requiere the BDE.It would be nice to have a TDataset descendant that encapsulates thelibmysql.dll API...
LIBMYSQL.DLL -----------您也可以访问MySQL服务器使用的libmysql.dll库。 Thisfile不标准的MySQL安装,但你candownload从http://www.fichtner.net/delphi/mysql.delphi.phtmlYou mysql.pas should also download the file, which is the type declaration contains a use of this library a unitthat of Delphi. You need toinclude, Terms of Use of this unit, function library contains any unit which tocall from. In the following example, open a connection to a MySQL server, open adatabase, perform a query result StringGrid and stored, andfinally close the connection. Use, mysql program TForm1.Button1Click (Sender TObject a) VAR mysqlcon: TMySQL a / / the MySQL connection structure presults: pmysql_res; / / pointer performance bow structure: pmysql_row pointer / / row structure if mysqlcon.net the .last_errno, J: integer pfields: PMYSQL_FIELDS; / / pointer field of the array the I / / counter start / / connect to the server mYSQL_CONNECT (@ mysqlcon, 'localhost' a 'root', ''); <> 0 then begin 'Fu Hui Fou sharp fall Ni Qian scallop along lower edge of flag ぇ Bang Yi Ai Huan Yi Lu Xiang of Ji (trim (mysqlcon.net.last_error)); exit; end; / / open the mysql database mysql_select_db (@ mysqlcon,' mysql's') < > 0, then mysql_close (@ mysqlcon); / / begin not disclosed mysql database 'off' Fu Hui Fou sharp fall Ni img scallop along lower edge of flag ぇ Huan Hamamatsu Ai Xi Yi Jie Lujuan of () exit; end; presults: = zero ; results / / query is sent to the server, and the resulting request mysql_query (@ mysqlcon, "SELECT * FROM user); presults = mysql_store_result (@ mysqlcon); / / Set the size of the grid StringGrid1.ColCount: = presults ^. field_count of StringGrid1.RowCount: = presults ^. ROW_COUNT + 1 / / name pfields filled mesh header field: For j = presults ^ art;: = 0 to presults ^ -1. field_count to do StringGrid1.Cells [J, 0]: = pfields ^ [J]. Name; / / i fill grid: = 1 presults of ^. Start ROW_COUNT bow: = mysql_fetch_row of (presults) in J: = 0 to presults ^ -1. field_count do StringGrid1.Cells, [J]: = bow ^ [J]; end; finally the mysql_free_result (presults); / / release memory is mysql_close (@ mysqlcon) end / / end off, as you can see that it is quite complicated, which constitute the main (I would say, but very important) disadvantage of using thelibmysql.dll library. The advantage is full control of its speed than ODBC ,, it would not requiere the BDE.It TDataSet a good package of a successor thelibmysql.dll API, ... / / I fill grid: = 1 presults of ^. Start ROW_COUNT bow: = mysql_fetch_row of (presults) in J: = 0 to presults ^ -1. field_count do StringGrid1.Cells, [J]: = bow ^ [J]; end; finally the mysql_free_result (presults); / / release memory is mysql_close (@ mysqlcon) end / / end off, as you can see that it is quite complicated, which constitute the main (I would say, but very important) disadvantage of using thelibmysql.dll library. The advantage is full control of its speed than ODBC ,, it would not requiere the BDE.It TDataSet a good package of a successor thelibmysql.dll API, ... / / I fill grid: = 1 presults of ^. Start ROW_COUNT bow: = mysql_fetch_row of (presults) in J: = 0 to presults ^ -1. field_count do StringGrid1.Cells, [J]: = bow ^ [J]; end; finally the mysql_free_result (presults); / / release memory is mysql_close (@ mysqlcon) end / / end off, as you can see that it is quite complicated, which constitute the main (I would say, but very important) disadvantage of using thelibmysql.dll library. The advantage is full control of its speed than ODBC ,, it would not requiere the BDE.It TDataSet a good package of a successor thelibmysql.dll API, ...
View Code

In delphi design data connection mysql

How to design data in delphi mysql connection
 2008 - 07 - 21  10 : 49 using delphi mysql connection to make a second-grade service management system should be a small model of the ideal data management application,
 1 using delphi client. the user interface than using browser-interface enhances ease of use and more clients, more comprehensive,
 2 using mysql server-side software can save money (to not use pirated software is concerned), the function is more powerful, is now popular 

database management software, many people will use. 

Because delphi mysql connection itself can only provide support MySQL3. 23 the following versions of mysql, it is now widely used for 5. 0 to 

version can not provide support, will be in use into the "Unable to the Load libmysql" error prompt. 

For solving this problem in several ways, now I use a delphi replace itself comes with dbxopenmysql50.dll 

dbexpmysql.dll. 

Replacement of two ways 
1 . Direct download dbxopenmysql50.dll renamed dbexpmysql.dll saved to C: \ Program Files \ Borland \ Delphi7

\ Bin, the original file to be replaced, and then have to modify GetDriverFunc connction under changed in programming 

getSQLDriverMYSQL50. 


2 The design dbxopenmysql50.dll copied to the user directory, then changed in the program design will libraryname Connection 

dbxopenmysql50.dll, and GetDriverFunc to getSQLDriverMYSQL50. 

Sample programs and source code: 
first add in the program controls simpledataset1, DataSource1, DBGrid1 
Edit1 enter the host name 
edit2 input port (in addition to this function is invalid, because they can not make changes to the port) 
Edit3 database name 
edit4 connection mysql database user name 
edit5 connection password mysql database 
edit6 table 

set caption button1 to exit 
set button2 the caption for the connection 
settings dataset datasource1 for simpledataset1 
set dbgrid1 the datasource is datasource1 

double-click button2 enter the following code connected 
with simpledataset1.Connection do 
      the begin 
      DriverName: ='dbxmysql';
      GetDriverFunc := 'getSQLDriverMYSQL50';
      LibraryName := 'dbxopenmysql50.dll';
      VendorLib := 'libmysql.dll';
      Params.Append('HostName=localhost');

//     Params.Append('HostName='+edit1.Text+':'+edit2.Text);
// Params.Append('port='+edit2.Text );
      Params.Append('Database='+edit3.Text );
      Params.Append('= User_Name ' + edit4.Text); 
      Params.Append ( ' Password = ' + edit5.Text); 

      End ; 
     simpledataset1.DataSet.CommandText: = ' SELECT * from ' + ; edit6.Text 
     simpledataset1.Open; 
double-click input exit button1 the following code 
simpledataset1.close; 
use Close; 

program code after the completion of the total inventory is: 

Unit Unit1; 

interface 

uses 
Windows, the Messages, the SysUtils, Variants, the Classes, Graphics, Controls, Forms, 
the Dialogs, DBXpress, FMTBcd, DB, Grids, DBGrids, SqlExpr, DBClient, 
SimpleDS, StdCtrls; 

of the type 
TForm1= class(TForm)
    DBGrid1: TDBGrid;
    SimpleDataSet1: TSimpleDataSet;
    DataSource1: TDataSource;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Button2: TButton;
    Label5: TLabel;
    Edit5: TEdit;
    Edit6: TEdit;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;
Connection: TSQLConnection;
implementation

{$R *.dfm}


procedure TForm1.Button2Click(Sender: TObject);
begin
     with simpledataset1.Connection do
      begin
      DriverName := 'dbxmysql';
      GetDriverFunc := 'getSQLDriverMYSQL50';
      LibraryName := 'dbxopenmysql50.dll';
      VendorLib := 'libmysql.dll';
      Params.Append('HostName=localhost');

//     Params.Append('HostName='+edit1.Text+':'+edit2.Text);
// Params.Append('port='+edit2.Text );
      Params.Append('Database='+edit3.Text );
      Params.Append('User_Name='+edit4.Text );
      Params.Append('Password='+edit5.Text );

      end;
     simpledataset1.DataSet.CommandText :='select * from '+edit6.Text ;
     simpledataset1.Open;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
simpledataset1.close;
close;

end;
procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.

转载请注作者:nathen.zhang     Email:[email protected]
 
View Code

 

Access mysql, using libmysql.dll; mysql.pas where to download

http://www.audio-data.de/mysql.html


Deutsch
mysql.pas (Version 2009-09-13)
Client API for MySQL AB`s SQL Database Server using LibMySql.dll or LibMySqld.dll (Embeeded MYSQL Server). It is a Pascal translation of mysql.h and some other C header files needed for writing clients for the MySQL database server.
The unit is tested with:
Delphi Versions: (3), (4), 5, 6, 2007, 2009, 2010.
LibMySql.dll Versions: 3.23, 4.0, 4.1, 5.0, 5.1, 6.0
MySQL-Server Versions: 3.23, 4.0, 4.1, 5.0, 5.1

Download mysql.pas

The contents of the file mysql.pas are used with permission, subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html
Other components: SimpleXML Change log 2009-03-27 (so) Page created 2009-03-29 (so) Demo application 2009-04-13 (so) Improved dynamic loading 2009-04-26 (so) Demo Delphi 2009 Added functions: mysql_autocommit, mysql_set_character_set, 
mysql_commit, mysql_rollback, mysql_set_server_option,
mysql_sqlstate, mysql_warning_count, MySql_StrLen,
CharSetNameToCodepage, CodepageToCharSetName, MySqlToUTF16,
UTF16ToMySql 2009-06-08 (so) Added functions: mysql_server_init, mysql_server_end,
Support for prepared statements 2009-06-09 (so) Added records: TNET501, TMYSQL501 2009-06-10 (so) Added functions mysql_thread_init, mysql_thread_end 2009-06-24 (so) Added functions mysql_more_results, mysql_next_result, FormatIdentifier 2009-07-04 (so) Added functions EscapeString, EscapeForLike, QuoteString, FullFieldname
Change FormatIdentifier to QuoteName 2009-08-04 (so) Bug in GetVersion fixed 2009-09-13 (so) ThreadDemo added
















LIBMYSQL.DLL ------------ You can also access a MySQL server using the libmysql.dll library. This file doesn't come with the standard MySQL installation, but you can download it from: http://www.dlldll.com/libmysql.dll_download.html You should also download the mysql.pas file which is a Delphi a unit that contains the type declarations for using this library. You need to include this unit in the uses clause of any unit from which you want to call the functions contained in the library. The following example opens a connection to MySql server, opens a database, performs a query and stores the result in a StringGrid, and finally closes the connection. uses ..., mysql; procedure TForm1.Button1Click(Sender: TObject); var mysqlcon: TMySQL; // MySQL-connection structure presults: pmysql_res; // Pointer to a results structure prow: pmysql_row; // Pointer to a row structure pfields: PMYSQL_FIELDS; // Pointer to a fields array i, j: Integer; // Counters begin // Connect to the server mysql_connect(@mysqlcon, 'localhost', 'root', ''); if mysqlcon.net.last_errno <> 0 then begin ShowMessage (Trim(mysqlcon.net.last_error)); exit; end; // Open the mysql database if mysql_select_db(@mysqlcon, 'mysql') <> 0 then begin mysql_close(@mysqlcon); // Disconnect ShowMessage('Couldn''t open mysql database'); exit; end; presults:= nil; try // Send the query to the server and get the results mysql_query(@mysqlcon, 'SELECT * FROM user'); presults := mysql_store_result(@mysqlcon); // Set the size of the grid StringGrid1.ColCount := presults^.field_count; StringGrid1.RowCount := presults^.row_count + 1; // Fill the grid header with the fields' names pfields := presults^.fields; for j := 0 to presults^.field_count -1 do StringGrid1.Cells[j, 0] := pfields^[j].name; // Fill the grid for i := 1 to presults^.row_count do begin prow := mysql_fetch_row(presults); for j := 0 to presults^.field_count -1 do StringGrid1.Cells[j, i]:= prow^[j]; end; finally mysql_free_result(presults); // Release memory mysql_close(@mysqlcon); // Disconnect end; end; As you can see, it's quite complicated, and this constitutes the main (and I would say only, but very important) disadvantage of using the libmysql.dll library. The advantages are that you have full control, that it's faster than using ODBC, and that it doesn't requiere the BDE. It would be nice to have a TDataset descendant that encapsulates the libmysql.dll API...
View Code

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11345545.html