1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, winsock;
type
TForm1 = class(TForm)
bc: TButton;
l1: TLabel;
ehost: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
euser: TEdit;
epassword: TEdit;
emasterdb: TEdit;
procedure bcClick(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
procedure connexion;
end;
var
Form1: TForm1;
type
my_bool = byte;
gptr = pansiChar;
my_ulonglong = int64;
const
MYSQL_ERRMSG_SIZE = 200;
type
my_socket = TSocket;
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;
type
mysql_status = (
MYSQL_STATUS_READY, MYSQL_STATUS_GET_RESULT,
MYSQL_STATUS_USE_RESULT
);
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;
type
enum_field_types = FIELD_TYPE_DECIMAL..FIELD_TYPE_STRING;
type
PMYSQL_FIELD = ^TMYSQL_FIELD;
TMYSQL_FIELD = record
name: pansiChar; // Name of column
table: pansiChar; // Table of column if column was a field
org_table : pansiChar;
db : pansiChar;
def: pansiChar; // Default value (set by mysql_list_fields)
length: longword; // Width of column
max_length: longword; // Width of column
flags: longword; // Div flags
decimals: longword; // Number of decimals in field
_type: enum_field_types; // Type of field. Se mysql_com.h for types
end;
type
PMYSQL_FIELDS = ^TMYSQL_FIELDS;
TMYSQL_FIELDS = array[0..MaxInt div SizeOf(TMYSQL_FIELD) - 1] of TMYSQL_FIELD;
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
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: pansiChar;
my_cnf_file, my_cnf_group, charset_dir, charset_name: pansiChar;
use_ssl: my_bool; // if to use SSL or not
ssl_key: pansiChar; // PEM key file
ssl_cert: pansiChar; // PEM cert file
ssl_ca: pansiChar; // PEM CA file
ssl_capath: pansiChar; // PEM directory of CA-s?
end;
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, user, passwd, unix_socket, server_version, host_info, info, db: pansiChar;
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
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
);
function mysql_init(_mysql: PMYSQL): PMYSQL; stdcall;
external 'libmysql.dll' name 'mysql_init';
function mysql_options(_mysql: PMYSQL; option: mysql_option; const arg: pansiChar): longint; stdcall;
external 'libmysql.dll' name 'mysql_options';
function mysql_real_connect(_mysql: PMYSQL; const host, user, passwd, db: pansiChar;
port: longword; const unix_socket: pansiChar; clientflag: longword): PMYSQL;
external 'libmysql.dll' name 'mysql_real_connect';
implementation
{$R *.dfm}
procedure TForm1.bcClick(Sender: TObject);
begin
connexion;
end;
procedure Tform1.connexion;
Var
portip: integer;
// myres: PMYSQL_RES;
// myrow: PMYSQL_ROW;
// req: string;
// i,j, debut, sep:integer;
var
host, masterdb, user, password: ansistring;
phost, pmasterdb, puser, ppassword: pansichar;
mysqlc: pmysql;
begin
host:=ansistring(ehost.text);
user:=ansistring(euser.Text);
password:=ansistring(epassword.Text);
masterdb:=ansistring(emasterdb.Text);
phost:=pansichar(host);
puser:=pansichar(user);
ppassword:=pansichar(password);
pmasterdb:=pansichar(masterdb);
portip:=3306;
MySQLC := mysql_init(nil);
if mysqlc<>nil then
begin
mysql_options(mysqlC,MYSQL_OPT_COMPRESS,nil);
//plusieurs tentatives de syntaxe mais toutes arrivent au même résultat
if mysql_real_connect(mysqlC, phost, puser, ppassword, pmasterdb, portip, nil, 0)=nil then
//if mysql_real_connect(mysqlC, pansiChar(ansistring(ehost.text)),
// pansiChar(ansistring(euser.text)),
// pansiChar(ansistring(epassword.text)),
// pansichar(ansistring(emasterdb.text)),
// portip, nil, 0)=nil then
begin
l1.caption:='La connexion a echouée';
exit;
end
else
begin
l1.caption:='Connexion OK';
end;
end;
end;
end. |
Partager