me,myself,english and programming..

How to program Gambas with MySQL in Ubuntu

I’ve read a nice introduction to Gambas here.Gambas is a free development environment based on a Basic interpreter with object extensions.

Since installing Gambas in ubuntu is a piece of cake, I then decide to try to program using Gambas. I found quite a number of article to do a connection with MySQL but then it fail when I run it with error message :

Cannot find driver for database: mysql

Googling help me to find this another nice article and i’m happily coding using that sample.Thanks. :)

then, I hit another error.

Can’t connect to local MySQL server through socket’/var/run/mysqld/mysqld.sock’(2)

This time I’m very sure it’s not about the code anymore, it’s something to do with Gambas configuration. Since I have install MySQL using XAMPP,socket file is in /opt/lampp/var/mysql/mysql.sock not at the place that Gambas looking for.I can’t find the solution yet. I just use temporary solution where I manually create the link file. Below are the command.

sudo mkdir /var/run/mysqld
sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock

Here are the way how to program Gambas with MySQL. :)

Assuming you already know how to do simple form using Gambas.If not, better you check out this post.

gb.db must be selected.

Gambas DB Component

Create a form.

Gambas 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

Here the table structure with sample data.

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');

You will have the output like below.

Gambas MySahabat

 

Now you can program in Ubuntu, using Gambas and powered with MySQL. :)

Thanks again to IlmuKomputer

Happy programming. :)

  1. 11 Responses to “How to program Gambas with MySQL in Ubuntu”

  2. By melayubuntu on Oct 5, 2008 | Reply

    demm…aku xde masa nk xplore jauh2 gambas nih…
    pe2 pun mmg gempak la tutorial nih…huhu

    melayubuntu’s last blog post..ODF Olympiad 2008

  3. By arejae on Oct 8, 2008 | Reply

    aku pun takder explore jauh2..sket2 jer zul.

  4. By Extremist Thinker on Oct 12, 2008 | Reply

    ore kito. slmt ari rayooo. bereh!

  5. By arejae on Oct 14, 2008 | Reply

    bereh…selamat jugok ko demo.

  6. By ROFX on Dec 18, 2008 | Reply

    Mau buat koneksi serial di gambas pake apa mas?
    Setauq MSComm’nya gambas,,itu apa?
    Mohon petunjuknya…
    Terima kasih…

  7. By arejae on Dec 31, 2008 | Reply

    ROFX, sorry yer saya kurang pasti tentang itu. Belum ada kesempatan untuk explore about that.

  8. By nakula on Jun 9, 2010 | Reply

    thx telah memberi gambaran dalam tugas ane …

  9. By victwo on Nov 9, 2010 | Reply

    excellent gambas2 mysql tutorial

  10. By austinium on Nov 30, 2010 | Reply

    Thanks

  1. 2 Trackback(s)

  2. Oct 21, 2008: Gambas - Can’t connect to local MySQL server through socket workaround | Arejae.Com
  3. Nov 9, 2008: Voorbeeld van mysql gebruik in Gambas at Gambas.noxqs.org

Post a Comment