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:
| unit unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls, ExtCtrls, mysql;
type TForm1 = class(TForm) HostEdit: TEdit; Bevel1: TBevel; Label1: TLabel; Label2: TLabel; UserEdit: TEdit; Label3: TLabel; PasswordEdit: TEdit; LoginButton: TButton; DatabaseListBox: TListBox; Label4: TLabel; Label5: TLabel; Label6: TLabel; TableListBox: TListBox; TableStringGrid: TStringGrid; Label7: TLabel; FieldListGrid: TStringGrid; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure LoginButtonClick(Sender: TObject); procedure DatabaseListBoxClick(Sender: TObject); procedure TableListBoxClick(Sender: TObject); private LibHandle: PMYSQL; ConHandle: PMYSQL; function DoQuote(const s: String): String; public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure ClearGrid(Grid: TStringGrid); var row, col: Integer; begin for row := 1 to Grid.RowCount - 1 do for col := 0 to Grid.ColCount - 1 do Grid.Cells[col, row] := ''; end;
procedure TForm1.DatabaseListBoxClick(Sender: TObject); var MyResult: Integer; mySQL_Res: PMYSQL_RES; MYSQL_ROW: PMYSQL_ROW; begin TableListBox.Items.Clear; ClearGrid(FieldListGrid); TableListBox.Enabled := False; with DatabaseListBox do MyResult := mysql_select_db(ConHandle, PAnsiChar(Items[ItemIndex])); if MyResult<>0 then raise Exception.Create(mysql_error(ConHandle)); mySQL_Res := mysql_list_tables(ConHandle, nil); if mySQL_Res=nil then raise Exception.Create(mysql_error(ConHandle)); try repeat MYSQL_ROW := mysql_fetch_row(mySQL_Res); if MYSQL_ROW<>nil then begin TableListBox.Items.Add(MYSQL_ROW^[0]); end; until MYSQL_ROW=nil; finally mysql_free_result(mySQL_Res); end; if TableListBox.Items.Count>0 then begin TableListBox.Enabled := True; end; end;
function TForm1.DoQuote(const s: String): String; begin SetLength(Result, Length(s)*2+1); SetLength(Result, mysql_real_escape_string(ConHandle, Pointer(Result), PChar(s), Length(s))); Result := '`' + Result + '`'; end;
procedure TForm1.FormCreate(Sender: TObject); begin libmysql_fast_load(nil); LibHandle := mysql_init(nil); FieldListGrid.Cells[0, 0] := 'Name'; FieldListGrid.Cells[1, 0] := 'Type'; FieldListGrid.Cells[2, 0] := 'Len'; FieldListGrid.Cells[3, 0] := 'Flags'; end;
procedure TForm1.FormDestroy(Sender: TObject); begin mysql_close(LibHandle); libmysql_free; end;
procedure TForm1.LoginButtonClick(Sender: TObject); var mySQL_Res: PMYSQL_RES; MYSQL_ROW: PMYSQL_ROW; begin DatabaseListBox.Items.Clear; DatabaseListBox.Enabled := False; ConHandle := mysql_real_connect(LibHandle, PAnsiChar(HostEdit.Text), PAnsiChar(UserEdit.Text), PAnsiChar(PasswordEdit.Text), nil, 0, nil, 0); if ConHandle=nil then raise Exception.Create(mysql_error(LibHandle)); Caption := HostEdit.Text + ' ' + mysql_get_server_info(ConHandle); mySQL_Res := mysql_list_dbs(ConHandle, nil); if mySQL_Res=nil then raise Exception.Create(mysql_error(ConHandle)); try repeat MYSQL_ROW := mysql_fetch_row(mySQL_Res); if MYSQL_ROW<>nil then begin DatabaseListBox.Items.Add(MYSQL_ROW^[0]); end; until MYSQL_ROW=nil; finally mysql_free_result(mySQL_Res); end; if DatabaseListBox.Items.Count>0 then begin LoginButton.Enabled := False; DatabaseListBox.Enabled := True; end; end;
function GetFieldTypeString(mySQL_Field: PMYSQL_FIELD): String; const FieldtypeString1: array [MYSQL_TYPE_DECIMAL..MYSQL_TYPE_BIT] of String=( 'NUMERIC', 'TINYINT', 'SMALLINT', 'INTEGER', 'FLOAT', 'DOUBLE', 'T_NULL', 'TIMESTAMP', 'BIGINT', 'MEDIUMINT', 'DATE', 'TIME', 'DATETIME', 'YEAR', 'NEWDATE', 'VARCHAR', 'BIT'); FieldtypeString2: array [MYSQL_TYPE_NEWDECIMAL..MYSQL_TYPE_GEOMETRY] of String=( 'NEWDECIMAL', 'ENUM', 'SET', 'TINY_BLOB', 'MEDIUM_BLOB', 'LONG_BLOB', 'BLOB', 'VAR_STRING', 'STRING', 'GEOMETRY'); begin if mysql_field_type(mySQL_Field) in [MYSQL_TYPE_DECIMAL..MYSQL_TYPE_BIT] then Result := FieldtypeString1[mysql_field_type(mySQL_Field)] else if mysql_field_type(mySQL_Field) in [MYSQL_TYPE_NEWDECIMAL..MYSQL_TYPE_GEOMETRY] then Result := FieldtypeString2[mysql_field_type(mySQL_Field)] else Result := 'unknown'; end;
function GetFieldFlagString(mySQL_Field: PMYSQL_FIELD): String; begin Result := ''; if IS_NUM_FIELD(mySQL_Field) then begin if (mysql_field_flag(mySQL_Field) and UNSIGNED_FLAG) <> 0 then Result := ' UNSIGNED'; if (mysql_field_flag(mySQL_Field) and AUTO_INCREMENT_FLAG) <> 0 then Result := Result + ' INC'; end else begin if (mysql_field_flag(mySQL_Field) and ENUM_FLAG)<>0 then Result := ' ENUM' else if (mysql_field_flag(mySQL_Field) and SET_FLAG)<>0 then Result := ' SET' else if (mysql_field_flag(mySQL_Field) and BLOB_FLAG)<>0 then Result := ' BLOB'; end; if IS_NOT_NULL(mysql_field_flag(mySQL_Field)) then Result := Result + ' NOT NULL'; end;
procedure TForm1.TableListBoxClick(Sender: TObject); var i, r, field_count, row_count: Integer; mySQL_Res: PMYSQL_RES; MYSQL_ROW: PMYSQL_ROW; mySQL_Field: PMYSQL_FIELD; sql: String; tablename: String; begin ClearGrid(FieldListGrid); with TableListBox do tablename := Items[ItemIndex]; tablename := DoQuote(tablename);
sql := 'select * from ' + tablename; if mysql_real_query(ConHandle, PChar(sql), Length(sql))<>0 then raise Exception.Create(mysql_error(ConHandle)); mySQL_Res := mysql_store_result(ConHandle); if mySQL_Res<>nil then begin try field_count := mysql_num_fields(mySQL_Res); FieldListGrid.RowCount := field_count+1; TableStringGrid.ColCount := field_count; for i := 0 to field_count - 1 do begin mySQL_Field := mysql_fetch_field(mySQL_Res); if mySQL_Field<>nil then begin TableStringGrid.Cells[i, 0] := mysql_field_name(mySQL_Field); FieldListGrid.Cells[0, i+1] := mysql_field_name(mySQL_Field); FieldListGrid.Cells[1, i+1] := GetFieldTypeString(mySQL_Field); FieldListGrid.Cells[2, i+1] := IntToStr(mysql_field_length(mySQL_Field)); FieldListGrid.Cells[3, i+1] := GetFieldFlagString(mySQL_Field); end; end; row_count := mysql_num_rows(mySQL_Res); if row_count>0 then begin TableStringGrid.RowCount := row_count + 1; for r := 1 to row_count do begin MYSQL_ROW := mysql_fetch_row(mySQL_Res); if MYSQL_ROW<>nil then begin for i := 0 to field_count - 1 do TableStringGrid.Cells[i, r] := MYSQL_ROW^[i]; end; end; end else begin TableStringGrid.RowCount := 2; for i := 0 to field_count - 1 do TableStringGrid.Cells[i, 1] := ''; end; finally mysql_free_result(mySQL_Res); end; end; end;
end. |