How to program Gambas with MySQL in Ubuntu
- sudo mkdir /var/run/mysqld
- sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
Assuming you already know how to do simple form using Gambas.If not, better you check out this post.
gb.db must be selected.
Create a form.
Here are the Gambas code.
- ' Gambas class file
- PUBLIC MyConn AS Connection
- PUBLIC MyRS AS Result
- PUBLIC vCari AS String
- STATIC PUBLIC SUB Main()
- DIM oForm AS form
- oForm = NEW fsahabat
- oForm.showModal()
- END
- PUBLIC SUB _new()
- ME.center
- DBConnect
- getData
- transferData
- fillUpListBox
- END
- PUBLIC SUB DBConnect()
- MyConn = NEW Connection
- MyConn.Close
- MyConn.Type = "mysql"
- MyConn.Host = "localhost"
- MyConn.Login = "root"
- MyConn.Password = "root"
- MyConn.Name = "sahabat"
- MyConn.Open
- CATCH
- Message.error(Error.text)
- END
- PUBLIC SUB getData()
- DIM sql AS String
- sql = "select * from tsahabat order by id"
- MyRS = MyConn.Exec(sql)
- CATCH
- Message.error(Error.text)
- END
- PUBLIC SUB transferData()
- vCari = Str(MyRS!id)
- txtName.text = MyRS!nama
- txtAlamat.text = MyRS!almt1
- txtPhone.text = MyRS!telp
- CATCH
- Message.error(Error.text)
- END
- PUBLIC SUB fillUpListBox()
- DIM i AS Integer
- DIM vKode AS String
- listbox1.Clear
- FOR i = 1 TO MyRS.Count
- vKode = Space$(7 - Len(Str(MyRS!id))) & Str(MyRS!id)
- listbox1.add(vKode & " | " & MyRS!nama)
- MyRS.MoveNext
- NEXT
- CATCH
- Message.error(Error.text)
- END
- PUBLIC SUB listbox1_Click()
- DIM sql AS String
- btnSave.Text = "UPDATE"
- vCari = Trim(Left$(listbox1.text, 7))
- sql = "select * from tsahabat where id =" & vCari
- MyRS = MyConn.Exec(sql)
- transferData
- CATCH
- Message.Error(Error.Text)
- END
- PUBLIC SUB btnSave_Click()
- DIM sql AS String
- IF btnAdd.Enabled = FALSE THEN
- sql = "insert into tsahabat (nama,telp,almt1 )values( '" &
- txtName.text & "','" & txtPhone.text & "','" &
- txtAlamat.text & "')"
- MyRS = MyConn.Exec(sql)
- btnAdd.Enabled = TRUE
- ELSE
- sql = "update tsahabat set "
- sql = sql & "nama = '" &
- txtName.text
- sql = sql & "',almt1 = '" &
- txtAlamat.text
- sql = sql & "',telp = '" & txtPhone.text & "' where id = " & vCari
- MyRS = MyConn.Exec(sql)
- ENDIF
- getData
- fillUpListBox
- CATCH
- Message.Error(Error.Text)
- END
- PUBLIC SUB btnCancel_Click()
- DIM sql AS String
- vCari = Trim(Left$(listbox1.text, 7))
- IF vCari <> "" THEN
- btnAdd.Enabled = TRUE
- sql = "select * from tsahabat where id =" & vCari
- MyRS = MyConn.Exec(sql)
- transferData
- CATCH
- Message.Error(Error.Text)
- ELSE
- clear
- ENDIF
- END
- PUBLIC SUB btnAdd_Click()
- clear
- btnAdd.Enabled = FALSE
- btnSave.Text = "SAVE"
- END
- PUBLIC SUB btnDelete_Click()
- DIM sql AS String
- IF Message.Question("Confirm to delete", "Yes", "No") = 1 THEN
- sql = "delete from tsahabat where id = " & vCari
- MyRS = MyConn.Exec(sql)
- getData
- transferData
- fillUpListBox
- ENDIF
- END
- PRIVATE SUB clear()
- txtName.text = ""
- txtPhone.text = ""
- txtAlamat.text = ""
- END
- PUBLIC SUB _free()
- MyConn.Close
- Message.Info("Thanks..., Salam Linux ")
- END
- 'End of program
- create table tsahabat (
- id int(7) NOT NULL auto_increment,
- nama char(50) ,almt1 char(255),telp char(10),
- primary key (id)
- );
- insert into tsahabat (nama,almt1,telp) values ('arejae','Sg Buloh,Selangor','012-1234567');
- create table tsahabat (
- id int(7) NOT NULL auto_increment,
- nama char(50) ,almt1 char(255),telp char(10),
- primary key (id)
- );
- insert into tsahabat (nama,almt1,telp) values ('arejae','Sg Buloh,Selangor','012-1234567');
Now you can program in Ubuntu, using Gambas and powered with MySQL.
Thanks again to IlmuKomputer
Happy programming.