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 siri), 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 siri ya :), berikut ini listing program selengkapnya dari ubackup.pas : <code> unit ubackup; interface uses windows, messages, sysutils, iants, 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(ser: tobject); procedure button1click(ser: tobject); procedure tbcreateclick(ser: 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 } fbackup: tfbackup; implementation {$r *.dfm} procedure tfbackup.button1click(ser: tobject); slsql: tstringlist; sqlcomment: tstringlist; dlg: tsavedialog; if cbdb.itemindex < 0 then application.messagebox('anda belum memilih database yang akan di-backup.', 'informasi',mb_ok or mb_iconexclamation); exit; 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 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('# -----------------------------------------------------------------------' + '------');
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; dbkonek.disconnect; finally slsql.free; sqlcomment.free; procedure tfbackup.cbdbdropdown(ser: tobject); connectdb; scriptsql('show databases',true); cbdb.items.clear; while not(q1.eof) do cbdb.items.add(q1.fieldbyname('database').asstring); q1.next; procedure tfbackup.connectdb; with dbkonek do disconnect; connect; function tfbackup.createdata(tbname: string): string; _sql: string; i,_i: integer; s,svalue: string; h: string; h := ''''; _sql := 'select * from `' + tbname + '`'; scriptsql(_sql,true); if q1.eof then result := #13 + '/* [ data kosong ] */'; exit; for i := 0 to q1.recordcount - 1 do
for _i := 0 to q1.fields.count - 1 do svalue := svalue + h + cektanggal(q1.fields[_i].asstring) + h + ','; svalue := copy(svalue,1,length(svalue) - 1); svalue := 'replace into ' + tbname + ' values(' + svalue + ');'; s := s + #13 + svalue; svalue := ''; q1.next; s := #13 + #13 + '/* ==> data untuk tabel "' + tbname + '" */' + #13 + s; result := s function tfbackup.createdatabase(dbname: string): string; h: string; trflag: treplaceflags; sresult : tstringlist; sresult := tstringlist.create; try trflag := [rfreplaceall]; if not(dbcreate.checked) then result := ''; exit; 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; function tfbackup.createtable(dbname: string): tstringlist; _sql: string; slsql: tstringlist; tbname: tstringlist; i: integer; trflag: treplaceflags; 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 tbname.add(q1.fields[0].asstring); q1.next; for i := 0 to q1.recordcount - 1 do _sql := 'show create table ' + tbname[i] ; scriptsql(_sql,true); while not(q1.eof) do 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; result := slsql; finally // slsql.free; procedure tfbackup.scriptsql(_sql: string; issearch: boolean); with q1 do close; sql.text := _sql; if issearch then open else execsql; function tfbackup.cektanggal(s: string) : string; sout: tdatetime; if trystrtodate(s,sout) then
result := formatdatetime('yyyy-mm-dd',sout) else result := s; procedure tfbackup.tbcreateclick(ser: tobject); datacreate.enabled := tbcreate.checked;. </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 top = 182 width = 441 height = 75 bevelouter = bvlowered color = clwhite taborder = 3 object dbcreate: tcheckbox top = 7 width = 169 height = 17 cursor = crhandpoint caption = 'create database (if not exists)' checked = true state = cbchecked taborder = 0 object tbcreate: tcheckbox top = 30 width = 153 height = 17
cursor = crhandpoint caption = 'create table (if not exists)' checked = true state = cbchecked taborder = 1 onclick = tbcreateclick object datacreate: tcheckbox top = 53 width = 201 height = 17 cursor = crhandpoint caption = 'write data in table (replace mode)' checked = true state = cbchecked taborder = 2 object panel2: tpanel top = 31 width = 441 height = 122 bevelouter = bvlowered color = clwhite taborder = 1 object label1: tlabel top = 7 width = 64 height = 13 caption = 'server host :' object label2: tlabel left = 13 top = 38 width = 59 height = 13 caption = 'user name :' object label3: tlabel left = 19 top = 69 width = 53 height = 13 caption = 'password :' object label4: tlabel left = 19 top = 93 width = 53 height = 13 caption = 'database :' object edserver: tedit left = 78
top = 6 width = 155 height = 21 taborder = 0 text = 'localhost' object eduser: tedit left = 78 top = 36 width = 121 height = 21 taborder = 1 text = 'root' object edpass: tedit left = 78 top = 66 width = 91 height = 21 passwordchar = '*' taborder = 2 object cbdb: tcombobox left = 78 top = 93 width = 155 height = 21 style = csdropdownlist itemheight = 13 taborder = 3 ondropdown = cbdbdropdown object panel1: tpanel 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] parentfont = false taborder = 0 object panel3: tpanel 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] parentfont = false taborder = 2 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] parentfont = false taborder = 4 object button1: tbutton left = 360 top = 6 width = 75 height = 25 caption = '&execute!!!' taborder = 0 onclick = button1click object dbkonek: tzconnection protocol = 'mysql' hostname = 'localhost' user = 'root' left = 392 top = 40 object q1: tzquery connection = dbkonek params = <> left = 424 top = 40 </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...