[Gambas-user] gambas and the update method

mike webb mwebb at ...1362...
Sun Mar 19 16:30:44 CET 2006


please excuse my poor coding, i'm still coming to grips with gambas.
also i'm really hoping i didn't forget to tell you about an inportant 
part that you'll need to know just to
get the program to run.
consider button2_click the code goes as follows:

PUBLIC SUB Button2_Click()
    currow = tableview1.Row
    mglobal.rs1["id"] = Val(textbox1.Text)
    mglobal.rs1["name"] = textbox2.Text
    mglobal.rs1.update
    mglobal.rs1 = mglobal.db.edit("user","","")
    tableview1.Refresh
END

it seems to me that the
mglobal.rs1.update
should have taken care of the database and the result set.
but to make the program work i have to include
mglobal.rs1 = mglobal.db.edit("user","","")
try commenting out the line
mglobal.rs1 = mglobal.db.edit("user","","")
and run the program again, see what i mean, nothing got updated.
note that i have to do this anytime the result set is changed.

is their a way to make the "update" method more powerful so that
we don't have to include the "edit" method ???
looks to me like the result set and the database aren't staying inline.
i realize that their are other ways of acheiveing this desired results 
that don't require the "update"
method at all, but i would like to use this method.

the following little ditty was written to highlight my point about the 
"update" method.
it will one day be used in my top secert project "gambas on rails"
you may use it anyway you might want.
if you improve on it though could you send me the improvments ??
email address is mwebb at ...1362...


to run this program you need the following:
the gbstest database with some names in the user table.
you'll need to create a form and a module
create a blank form and add the following,
3 buttons called button1,button2,button3
change the following properties for the buttons
button1.text = delete
button2.text = edit
button3.text = add

1 tableview called tableview1
2 textboxes called textbox1,textbox2
save the form as form1
copy the following into form1 class

' Gambas class file
PUBLIC currow AS Integer
PUBLIC oldbackground AS Integer

PUBLIC SUB Form_Open()
    DIM i AS Float
    IF mglobal.OpenDB("gbstest") = TRUE THEN
        tableview1.columns.count=2
        tableview1.columns[0].text="ID"
        tableview1.columns[1].text="name"
        tableview1.Columns[0].Width = 75
        tableview1.Columns[1].Width = 360
        mglobal.rs1 = mglobal.db.edit("user","","")
        tableview1.rows.count=mglobal.rs1.count
        currow = tableview1.Row
    ELSE
        message.Info("unable to open db")
    ENDIF      
END
PUBLIC SUB TableView1_Data(Row AS Integer, Column AS Integer)
    mglobal.rs1.MoveTo(Row)
    IF row = currow THEN
        oldbackground = tableview1.Data.Background
        TableView1.Data.Background = &HF0EEFF&
        textbox1.Text = Str(mglobal.rs1["id"])
        textbox2.Text = Str(mglobal.rs1["name"])
    ELSE
        TableView1.Data.Background = oldbackground
    ENDIF
    tableview1.data.Text = Str(mglobal.rs1[tableview1.columns[column].text])
    mglobal.rs1.Moveto(currow)
END
PUBLIC SUB TableView1_Click()
    tableview1_rowclick(tableview1.Row)
END
PUBLIC SUB TableView1_RowClick(Row AS Integer)
    tableview1.Row = row
    currow = row    
    tableview1.Refresh
END
PUBLIC SUB Button2_Click()
    currow = tableview1.Row
    mglobal.rs1["id"] = Val(textbox1.Text)
    mglobal.rs1["name"] = textbox2.Text
    mglobal.rs1.update
    mglobal.rs1 = mglobal.db.edit("user","","")
    tableview1.Refresh
END
PUBLIC SUB Button1_Click()
    currow = tableview1.Row
    mglobal.rs1.delete  
    mglobal.rs1.update
    mglobal.rs1 = mglobal.db.edit("user","","")
    tableview1.rows.count=mglobal.rs1.count
    tableview1.Refresh
END
PUBLIC SUB Button3_Click()
    currow = tableview1.Row
    mglobal.rs1 = mglobal.db.create("user")
    mglobal.rs1["id"] = Val(textbox1.Text)
    mglobal.rs1["name"] = textbox2.Text
    mglobal.rs1.update
    mglobal.rs1 = mglobal.db.edit("user","","")
    tableview1.rows.count=mglobal.rs1.count
    tableview1.Refresh
END

'****************************************
create a module and save it as mglobal
then copy this into the module
'****************************************

' Gambas module file

PUBLIC db AS NEW Connection
PUBLIC rs1 AS Result

PUBLIC FUNCTION OpenDB(dbname AS String) AS Boolean
IF mglobal.conn("localhost","root","",dbname) = TRUE THEN  
    RETURN TRUE
ELSE
    RETURN FALSE
ENDIF
END

PUBLIC FUNCTION conn(localhost AS String, mylogin AS String,mypassword 
AS String, mydbname AS String) AS Boolean
  db.close
  db.Type = "mysql"
  db.Host = localhost
  db.Login = mylogin
  db.Password = mypassword
  db.name = mydbname
  TRY db.Open
  IF ERROR THEN
    PRINT "cannot open database. error = "; error.Text
    RETURN FALSE
  ENDIF
  RETURN TRUE
CATCH
  Message.error(Error.text)
  RETURN FALSE
END


in a function in mglobal called openDB you may have to change your sql 
login name or password.
gezz i hope i didn't forget anything

 





More information about the User mailing list