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. :)

If you're new here, you may want to subscribe to my RSS feed or get my latest post directly in your mailbox. Thanks for visiting !

Related Post:
  • Gambas - Can’t connect to local MySQL server through socket workaround
  • How to run Apache and MySql as a services
  • MySQL bought by Sun Microsystems
  • Ubuntu Hardy Heron FREE CD arrived
  • How to Setup Your Web Development Server in 7 Minutes



  • Can't find what you are looking for? Go Gooogle...
    Google

    6 Responses to “How to program Gambas with MySQL in Ubuntu”

    1. melayubuntu on October 5th, 2008 at 10:01 pm Using Mozilla Firefox Mozilla Firefox 3.0.3 on Ubuntu Linux Ubuntu Linux

      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

    2. arejae on October 8th, 2008 at 12:58 am Using Mozilla Firefox Mozilla Firefox 3.0.1 on Linux Linux

      aku pun takder explore jauh2..sket2 jer zul.

    3. Extremist Thinker on October 12th, 2008 at 2:44 pm Using Opera Opera 9.51 on Windows Windows XP

      ore kito. slmt ari rayooo. bereh!

    4. arejae on October 14th, 2008 at 3:04 pm Using Internet Explorer Internet Explorer 7.0 on Windows Windows XP

      bereh…selamat jugok ko demo.

    5. [...] I write about Gambas programming and my temporary solution for connecting to MySQL [...]

    6. [...] blog post geeft een volledig uitgewerkt voorbeeld, met code, data-beschrijving, en scherm: How to program Gambas with MySQL in Ubuntu De installatie is gericht op Ubuntu, maar het voorbeeld is voor alle Gambas gebruikers natuurlijk. [...]

    Leave a Reply


    AWSOM Powered