Delphi连接MySql待测试验证

要在一个Delphi程序中调用MysqL数据库,查到有个资料如下,待验证,验证后会给出结果。暂时做个标记

 


用libMysqL.dll(来自于MysqL安装后的文件
MysqL.pas

以下是MysqL.pas

分享图片

// -----------------------------------------------------------------------------------------------
//
//                    MysqL Client API for Borland Delphi (version 4 and above)
//
//                           Pascal Interface Unit for libMysqL.dll,the
//                        Client Library for MysqL AB‘s sql Database Server
//
//                  This is a literal translation of relevant parts of MysqL AB‘s
//                    C header files,MysqL.h,MysqL_com.h,and MysqL_version.h
//
//                            copyright (c) 1999-2002 Matthias Fichtner
//                           (see license.txt for licensing @R_544_4045@ion)
//
// -----------------------------------------------------------------------------------------------
//                       See MysqL.h for MysqL AB‘s copyright and GPL notice
// -----------------------------------------------------------------------------------------------
//
//       17-Aug-1999  mf  Translated MysqL.h                             MysqL 3.22.24
//       19-Aug-1999  mf  Corrected some type deFinitions                MysqL 3.22.24
//       20-Aug-1999  mf  Finished debugging the unit                    MysqL 3.22.24
//       18-Sep-1999  mf  Code maintenance for release 3.22.26a          MysqL 3.22.26a
//       22-Oct-1999  mf  Code maintenance for release 3.22.28           MysqL 3.22.28
//       02-Jan-2000  mf  Code maintenance for release 3.22.29           MysqL 3.22.29
//       21-Jan-2000  mf  Code maintenance for release 3.22.30           MysqL 3.22.30
//       07-Feb-2000  mf  Code maintenance for release 3.22.31           MysqL 3.22.31
//       16-Feb-2000  mf  Code maintenance for release 3.22.32           MysqL 3.22.32
//       13-Aug-2000  mf  Code maintenance for release 3.22.34           MysqL 3.22.34
//       14-Aug-2000  mf  Reworked entire unit for first 3.23 release    MysqL 3.23.19-beta
//       14-Aug-2000  mf  Added MysqL_character_set_name()               MysqL 3.23.22-beta
//       11-Sep-2000  mf  Added IS_NUM_FIELD and INTERNAL_NUM_FIELD      MysqL 3.23.24-beta
//       08-Oct-2000  mf  Modified TMEM_ROOT,enum_server_command,MysqL 3.23.25-beta
//                        and INTERNAL_NUM_FIELD
//       01-Nov-2000  mf  Code maintenance for release 3.23.27           MysqL 3.23.27-beta
//       25-Nov-2000  mf  Code maintenance for release 3.23.28           MysqL 3.23.28-gamma
//       05-Jan-2001  mf  Code maintenance for release 3.23.30           MysqL 3.23.30-gamma
//       19-Jan-2001  mf  Code maintenance for release 3.23.31           MysqL 3.23.31
//       11-Mar-2001  mf  Added functions MysqL_real_send_query(),MysqL 3.23.33
//                        MysqL_send_query(),and MysqL_reap_query()
//       28-Mai-2001  mf  Modified MysqL_send_query(),removed           MysqL 3.23.38
//                        MysqL_real_send_query(),MysqL_reap_query(),
//                        added MysqL_read_query_result(),and fixed
//                        CLIENT_TRANSACTIONS
//       07-Aug-2001  mf  Code maintenance for release 3.23.40           MysqL 3.23.40
//       23-Sep-2001  mf  Code maintenance for release 3.23.42           MysqL 3.23.42
//       29-Jan-2002  mf  Added libMysqL_load(),libMysqL_free(),MysqL 3.23.47
//                        libMysqL_status and LIBMysqL_ constants
//                        for dynamic loading of libMysqL.dll
//       11-Mar-2002  mf  Added MysqL_OPT_LOCAL_INFILE to MysqL_option   MysqL 3.23.49
//
// -----------------------------------------------------------------------------------------------
//
//                   Latest releases of MysqL.pas are made available through the
//                   distribution site at: http://www.fichtner.net/delphi/MysqL/
//
//                  Please send questions,bug reports,and suggestions regarding
//                  MysqL.pas to Matthias Fichtner <[email protected]>
//
//                      See readme.txt for an introduction and documentation.
//                    See license.txt for licensing @R_544_4045@ion and disclaimer.
//
// -----------------------------------------------------------------------------------------------
//                     This unit is provided "as is". Use it at your own risk.
// -----------------------------------------------------------------------------------------------

unit MysqL;

// -----------------------------------------------------------------------------------------------
INTERFACE
// -----------------------------------------------------------------------------------------------

uses
  Windows,// Needed for some type deFinitions
  Winsock;  // Needed for some type deFinitions

// ----------------
// From MysqL.h ...
// ----------------

type
  my_bool = byte;
  gptr = pChar;

type
  PUSED_MEM = ^TUSED_MEM;  // struct for once_alloc
  TUSED_MEM = record
    next: PUSED_MEM;       // Next block in use
    left: longword;        // memory left in block
    size: longword;        // size of block
  end;

type
  error_proc = procedure;

type
  PMEM_ROOT = ^TMEM_ROOT;
  TMEM_ROOT = record
    free: PUSED_MEM;
    used: PUSED_MEM;
    pre_alloc: PUSED_MEM;
    min_malloc: longword;
    block_size: longword;
    error_handler: error_proc;
  end;

type
  my_socket = TSocket;

// --------------------
// From MysqL_com.h ...
// --------------------

const
  NAME_LEN = 64;               // Field/table name length
  HOSTNAME_LENGTH = 60;
  USERNAME_LENGTH = 16;
  SERVER_VERSION_LENGTH = 60;

  LOCAL_HOST = localhost;
  LOCAL_HOST_NAMEDPIPE = .;

  MysqL_NAMEDPIPE = MysqL;
  MysqL_SERVICENAME = MysqL;

type
  enum_server_command = (
    COM_SLEEP,COM_QUIT,COM_INIT_DB,COM_QUERY,COM_FIELD_LIST,COM_CREATE_DB,COM_DROP_DB,COM_REFRESH,COM_SHUTDOWN,COM_STATISTICS,COM_PROCESS_INFO,COM_CONNECT,COM_PROCESS_KILL,COM_DEBUG,COM_PING,COM_TIME,COM_DELAYED_INSERT,COM_CHANGE_USER,COM_binlog_DUMP,COM_TABLE_DUMP,COM_CONNECT_OUT
  );

const
  NOT_NULL_FLAG = 1;      // Field can‘t be NULL
  PRI_KEY_FLAG = 2;       // Field is part of a primary key
  UNIQUE_KEY_FLAG = 4;    // Field is part of a unique key
  MULTIPLE_KEY_FLAG = 8;  // Field is part of a key
  BLOB_FLAG = 16;         // Field is a blob
  UNSIGNED_FLAG = 32;     // Field is unsigned
  ZEROFILL_FLAG = 64;     // Field is zerofill
  BINARY_FLAG = 128;

  // The following are only sent to new clients

  ENUM_FLAG = 256;            // field is an enum
  AUTO_INCREMENT_FLAG = 512;  // field is a autoincrement field
  TIMESTAMP_FLAG = 1024;      // Field is a timestamp
  SET_FLAG = 2048;            // field is a set
  NUM_FLAG = 32768;           // Field is num (for clients)
  PART_KEY_FLAG = 16384;      // Intern; Part of some key
  GROUP_FLAG = 32768;         // Intern: Group field
  UNIQUE_FLAG = 65536;        // Intern: Used by sql_yacc

  REFRESH_GRANT = 1;     // Refresh grant tables
  REFRESH_LOG = 2;       // Start on new log file
  REFRESH_TABLES = 4;    // close all tables
  REFRESH_HOSTS = 8;     // Flush host cache
  REFRESH_STATUS = 16;   // Flush status variables
  REFRESH_THREADS = 32;  // Flush status variables
  REFRESH_SLAVE = 64;    // Reset master info and restart slave
                         // thread
  REFRESH_MASTER = 128;  // Remove all bin logs in the index
                         // and truncate the index

  // The following can‘t be set with MysqL_refresh()

  REFRESH_READ_LOCK = 16384;  // Lock tables for read
  REFRESH_FAST = 32768;       // Intern flag

  CLIENT_LONG_PASSWORD = 1;      // new more secure passwords
  CLIENT_FOUND_ROWS = 2;         // Found instead of affected rows
  CLIENT_LONG_FLAG = 4;          // Get all column flags
  CLIENT_CONNECT_WITH_DB = 8;    // One can specify db on connect
  CLIENT_NO_SCHEMA = 16;         // Don‘t allow database.table.column
  CLIENT_COMPRESS = 32;          // Can use compression protcol
  CLIENT_ODBC = 64;              // Odbc client
  CLIENT_LOCAL_FILES = 128;      // Can use LOAD DATA LOCAL
  CLIENT_IGnorE_SPACE = 256;     // Ignore spaces before ‘(‘
  CLIENT_INteraCTIVE = 1024;     // This is an interactive client
  CLIENT_SSL = 2048;             // Switch to SSL after handshake
  CLIENT_IGnorE_SIGPIPE = 4096;  // IGnorE sigpipes
  CLIENT_TRANSACTIONS = 8192;    // Client kNows about transactions

  SERVER_STATUS_IN_TRANS = 1;    // Transaction has started
  SERVER_STATUS_AUTOCOMMIT = 2;  // Server in auto_commit mode

  MysqL_ERRMSG_SIZE = 200;
  NET_READ_TIMEOUT = 30;       // Timeout on read
  NET_WRITE_TIMEOUT = 60;      // Timeout on write
  NET_WAIT_TIMEOUT = 8*60*60;  // Wait for new query

type
  PVio = ^TVio;
  TVio = record
  end;

type
  PNET = ^TNET;
  TNET = record
    vio: PVio;
    fd: my_socket;
    fcntl: longint;
    buff,buff_end,write_pos,read_pos: pByte;
    last_error: array [0..MysqL_ERRMSG_SIZE - 1] of char;
    last_errno,max_packet,timeout,pkt_nr: longword;
    error: byte;
    return_errno,compress: my_bool;
    no_send_ok: my_bool;  // needed if we are doing several
      // queries in one command ( as in LOAD TABLE ... FROM MASTER ),
      // and do not want to confuse the client with OK at the wrong time
    remain_in_buf,length,buf_length,where_b: longword;
    return_status: pLongword;
    reading_or_writing: byte;
    save_char: char;
  end;

const
  packet_error: longword = $ffffffff;

const
  FIELD_TYPE_DECIMAL = 0;
  FIELD_TYPE_TINY = 1;
  FIELD_TYPE_SHORT = 2;
  FIELD_TYPE_LONG = 3;
  FIELD_TYPE_FLOAT = 4;
  FIELD_TYPE_DOUBLE = 5;
  FIELD_TYPE_NULL = 6;
  FIELD_TYPE_TIMESTAMP = 7;
  FIELD_TYPE_LONGLONG = 8;
  FIELD_TYPE_INT24 = 9;
  FIELD_TYPE_DATE = 10;
  FIELD_TYPE_TIME = 11;
  FIELD_TYPE_DATETIME = 12;
  FIELD_TYPE_YEAR = 13;
  FIELD_TYPE_NEWDATE = 14;
  FIELD_TYPE_ENUM = 247;
  FIELD_TYPE_SET = 248;
  FIELD_TYPE_TINY_BLOB = 249;
  FIELD_TYPE_MEDIUM_BLOB = 250;
  FIELD_TYPE_LONG_BLOB = 251;
  FIELD_TYPE_BLOB = 252;
  FIELD_TYPE_VAR_STRING = 253;
  FIELD_TYPE_STRING = 254;

const
  FIELD_TYPE_CHAR = FIELD_TYPE_TINY;      // For compability
  FIELD_TYPE_INTERVAL = FIELD_TYPE_ENUM;  // For compability

type
  enum_field_types = FIELD_TYPE_DECIMAL..FIELD_TYPE_STRING;

// ------------------------
// From MysqL_version.h ...
// ------------------------

const
  PROTOCOL_VERSION = 10;
  MysqL_SERVER_VERSION = 3.23.49;
  MysqL_SERVER_SUFFIX = ‘‘;
  FRM_VER = 6;
  MysqL_VERSION_ID = 32349;
  MysqL_PORT = 3306;
  MysqL_UNIX_ADDR = /tmp/MysqL.sock;

// ----------------
// From MysqL.h ...
// ----------------

function IS_PRI_KEY(n: longword): boolean;
function IS_NOT_NULL(n: longword): boolean;
function IS_BLOB(n: longword): boolean;
function IS_NUM(t: longword): boolean;

type
  PMysqL_FIELD = ^TMysqL_FIELD;
  TMysqL_FIELD = record
    name: pChar;              // Name of column
    table: pChar;             // Table of column if column was a field
    def: pChar;               // Default value (set by MysqL_list_fields)
    _type: enum_field_types;  // Type of field. Se MysqL_com.h for types
    length: longword;         // Width of column
    max_length: longword;     // Max width of selected set
    flags: longword;          // Div flags
    decimals: longword;       // Number of decimals in field
  end;

function IS_NUM_FIELD(f: PMysqL_FIELD): boolean;
function INTERNAL_NUM_FIELD(f: PMysqL_FIELD): boolean;

type
  PMysqL_ROW = ^TMysqL_ROW;  // return data as array of strings
  TMysqL_ROW = array[0..MaxInt div SizeOf(pChar) - 1] of pChar;

type
  MysqL_FIELD_OFFSET = longword;  // offset to current field

type
  my_ulonglong = int64;

const
  MysqL_COUNT_ERROR: my_ulonglong = not 0;

type
  PMysqL_ROWS = ^TMysqL_ROWS;
  TMysqL_ROWS = record
    next: PMysqL_ROWS;  // list of rows
    data: PMysqL_ROW;
  end;

type
  MysqL_ROW_OFFSET = PMysqL_ROWS;  // offset to current row

type
  PMysqL_DATA = ^TMysqL_DATA;
  TMysqL_DATA = record
    rows: my_ulonglong;
    fields: longword;
    data: PMysqL_ROWS;
    alloc: TMEM_ROOT;
  end;

type
  PMysqL_OPTIONS = ^TMysqL_OPTIONS;
  TMysqL_OPTIONS = record
    connect_timeout,client_flag: longword;
    compress,named_pipe: my_bool;
    port: longword;
    host,init_command,user,password,unix_socket,db: pChar;
    my_cnf_file,my_cnf_group,charset_dir,charset_name: pChar;
    use_ssl: my_bool;   // if to use SSL or not
    ssl_key: pChar;     // PEM key file
    ssl_cert: pChar;    // PEM cert file
    ssl_ca: pChar;      // PEM CA file
    ssl_capath: pChar;  // PEM directory of CA-s?
  end;

type
  MysqL_option = (
    MysqL_OPT_CONNECT_TIMEOUT,MysqL_OPT_COMPRESS,MysqL_OPT_NAMED_PIPE,MysqL_INIT_COMMAND,MysqL_READ_DEFAULT_FILE,MysqL_READ_DEFAULT_GROUP,MysqL_SET_CHARSET_DIR,MysqL_SET_CHARSET_NAME,MysqL_OPT_LOCAL_INFILE
  );

type
  MysqL_status = (
    MysqL_STATUS_READY,MysqL_STATUS_GET_RESULT,MysqL_STATUS_USE_RESULT
  );

type
  PMysqL_FIELDS = ^TMysqL_FIELDS;
  TMysqL_FIELDS = array[0..MaxInt div SizeOf(TMysqL_FIELD) - 1] of TMysqL_FIELD;

type
  PCHARSET_INFO = ^TCHARSET_INFO;
  TCHARSET_INFO = record
    // Omitted: Structure not necessarily needed.
    // DeFinition of struct charset_info_st can be
    // found in include/m_ctype.h
  end;

type
  PMysqL = ^TMysqL;
  TMysqL = record
    net: TNET;                    // Communication parameters
    connector_fd: gptr;           // ConnectorFd for SSL
    host,passwd,server_version,host_info,info,db: pChar;
    port,client_flag,server_capabilities: longword;
    protocol_version: longword;
    field_count: longword;
    server_status: longword;
    thread_id: longword;          // Id for connection in server
    affected_rows: my_ulonglong;
    insert_id: my_ulonglong;      // id if insert on table with NEXTNR
    extra_info: my_ulonglong;     // Used by MysqLshow
    packet_length: longword;
    status: MysqL_status;
    fields: PMysqL_FIELDS;
    field_alloc: TMEM_ROOT;
    free_me: my_bool;             // If free in MysqL_close
    reconnect: my_bool;           // set to 1 if automatic reconnect
    options: TMysqL_OPTIONS;
    scramble_buff: array [0..8] of char;
    charset: PCHARSET_INFO;
    server_language: longword;
  end;

type
  PMysqL_RES = ^TMysqL_RES;
  TMysqL_RES = record
    row_count: my_ulonglong;
    field_count,current_field: longword;
    fields: PMysqL_FIELDS;
    data: PMysqL_DATA;
    data_cursor: PMysqL_ROWS;
    field_alloc: TMEM_ROOT;
    row: PMysqL_ROW;          // If unbuffered read
    current_row: PMysqL_ROW;  // buffer to current row
    lengths: pLongword;       // column lengths of current row
    handle: PMysqL;           // for unbuffered reads
    eof: my_bool;             // Used my MysqL_fetch_row
  end;

// Functions to get @R_544_4045@ion from the MysqL and MysqL_RES structures
// Should definitely be used if one uses shared libraries

var
  MysqL_num_rows: function(res: PMysqL_RES): my_ulonglong; stdcall;
  MysqL_num_fields: function(res: PMysqL_RES): longword; stdcall;
  MysqL_eof: function(res: PMysqL_RES): my_bool; stdcall;
  MysqL_fetch_field_direct: function(res: PMysqL_RES; fieldnr: longword): PMysqL_FIELD; stdcall;
  MysqL_fetch_fields: function(res: PMysqL_RES): PMysqL_FIELDS; stdcall;
  MysqL_row_tell: function(res: PMysqL_RES): PMysqL_ROWS; stdcall;
  MysqL_field_tell: function(res: PMysqL_RES): longword; stdcall;

var
  MysqL_field_count: function(_MysqL: PMysqL): longword; stdcall;
  MysqL_affected_rows: function(_MysqL: PMysqL): my_ulonglong; stdcall;
  MysqL_insert_id: function(_MysqL: PMysqL): my_ulonglong; stdcall;
  MysqL_errno: function(_MysqL: PMysqL): longword; stdcall;
  MysqL_error: function(_MysqL: PMysqL): pChar; stdcall;
  MysqL_info: function(_MysqL: PMysqL): pChar; stdcall;
  MysqL_thread_id: function(_MysqL: PMysqL): longword; stdcall;
  MysqL_character_set_name: function(_MysqL: PMysqL): pChar; stdcall;

type
  PMysqL_LENGTHS = ^TMysqL_LENGTHS;
  TMysqL_LENGTHS = array[0..MaxInt div SizeOf(longword) - 1] of longword;

type
  extend_buffer_func = function(void: pointer; _to: pChar; length: pLongword): pChar;

var
  MysqL_init: function(_MysqL: PMysqL): PMysqL; stdcall;
  {$IFDEF HAVE_OPENSSL}
  MysqL_ssl_set: function(_MysqL: PMysqL; const key,cert,ca,capath: pChar): longint; stdcall;
  MysqL_ssl_cipher: function(_MysqL: PMysqL): pChar; stdcall;
  MysqL_ssl_clear: function(_MysqL: PMysqL): longint; stdcall;
  {$ENDIF} // HAVE_OPENSSL
  MysqL_connect: function(_MysqL: PMysqL; const host,passwd: pChar): PMysqL; stdcall;
  MysqL_change_user: function(_MysqL: PMysqL; const user,db: pChar): my_bool; stdcall;
  MysqL_real_connect: function(_MysqL: PMysqL; const host,db: pChar; port: longword; const unix_socket: pChar; clientflag: longword): PMysqL; stdcall;
  MysqL_close: procedure(sock: PMysqL); stdcall;
  MysqL_select_db: function(_MysqL: PMysqL; const db: pChar): longint; stdcall;
  MysqL_query: function(_MysqL: PMysqL; const q: pChar): longint; stdcall;
  MysqL_send_query: function(_MysqL: PMysqL; const q: pChar; length: longword): longint; stdcall;
  MysqL_read_query_result: function(_MysqL: PMysqL): longint; stdcall;
  MysqL_real_query: function(_MysqL: PMysqL; const q: pChar; length: longword): longint; stdcall;
  MysqL_create_db: function(_MysqL: PMysqL; const DB: pChar): longint; stdcall;
  MysqL_drop_db: function(_MysqL: PMysqL; const DB: pChar): longint; stdcall;
  MysqL_shutdown: function(_MysqL: PMysqL): longint; stdcall;
  MysqL_dump_debug_info: function(_MysqL: PMysqL): longint; stdcall;
  MysqL_refresh: function(_MysqL: PMysqL; refresh_options: longword): longint; stdcall;
  MysqL_kill: function(_MysqL: PMysqL; pid: longword): longint; stdcall;
  MysqL_ping: function(_MysqL: PMysqL): longint; stdcall;
  MysqL_stat: function(_MysqL: PMysqL): pChar; stdcall;
  MysqL_get_server_info: function(_MysqL: PMysqL): pChar; stdcall;
  MysqL_get_client_info: function: pChar; stdcall;
  MysqL_get_host_info: function(_MysqL: PMysqL): pChar; stdcall;
  MysqL_get_proto_info: function(_MysqL: PMysqL): longword; stdcall;
  MysqL_list_dbs: function(_MysqL: PMysqL; const wild: pChar): PMysqL_RES; stdcall;
  MysqL_list_tables: function(_MysqL: PMysqL; const wild: pChar): PMysqL_RES; stdcall;
  MysqL_list_fields: function(_MysqL: PMysqL; const table,wild: pChar): PMysqL_RES; stdcall;
  MysqL_list_processes: function(_MysqL: PMysqL): PMysqL_RES; stdcall;
  MysqL_store_result: function(_MysqL: PMysqL): PMysqL_RES; stdcall;
  MysqL_use_result: function(_MysqL: PMysqL): PMysqL_RES; stdcall;
  MysqL_options: function(_MysqL: PMysqL; option: MysqL_option; const arg: pChar): longint; stdcall;
  MysqL_free_result: procedure(result: PMysqL_RES); stdcall;
  MysqL_data_seek: procedure(result: PMysqL_RES; offset: my_ulonglong); stdcall;
  MysqL_row_seek: function(result: PMysqL_RES; offset: MysqL_ROW_OFFSET): MysqL_ROW_OFFSET; stdcall;
  MysqL_field_seek: function(result: PMysqL_RES; offset: MysqL_FIELD_OFFSET): MysqL_FIELD_OFFSET; stdcall;
  MysqL_fetch_row: function(result: PMysqL_RES): PMysqL_ROW; stdcall;
  MysqL_fetch_lengths: function(result: PMysqL_RES): PMysqL_LENGTHS; stdcall;
  MysqL_fetch_field: function(result: PMysqL_RES): PMysqL_FIELD; stdcall;
  MysqL_escape_string: function(_to: pChar; const from: pChar; from_length: longword): longword; stdcall;
  MysqL_real_escape_string: function(_MysqL: PMysqL; _to: pChar; const from: pChar; length: longword): longword; stdcall;
  MysqL_debug: procedure(const debug: pChar); stdcall;
  MysqL_odbc_escape_string: function(_MysqL: PMysqL; _to: pChar; to_length: longword; const from: pChar; from_length: longword; param: pointer; extend_buffer: extend_buffer_func): pChar; stdcall;
  myodbc_remove_escape: procedure(_MysqL: PMysqL; name: pChar); stdcall;
  MysqL_thread_safe: function: longword; stdcall;

function MysqL_reload(_MysqL: PMysqL): longint;

// Status codes for libMysqL.dll

const
  LIBMysqL_UNDEFINED = 0;     // libMysqL_load() has not yet been called
  LIBMysqL_MISSING = 1;       // No suitable DLL Could be located
  LIBMysqL_INCOMPATIBLE = 2;  // A DLL was found but it is not compatible
  LIBMysqL_READY = 3;         // The DLL was loaded successfully

var
  libMysqL_handle: HMODULE = 0;
  libMysqL_status: byte = LIBMysqL_UNDEFINED;

function libMysqL_load(name: pChar): byte;
procedure libMysqL_free;

// -----------------------------------------------------------------------------------------------
IMPLEMENTATION
// -----------------------------------------------------------------------------------------------

function IS_PRI_KEY(n: longword): boolean;
begin
  Result := (n and PRI_KEY_FLAG) = PRI_KEY_FLAG;
end;

function IS_NOT_NULL(n: longword): boolean;
begin
  Result := (n and NOT_NULL_FLAG) = NOT_NULL_FLAG;
end;

function IS_BLOB(n: longword): boolean;
begin
  Result := (n and BLOB_FLAG) = BLOB_FLAG;
end;

function IS_NUM(t: longword): boolean;
begin
  Result := (t <= FIELD_TYPE_INT24) or (t = FIELD_TYPE_YEAR);
end;

function IS_NUM_FIELD(f: PMysqL_FIELD): boolean;
begin
  Result := (f.flags and NUM_FLAG) = NUM_FLAG;
end;

function INTERNAL_NUM_FIELD(f: PMysqL_FIELD): boolean;
begin
  Result := (((f._type <= FIELD_TYPE_INT24) and ((f._type <> FIELD_TYPE_TIMESTAMP) or (f.length = 14) or (f.length = 8))) or (f._type = FIELD_TYPE_YEAR));
end;

function MysqL_reload(_MysqL: PMysqL): longint;
begin
  Result := MysqL_refresh(_MysqL,REFRESH_GRANT);
end;

function libMysqL_load(name: pChar): byte;

  procedure assign_proc(var proc: FARPROC; name: pChar);
  begin
    proc := GetProcAddress(libMysqL_handle,name);
    if proc = nil then libMysqL_status := LIBMysqL_INCOMPATIBLE;
  end;

begin
  libMysqL_free;
  if name = nil then name := libMysqL.dll;
  libMysqL_handle := LoadLibrary(name);
  if libMysqL_handle = 0 then libMysqL_status := LIBMysqL_MISSING
  else begin
    libMysqL_status := LIBMysqL_READY;
    assign_proc(@MysqL_num_rows,MysqL_num_rows);
    assign_proc(@MysqL_num_fields,MysqL_num_fields);
    assign_proc(@MysqL_eof,MysqL_eof);
    assign_proc(@MysqL_fetch_field_direct,MysqL_fetch_field_direct);
    assign_proc(@MysqL_fetch_fields,MysqL_fetch_fields);
    assign_proc(@MysqL_row_tell,MysqL_row_tell);
    assign_proc(@MysqL_field_tell,MysqL_field_tell);
    assign_proc(@MysqL_field_count,MysqL_field_count);
    assign_proc(@MysqL_affected_rows,MysqL_affected_rows);
    assign_proc(@MysqL_insert_id,MysqL_insert_id);
    assign_proc(@MysqL_errno,MysqL_errno);
    assign_proc(@MysqL_error,MysqL_error);
    assign_proc(@MysqL_info,MysqL_info);
    assign_proc(@MysqL_thread_id,MysqL_thread_id);
    assign_proc(@MysqL_character_set_name,MysqL_character_set_name);
    assign_proc(@MysqL_init,MysqL_init);
    {$IFDEF HAVE_OPENSSL}
    assign_proc(@MysqL_ssl_set,MysqL_ssl_set);
    assign_proc(@MysqL_ssl_cipher,MysqL_ssl_cipher);
    assign_proc(@MysqL_ssl_clear,MysqL_ssl_clear);
    {$ENDIF} // HAVE_OPENSSL
    assign_proc(@MysqL_connect,MysqL_connect);
    assign_proc(@MysqL_change_user,MysqL_change_user);
    assign_proc(@MysqL_real_connect,MysqL_real_connect);
    assign_proc(@MysqL_close,MysqL_close);
    assign_proc(@MysqL_select_db,MysqL_select_db);
    assign_proc(@MysqL_query,MysqL_query);
    assign_proc(@MysqL_send_query,MysqL_send_query);
    assign_proc(@MysqL_read_query_result,MysqL_read_query_result);
    assign_proc(@MysqL_real_query,MysqL_real_query);
    assign_proc(@MysqL_create_db,MysqL_create_db);
    assign_proc(@MysqL_drop_db,MysqL_drop_db);
    assign_proc(@MysqL_shutdown,MysqL_shutdown);
    assign_proc(@MysqL_dump_debug_info,MysqL_dump_debug_info);
    assign_proc(@MysqL_refresh,MysqL_refresh);
    assign_proc(@MysqL_kill,MysqL_kill);
    assign_proc(@MysqL_ping,MysqL_ping);
    assign_proc(@MysqL_stat,MysqL_stat);
    assign_proc(@MysqL_get_server_info,MysqL_get_server_info);
    assign_proc(@MysqL_get_client_info,MysqL_get_client_info);
    assign_proc(@MysqL_get_host_info,MysqL_get_host_info);
    assign_proc(@MysqL_get_proto_info,MysqL_get_proto_info);
    assign_proc(@MysqL_list_dbs,MysqL_list_dbs);
    assign_proc(@MysqL_list_tables,MysqL_list_tables);
    assign_proc(@MysqL_list_fields,MysqL_list_fields);
    assign_proc(@MysqL_list_processes,MysqL_list_processes);
    assign_proc(@MysqL_store_result,MysqL_store_result);
    assign_proc(@MysqL_use_result,MysqL_use_result);
    assign_proc(@MysqL_options,MysqL_options);
    assign_proc(@MysqL_free_result,MysqL_free_result);
    assign_proc(@MysqL_data_seek,MysqL_data_seek);
    assign_proc(@MysqL_row_seek,MysqL_row_seek);
    assign_proc(@MysqL_field_seek,MysqL_field_seek);
    assign_proc(@MysqL_fetch_row,MysqL_fetch_row);
    assign_proc(@MysqL_fetch_lengths,MysqL_fetch_lengths);
    assign_proc(@MysqL_fetch_field,MysqL_fetch_field);
    assign_proc(@MysqL_escape_string,MysqL_escape_string);
    assign_proc(@MysqL_real_escape_string,MysqL_real_escape_string);
    assign_proc(@MysqL_debug,MysqL_debug);
    assign_proc(@MysqL_odbc_escape_string,MysqL_odbc_escape_string);
    assign_proc(@myodbc_remove_escape,myodbc_remove_escape);
    assign_proc(@MysqL_thread_safe,MysqL_thread_safe);
  end;
  Result := libMysqL_status;
end;

procedure libMysqL_free;
begin
  if libMysqL_handle <> 0 then FreeLibrary(libMysqL_handle);
  libMysqL_handle := 0;
  libMysqL_status := LIBMysqL_UNDEFINED;
end;

// -----------------------------------------------------------------------------------------------
INITIALIZATION
// -----------------------------------------------------------------------------------------------

begin
  {$IFNDEF DONT_LOAD_DLL}
  libMysqL_load(nil);
  {$ENDIF} // DONT_LOAD_DLL
end;

// -----------------------------------------------------------------------------------------------
FINALIZATION
// -----------------------------------------------------------------------------------------------

begin
  libMysqL_free;
end;

end.
View Code

 

 

连接本机MysqL的例子:

// uses MysqL;   //当前目录要有libMysqL.dll,当然,放在系统目录也可以,这个文件MysqL安装目录里有
procedure TForm1.Button1Click(Sender: TObject);
var
  Row: PMysqL_ROW;
  MResult: PMysqL_RES;
  dbh:PMysqL;
  tables:string;
begin
  dbh := MysqL_init(nil);
  if dbh=nil then    begin  showmessage(初始化MysqL对象失败。);  exit;   end;
  if nil=MysqL_connect(dbh,localhost,root,‘‘) then
  begin
    Memo1.lines.Add(MysqL_error(dbh));  exit;
  end;
  if 0<>MysqL_select_db(dbh,chemdbs) then
  begin
    Memo1.Lines.Add(MysqL_error(dbh));
    MysqL_close(dbh);  exit;
  end;
  tables:=maildb;
  MysqL_query(dbh,PChar(select id,dw,em,lxr,flg from +tables+ where flg=0 limit 0,30000));
  MResult := MysqL_use_result(dbh);
  EMList.Clear;
  Ems.Clear; Sta.Clear;
  repeat
    Row := MysqL_fetch_row(MResult);
    if Row<>nil then begin
      Ems.Add(Row[2] + , + Row[1] + , + Row[3]);
      Sta.Add(1);
      EMList.Items.Add.Caption :=‘‘;
    end;
  until Row=nil;
  MysqL_free_result(MResult);
  MysqL_close(dbh);
  Logs.Lines.Add(inttostr(Ems.Count) + Record Read); 
end; 

 

1、基于安全考虑,很多MysqL服务器只允许本机连接,检查你的linux上的MysqL是否允许外部连接。
2、程序中只要MysqL_connect(dbh,‘localhost‘,‘root‘,‘‘) 后面3个参数正确就可以,主机地址、用户名密码。即可连接到其它服务器上的MysqL
3、移动数据只要把MysqL安装目录下的data目录下的某个目录移走,一个目录是一个数据库

 

libMysqL.dll 版本是:3.23.49
MysqL.pas 版本是3.23.49
后台MysqL服务器版本是3.23.47 for win2000及3.23.42 for sco openserver 5.0.6
经测试:运行正确,非常好.

把ado,bde,odbc扔到垃圾堆去吧.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐