Anda di halaman 1dari 9

judul :

dump mysql with delphi - opensource

header :
hmm...ini sebenernya dah pernah tak tanyain dalam thread tapi lom ada jawaban yang
memuaskan, aq coba ekplor terus dan akhirnya 3 bulan yang lalu bisa juga buatnya,
disini aq akan membagikan ilmu yang aq dapat yaitu cara untuk melakukan dumping
database mysql.

body :
setelah aq modifikasi dengan perubahan kode2 yang lebih sederhana semoga contoh
tool ini dapat dipelajari dengan mudah (kalo masih sulit maap ya :)) ini dia
screenshotnya :
http://www.geocities.com/einsthonk/tes.jpg
untuk artikel ini aq hanya memberikan contoh cara untuk dump mysql, untuk
restorenya...hmm...di artikel yang lain ya..:) (eksplor dulu sendiri), seperti
biasanya aq gak terima kritikan,saran apalagi bug dalam program ini karena program
ini hanyalah sekedar bagi-bagi ilmu doank dan kalo ada bug ya perbaiki sendiri ya
:) , berikut ini listing program selengkapnya dari ubackup.pas :

<code>
unit ubackup;

interface

uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, extctrls, stdctrls, zsqlprocessor, db, zabstractrodataset,
zabstractdataset, zdataset, zconnection,clipbrd;

type
tfbackup = class(tform)
panel1: tpanel;
panel2: tpanel;
label1: tlabel;
label2: tlabel;
label3: tlabel;
edserver: tedit;
eduser: tedit;
edpass: tedit;
label4: tlabel;
cbdb: tcombobox;
panel3: tpanel;
panel4: tpanel;
dbcreate: tcheckbox;
tbcreate: tcheckbox;
datacreate: tcheckbox;
panel5: tpanel;
button1: tbutton;
dbkonek: tzconnection;
q1: tzquery;
procedure cbdbdropdown(sender: tobject);
procedure button1click(sender: tobject);
procedure tbcreateclick(sender: tobject);
private
procedure connectdb;
procedure scriptsql(_sql: string ; issearch: boolean);
//procedure per-module
function createdatabase(dbname: string): string;
function createtable(dbname: string) : tstringlist;
function createdata(tbname: string): string;
function cektanggal(s: string) : string;
{ private declarations }
public
{ public declarations }
end;

var
fbackup: tfbackup;

implementation

{$r *.dfm}

procedure tfbackup.button1click(sender: tobject);


var
slsql: tstringlist;
sqlcomment: tstringlist;
dlg: tsavedialog;
begin
if cbdb.itemindex < 0 then
begin
application.messagebox('anda belum memilih database yang akan di-backup.',
'informasi',mb_ok or mb_iconexclamation);
exit;
end;
slsql := tstringlist.create;
sqlcomment := tstringlist.create;
try
if tbcreate.checked then
slsql := createtable(cbdb.text);
slsql.insert(0,createdatabase(cbdb.text));

scriptsql('select version()',true);
//tulis komentar
with sqlcomment do
begin
add('#
-----------------------------------------------------------------------------');
add('# database backup 1.0');
add('# -----------------------------------------------------------------------'
+
'------');
add('# host : ' + edserver.text );
add('# user name : ' + eduser.text );
add('# database : ' + cbdb.text );
add('# versi server : ' + q1.fields[0].asstring);
add('# tanggal dumping : ' + formatdatetime('dddd, dd mmmm yyyy',now));
add('#
-----------------------------------------------------------------------------');
add('# created by tigor mangatur manurung...!!!!');
add('# file ini merupakan hasil generate backup dengan menggunakan');
add('# database backup 1.0');
add('# -----------------------------------------------------------------------'
+
'------');
end;
slsql.insert(0,sqlcomment.text);
dlg := tsavedialog.create(self);
try
dlg.filter := 'file sql | *.sql';
dlg.title := 'simpan di...';
if dlg.execute then
slsql.savetofile(dlg.filename + '.sql');
finally
dlg.free;
end;

dbkonek.disconnect;
finally
slsql.free;
sqlcomment.free;
end;
end;

procedure tfbackup.cbdbdropdown(sender: tobject);


begin
connectdb;
scriptsql('show databases',true);
cbdb.items.clear;
while not(q1.eof) do
begin
cbdb.items.add(q1.fieldbyname('database').asstring);
q1.next;
end;
end;

procedure tfbackup.connectdb;
begin
with dbkonek do
begin
disconnect;
connect;
end;
end;

function tfbackup.createdata(tbname: string): string;


var
_sql: string;
i,_i: integer;
s,svalue: string;
h: string;
begin
h := '''';
_sql := 'select * from `' + tbname + '`';
scriptsql(_sql,true);

if q1.eof then
begin
result := #13 + '/* [ data kosong ] */';
exit;
end;

for i := 0 to q1.recordcount - 1 do
begin
for _i := 0 to q1.fields.count - 1 do
begin
svalue := svalue + h + cektanggal(q1.fields[_i].asstring) + h + ',';
end;
svalue := copy(svalue,1,length(svalue) - 1);
svalue := 'replace into ' + tbname + ' values(' + svalue + ');';
s := s + #13 + svalue;
svalue := '';
q1.next;
end;
s := #13 + #13 + '/* ==> data untuk tabel "' + tbname + '" */' + #13 + s;
result := s
end;

function tfbackup.createdatabase(dbname: string): string;


var
h: string;
trflag: treplaceflags;
sresult : tstringlist;
begin
sresult := tstringlist.create;
try
trflag := [rfreplaceall];
if not(dbcreate.checked) then
begin
result := '';
exit;
end;

h := '`';
connectdb;
scriptsql('show create database ' + dbname,true);
sresult.add(stringreplace(q1.fields[1].asstring,'create database ','create ' +
'database if not exists ',trflag) + ';');
sresult.add('use `' + dbname + '`;');
result := sresult.text;

finally
sresult.free;
end;
end;

function tfbackup.createtable(dbname: string): tstringlist;


var
_sql: string;
slsql: tstringlist;
tbname: tstringlist;
i: integer;
trflag: treplaceflags;
begin
trflag := [rfreplaceall];
slsql := tstringlist.create;
tbname := tstringlist.create;
try
dbkonek.database := dbname;
connectdb;
slsql.clear;

_sql := 'show tables';


scriptsql(_sql,true);
//setlength(tbname,q1.recordcount - 1);
while not(q1.eof) do
begin
tbname.add(q1.fields[0].asstring);
q1.next;
end;

for i := 0 to q1.recordcount - 1 do
begin
_sql := 'show create table ' + tbname[i] ;
scriptsql(_sql,true);

while not(q1.eof) do
begin
slsql.add('/* |===================== [ dumping tabel ' + tbname[i] + ' ]
=====================| */');
slsql.add(stringreplace(q1.fields[1].asstring,'create table ','create
table if not exists ',trflag) + ';');
if datacreate.checked then
slsql.add(createdata(tbname[i]));

slsql.add('/* |===================== [ akhir dari tabel ' + tbname[i] +


' ] =====================| */');
slsql.add('');
slsql.add('');
slsql.add('');
slsql.add('');
q1.next;
end;
end;
result := slsql;
finally
// slsql.free;
end;
end;

procedure tfbackup.scriptsql(_sql: string; issearch: boolean);


begin
with q1 do
begin
close;
sql.text := _sql;
if issearch then
open
else
execsql;
end;
end;

function tfbackup.cektanggal(s: string) : string;


var
sout: tdatetime;
begin
if trystrtodate(s,sout) then
result := formatdatetime('yyyy-mm-dd',sout)
else
result := s;

end;

procedure tfbackup.tbcreateclick(sender: tobject);


begin
datacreate.enabled := tbcreate.checked;
end;

end.
</code>

untuk ubackup.dfm seperti berikut :


<code>
object fbackup: tfbackup
left = 252
top = 207
borderstyle = bsdialog
caption = 'database backup 1.0'
clientheight = 306
clientwidth = 457
color = 16763283
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'tahoma'
font.style = []
oldcreateorder = false
position = podesktopcenter
pixelsperinch = 96
textheight = 13
object panel4: tpanel
left = 8
top = 182
width = 441
height = 75
bevelouter = bvlowered
color = clwhite
parentbackground = false
taborder = 3
object dbcreate: tcheckbox
left = 8
top = 7
width = 169
height = 17
cursor = crhandpoint
caption = 'create database (if not exists)'
checked = true
state = cbchecked
taborder = 0
end
object tbcreate: tcheckbox
left = 8
top = 30
width = 153
height = 17
cursor = crhandpoint
caption = 'create table (if not exists)'
checked = true
state = cbchecked
taborder = 1
onclick = tbcreateclick
end
object datacreate: tcheckbox
left = 8
top = 53
width = 201
height = 17
cursor = crhandpoint
caption = 'write data in table (replace mode)'
checked = true
state = cbchecked
taborder = 2
end
end
object panel2: tpanel
left = 8
top = 31
width = 441
height = 122
bevelouter = bvlowered
color = clwhite
parentbackground = false
taborder = 1
object label1: tlabel
left = 8
top = 7
width = 64
height = 13
caption = 'server host :'
end
object label2: tlabel
left = 13
top = 38
width = 59
height = 13
caption = 'user name :'
end
object label3: tlabel
left = 19
top = 69
width = 53
height = 13
caption = 'password :'
end
object label4: tlabel
left = 19
top = 93
width = 53
height = 13
caption = 'database :'
end
object edserver: tedit
left = 78
top = 6
width = 155
height = 21
taborder = 0
text = 'localhost'
end
object eduser: tedit
left = 78
top = 36
width = 121
height = 21
taborder = 1
text = 'root'
end
object edpass: tedit
left = 78
top = 66
width = 91
height = 21
passwordchar = '*'
taborder = 2
end
object cbdb: tcombobox
left = 78
top = 93
width = 155
height = 21
style = csdropdownlist
itemheight = 13
taborder = 3
ondropdown = cbdbdropdown
end
end
object panel1: tpanel
left = 8
top = 8
width = 441
height = 25
caption = 'database configuration'
color = 9549311
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'tahoma'
font.style = [fsbold]
parentbackground = false
parentfont = false
taborder = 0
end
object panel3: tpanel
left = 8
top = 159
width = 441
height = 25
caption = 'backup configuration'
color = 13295103
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'tahoma'
font.style = [fsbold]
parentbackground = false
parentfont = false
taborder = 2
end
object panel5: tpanel
left = 10
top = 263
width = 439
height = 34
bevelouter = bvlowered
color = 15791615
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'tahoma'
font.style = [fsbold]
parentbackground = false
parentfont = false
taborder = 4
object button1: tbutton
left = 360
top = 6
width = 75
height = 25
caption = '&execute !!!'
taborder = 0
onclick = button1click
end
end
object dbkonek: tzconnection
protocol = 'mysql'
hostname = 'localhost'
user = 'root'
left = 392
top = 40
end
object q1: tzquery
connection = dbkonek
params = <>
left = 424
top = 40
end
end
</code>
hmm...masih bingun ato malah males liatnya??? ya udah kalo yang pingin instant
langsung aja donlot di link berikut ini :
http://www.esnips.com/nsdoc/2afac6a8-1ca1-4d4d-bde1-d28695202a5a/?action=forcedl
ok deh semoga berguna ya...

Anda mungkin juga menyukai