Beranda > Pemrograman > 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

Kategori:Pemrograman
  1. Bambang
    4 Juli 2011 pukul 8:57 am

    UBLIC db AS NEW Connection
    PUBLIC rs AS Result
    PUBLIC btn AS Button
    PUBLIC btnok AS String
    PUBLIC btnno AS String
    ‘untuk koneksi ke database
    PUBLIC FUNCTION koneksi(localhost AS String, CSoldier AS String, kubujati AS String) AS Boolean
    db.close
    db.Type = “mysql”
    db.Host = “localhost”
    db.Login = “CSoldier”
    db.Password = “kubujati”
    db.Open
    RETURN TRUE
    CATCH
    Message.Warning(“Password Salah”, “OK”)
    RETURN FALSE
    END

    PUBLIC SUB isi(tbv AS GridView, sql AS String)
    DIM i AS Float
    rs = db.exec(sql)
    WITH rs
    tbv.rows.count = 0
    IF .count 0 THEN
    tbv.columns.count = .fields.count
    tbv.rows.count = .count
    END IF
    END WITH
    END

    Hasilnya gak bisa login selalu password salah mohon pencerahan

    Bambang Setiawan

  2. 4 September 2014 pukul 3:47 pm

    What’s Happening i am new to this, I stumbled upon this I’ve discovered
    It absolutely useful and it has aided me out loads.
    I hope to give a contribution & help other users like its helped me.
    Great job.

  3. 28 Oktober 2014 pukul 10:01 pm

    makasih tulisan nya…!!

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: