Dasar-Dasar Pemrograman Database Menggunakan Gambas + MySQL + Ubuntu

Dasar-Dasar Pemrograman Database Menggunakan Gambas + MySQL + Ubuntu

Berikut ini contoh bagaimana melakukan koneksi, tambah data, hapus data, cari data ke MySQL menggunakan gambas.

Penulis berasumsi bahwa di linux andatelah teristall lampp.

Sebelum memulai pemrograman, syarat untuk mengkoneksikan database adalah adanya gb.db component dan gb.mysql  component melalui Ubuntu Software Center.

Aktifkan kedua komponen tersebut melalui Project > Properties  pada tab Components aktifkan gb.db component dan gb.mysql  component.

Kalau mendapati pesan bahwa tidak bisa melakukan koneksi ke sock mysql, masukkan perintah ini melalui console :

root@ubuntu:~# mkdir /var/run/mysqld
root@ubuntu:~# ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld//mysqld.sock

Berikut Source code lengkap dari Tutorial ini:

‘ Gambas class file
PUBLIC myrs AS Result

PUBLIC con AS NEW Connection

PUBLIC PROCEDURE Connect()
Con.Close()              ‘ Tutup Koneksi
Con.Type = “mysql”       ‘ Tipe Koneksi
Con.Host = “localhost”   ‘ Nama server
Con.Login = “root”       ‘ Username pada mysql
Con.Port = “3306″        ‘ Port pada mysql
Con.Name = “coba”        ‘ Nama database
Con.Password = “”        ‘ Password mysql
Con.Open()               ‘ Buka koneksi
END

PUBLIC SUB _new()

END
‘==================================================================
PUBLIC SUB Form_Open()
FMain.left = (Desktop.width / 2) – (FMain.width / 2)
FMain.top = (Desktop.height / 2) – (FMain.height / 2)
Connect()
isilist
END
‘==================================================================
PUBLIC SUB ListBox1_Click()
ListBox2.index = ListBox1.index
TextBox3.text = ListBox1.Text
TextBox4.text = ListBox1.Text
TextBox5.text = ListBox2.Text
END
‘==================================================================
PUBLIC SUB ctambah_Click()
DIM sql AS String
sql = “insert into stetbl(kata, arti) values (‘” & TextBox1.text & “‘,’” & TextBox2.text & “‘)”
myrs = con.Exec(sql)
isilist
TextBox1.text = “”
TextBox2.text = “”
END
‘==================================================================
PUBLIC SUB isilist()
DIM sql AS String
DIM i AS Integer

ListBox1.Clear
ListBox2.Clear

sql = “select * FROM stetbl ” “”
myrs = con.Exec(sql)

FOR i = 1 TO myrs.Count
ListBox1.Add(myrs!kata)
ListBox2.Add(myrs!arti)
myrs.MoveNext
NEXT
ListBox1.Index = 0
END
‘==================================================================
PUBLIC SUB chapus_Click()
DIM sql AS String
sql = “delete from stetbl where kata = ‘” & TextBox3.text & “‘”
PRINT sql
myrs = con.Exec(sql)
isilist
TextBox3.text = “”
END
‘==================================================================
PUBLIC SUB ckeluar_Click()
FMain.Close
END
‘==================================================================
PUBLIC SUB crubah_Click()
DIM sql AS String
sql = “update stetbl set arti = ‘” & TextBox5.text & “‘ where kata = ‘” & TextBox4.text & “‘”
PRINT sql
myrs = con.Exec(sql)
isilist
END
‘==================================================================
PUBLIC SUB ccari_Click()
DIM sql AS String
DIM i AS Integer
sql = “select * from stetbl where kata = ‘” & TextBox6.text & “‘”
PRINT sql
myrs = con.Exec(sql)

ListBox1.Clear
ListBox2.Clear

FOR i = 1 TO myrs.Count
ListBox1.Add(myrs!kata)
ListBox2.Add(myrs!arti)
myrs.MoveNext
NEXT

END

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s