Hi,

Am 12.09.2014 um 10:52 schrieb Tobias Boege:
>> Grmbl.
> 
> If you have time, of course! Or do what you want. It was just a quick idea.

:-)

Had a nice afternoon updating some machines / waiting for the next
question / programming your thing.

>>> Have you tested that? Do you have a patch for the project?
>>
>> No. I don't even know how to create a patch for Gambas.
> 
> Not for Gambas. For my Gambas project only. This is pretty easy *slap on
> my back*. You have a source archive of the old project, right? Then you
> uncompress it and change the project. In the IDE menu: Project -> Patch
> -> Create, a wizard will guide you through the creation of a patch for a
> Gambas project. The result is what you changed in the currently opened
> project from the source archive you select in the wizard.

Ok, the patch is attached. It works on my Mysql-server (but also there
are some hangs, maybe that was, what Benoit fixed). I did a module which
contains the connection to db together with the credentials. One could
fetch them from Settings there.

If you have a look at the connection, you will see, it is not closed als
long as the app lives.

It is persistant and u can reuse it in different places of your app.

I personally always work with results, that are defined in the
functions, and fill the controls with the data of the results which are
queried via this one connection.

This one persistant connection (per DB) ensures, that you will not do
something ugly where two parts of your application fiddle with the same
data via different connections. This is especially important, when your
app grows ...

I did and do not like the code, that creates the database, it injects
unneccessary complexity.


Alles Gute

Christof Thalhofer

-- 
[x] nail here for new monitor

diff -urNaX /tmp/gambas-patch-ignore a/.project b/.project
--- a/.project	2014-09-11 19:56:55.000000000 +0200
+++ b/.project	2014-09-12 19:58:23.000000000 +0200
@@ -1,5 +1,5 @@
 # Gambas Project File 3.0
-# Compiled with Gambas 3.5.90
+# Compiled with Gambas 3.5.4
 Title=Meine Adressen
 Startup=FMain
 Icon=Symbols/projekt_icon.png
@@ -16,4 +16,5 @@
 Authors="Dr. Hans Lehmann ~ Osterburg ~ 2009-2014"
 TabSize=2
 SourcePath=/tmp
+License=Other License(s)
 Packager=1
diff -urNaX /tmp/gambas-patch-ignore a/.src/FMain.class b/.src/FMain.class
--- a/.src/FMain.class	2014-09-11 19:44:21.000000000 +0200
+++ b/.src/FMain.class	2014-09-12 19:56:16.000000000 +0200
@@ -1,76 +1,84 @@
 ' Gambas class file
 
+'reuse this connectin within the form
+Private con As Connection
+
 Public Sub Form_Open()
+
+  'initialize one time
+  'you can do this in other forms
+  'also and have only one connection
+  'to db "Kontakte" as long as the app lives
+  con = MDataBase.DBKontakte
+
   FMain.Center
   FMain.Resizable = False
-  
-  MDataBase.ConnectDB("mysql", "localhost", "3606", "test", "", "Kontakte", "kontakte")
-  DataSource1.Connection = MDataBase.cDBVerbindung
+
+  DataSource1.Connection = con
   DataSource1.Table = "kontakte"
-' DataSource1.Filter = "Wohnort <> 'Leipzig' AND Wohnort NOT LIKE 'G%'" ' "Wohnort <> \"Leipzig\""
+
+  ' DataSource1.Filter = "Wohnort <> 'Leipzig' AND Wohnort NOT LIKE 'G%'" ' "Wohnort <> \"Leipzig\""
   DataSource1.Filter = "" ' Standard-Filter ---> ohne Filterung
   DataSource1.Sort = "PLZ"
-  
+
   DataSource1.ReadOnly = True
   DataBrowser1.Editable = False
-  
+
   ' Print DataSource1.Connection.Charset
-  
+
 End ' Form_Open
 
 Public Sub Form_Show()
-  Dim sSQL_Anweisung As String
+
+  Dim qry As String
   Dim r As Variant
-  
- 
+  Dim res As Result
+
   DataBrowser1.Orientation = Align.Bottom
   DataBrowser1.Columns = ["Vorname", "Nachname", "Wohnort", "PLZ", "Strasse", "TelefonFestnetz"]
   DataBrowser1.Labels = ["Vorname", "Nachname", "Ort", "Postleitzahl", "Straße", "Festnetz-Nummer"]
-  
+
   DataBrowser1.View.Columns[0].Width = 100
   DataBrowser1.View.Columns[1].Width = 100
   DataBrowser1.View.Columns[2].Width = 110
   DataBrowser1.View.Columns[3].Width = 95
   DataBrowser1.View.Columns[4].Width = 130
   DataBrowser1.View.Columns[5].Width = 70
-  
+
   dcVorname.Field = "Vorname"   ' DB-Feld (datensensitiv)
-  dcNachname.Field = "Nachname" 
+  dcNachname.Field = "Nachname"
   cdWohnort.Field = "Wohnort"
   dcPLZ.Field = "PLZ"
   dcStrasse.Field = "Strasse"
   dcTelefonFestnetz.Field = "TelefonFestnetz"
-  
-  sSQL_Anweisung = "SELECT * FROM " & DataSource1.Table
-  MDataBase.rDBResult = MDataBase.cDBVerbindung.Exec(sSQL_Anweisung)
-    
+
+  qry = "SELECT * FROM " & DataSource1.Table
+  res = con.Exec(qry)
+
   DataBrowser1.Editable = True
   'DataBrowser1.CanDelete = False
-  If MDataBase.rDBResult.Available Then
-     DataBrowser1.View.MoveTo(0, 0)
+  If res.Available Then
+    DataBrowser1.View.MoveTo(0, 0)
   Endif
-  'DataBrowser1.Editable = True  ' ---> letzte Zeile
-  
-'   Print DataBrowser1.View.Columns.Count
- 
-  ' DataCheckBox1.Text = "Fahrschüler"
-  ' DataCheckBox1.Value = True ' hier müsste ein Boolean-Feldname stehen
-  ' Print DataCheckBox1.Valid
-
 
 End ' Form_Show
 
 Public Sub Form_Close()
-  MDataBase.DBVerbindungSchliessen()
+
+  MDataBase.DeleteDBKontakte()
+
 End ' Form_Close
 
 Public Sub btnEnde_Click()
+
   FMain.Close
+
 End ' btnEnde_Click()
 
 Public Sub Button1_Click()
+
   Dim bFlag As Boolean
-  
+
   ' bFlag = DataSource1.Remove()
   ' Print bFlag
   If DataSource1.Remove() = False Then Print "LÖSCHEN ERFOLGREICH!"
@@ -79,8 +87,6 @@
 
 Public Sub Button2_Click()
 
-  
-  DataSource1.MoveNext() 
-  
+  DataSource1.MoveNext()
 
 End
diff -urNaX /tmp/gambas-patch-ignore a/.src/MDataBase.module b/.src/MDataBase.module
--- a/.src/MDataBase.module	2014-09-11 19:37:28.000000000 +0200
+++ b/.src/MDataBase.module	2014-09-12 19:50:48.000000000 +0200
@@ -1,92 +1,128 @@
 ' Gambas module file
 
-Public rDBResult As Result
-Public cDBVerbindung As New Connection
-Public sDatenbankTyp As String
+''' Singleton, keeps one connection per DB permanent open
+''' allows more than one DB
+'''
+'''
+
+Public DBKontakte As New Connection
+
+Public Sub _init()
+
+  StartDBKontakte()
+  'maybe you want to start not only one db?
+  'here you could start and access some more
+  'you could even generate a factory-pattern
+  'for starting lots of dbs in this module ;-)
+
+End
+
+Private Sub StartDBKontakte()
+
+  Dim DBType As String, DBHost As String, DBPort As String
+  Dim DBUserName As String, DBUserPassword As String, DBName As String
+
+  DBType = "mysql"
+  DBHost = "localhost"
+  DBPort = "3306"
+  DBUserName = "test"
+  DBUserPassword = ""
+  DBName = "Kontakte"
+
+  'Useless:
+  ' If Upper(DBType) = "SQLITE3" And Not Exist(DBHost) Then
+  '   Try Mkdir DBHost
+  ' Endif
+
+  'We keep one connection open
+  'for reuse and do not close until end of app
+  'DBKontakte.Close()
+
+  If Not DBKontakte.Opened Then
+
+    DBKontakte.Type = Lower(DBType) ' Der Datenbank-Typ muss klein geschrieben werden
+    DBKontakte.Host = DBHost
+    DBKontakte.Port = DBPort
+    DBKontakte.User = DBUserName
+    DBKontakte.Password = DBUserPassword
+    DBKontakte.Name = DBName ' Das ist der Datenbank-Name
+
+    ' Versuche, eine Verbindung zur DB herzustellen
+    Try DBKontakte.Open()
+    If Error Then
+      'wenns nicht hinhaut, db erstellen
+      initDBKontakte("kontakte", DBName)
+    Endif
+
+  Endif
+
+End
+
+Private Sub initDBKontakte(DBTabellenName As String, DBName As String) As Boolean
 
-Public Sub ConnectDB(DBType As String, DBHost As String, DBPort As String, DBUserName As String, DBUserPassword As String, DBName As String, DBTabellenName As String)
   Dim DBTabelle As Table
   Dim sSQL_Anweisung As String
-  
-  If Upper(DBType) = "SQLITE3" And Not Exist(DBHost) Then
-     Try Mkdir DBHost
-  Endif
-  
-  cDBVerbindung.Close()
-  cDBVerbindung.Type = Lower(DBType) ' Der Datenbank-Typ muss klein geschrieben werden
-  cDBVerbindung.Host = DBHost
-  cDBVerbindung.Port = DBPort  
-  cDBVerbindung.User = DBUserName  
-  cDBVerbindung.Password = DBUserPassword
-  cDBVerbindung.Name = DBName ' Das ist der Datenbank-Name
-
-' Versuch, eine DB-Verbindung zur DB auf dem DB-Server herzustellen
-  Try cDBVerbindung.Open()
-  
+
+  'testen, ob Server antwortet
+  DBKontakte.Name = ""
+  Try DBKontakte.Open
   If Error Then
-   ' Prüfen, ob die Datenbank mit dem angegebenen Namen existiert
-     If Not cDBVerbindung.Databases.Exist(DBName) Then
-        Message.Info("Die Datenbank " & DBName & " existiert nicht und wird angelegt!")
+    Message.Error("Der Server antwortet nicht")
+    Return False
+  Else
+    ' Prüfen, ob die Datenbank mit dem angegebenen Namen existiert
+    If Not DBKontakte.Databases.Exist(DBName) Then
+      Message.Info("Die Datenbank " & DBName & " existiert nicht und wird angelegt!")
       ' Datenbank genau dann anlegen, wenn sie NICHT existiert
-        cDBVerbindung.Databases.Add(DBName)
-        cDBVerbindung.Name = DBName
-        Try cDBVerbindung.Open()
-        If Error Then
-           Message.Info("Eine DB-Verbindung zum Server konnte NICHT hergestellt werden!")
-           Error.Raise(Error.Text)
-        Endif
-     Endif
-     ' Message.Error("Eine DB-Verbindung zum DB-Server konnte NICHT hergestellt werden!")
-     ' Return
-  Endif
-  
-  ' sSQL_Anweisung = "DROP TABLE `kontakte`" 
-  ' cDBVerbindung.Begin
-  '   cDBVerbindung.Exec(sSQL_Anweisung)
-  ' cDBVerbindung.Commit
-  
-  
-  
-' Prüfen, ob die Tabelle mit dem angegebenen Namen in der aktuellen DB existiert
-' Tabelle genau dann anlegen, wenn sie NICHT existiert  
-  If Not cDBVerbindung.Tables.Exist(Lower(DBTabellenName)) Then
-     Message.Info("Die DB-Tabelle <b>'" & DBTabellenName & "'</b> existiert nicht und wird angelegt!")
-   ' Leere Tabelle mit dem angegebenen Tabellen-Namen anlegen
-     DBTabelle = cDBVerbindung.Tables.Add(DBTabellenName, "InnoDB")
-          
-   ' Tabellenfelder definieren: 
-     DBTabelle.Fields.Add("Id", db.Serial)
-     DBTabelle.Fields.Add("Vorname", db.String, 16)
-     DBTabelle.Fields.Add("Nachname", db.String, 24) ' Nachname ---> Text mit 24 Zeichen
-     DBTabelle.Fields.Add("Wohnort", db.String, 32)
-     DBTabelle.Fields.Add("PLZ", db.String, 5)
-     DBTabelle.Fields.Add("Strasse", db.String, 24)
-     DBTabelle.Fields.Add("TelefonFestnetz", db.String, 16)
-     
-     DBTabelle.PrimaryKey = ["Id"]
-
-     DBTabelle.Update()
-     cDBVerbindung.Tables.Refresh ' Liste der Tabellen in der DB erneuern
-  
-     sSQL_Anweisung = "INSERT INTO `kontakte` VALUES " 
-     sSQL_Anweisung &= "('1','Arno','Adler','Arneburg','39606','Am Hafen 3','03937864322'),"
-     sSQL_Anweisung &= "('2','Bruno','Bär','Berlin','10404','Bode-Strasse 1','03094157777'),"
-     sSQL_Anweisung &= "('3','Gerda','Geier','Gera','07997','Gartenweg 23','03657788989'),"
-     sSQL_Anweisung &= "('4','Lutz','Lama','Leipzig','04103','Lessing-Allee 5','0641432222'),"
-     sSQL_Anweisung &= "('5','Maria','Meise','München','80805','Malergasse 10','0867554324'),"
-     sSQL_Anweisung &= "('6','Wolf','Walter','Werra','06766','Waldweg 6a','036922123');"
-     
-     cDBVerbindung.Begin
-       cDBVerbindung.Exec(sSQL_Anweisung)
-     cDBVerbindung.Commit
-  
+      DBKontakte.Databases.Add(DBName)
+      DBKontakte.close
+      DBKontakte.Name = DBName
+      DBKontakte.Open()
+    Endif
+
+    ' Prüfen, ob die Tabelle mit dem angegebenen Namen in der aktuellen DB existiert
+    ' Tabelle genau dann anlegen, wenn sie NICHT existiert
+    If Not DBKontakte.Tables.Exist(Lower(DBTabellenName)) Then
+      Message.Info("Die DB-Tabelle <b>'" & DBTabellenName & "'</b> existiert nicht und wird angelegt!")
+      ' Leere Tabelle mit dem angegebenen Tabellen-Namen anlegen
+      DBTabelle = DBKontakte.Tables.Add(DBTabellenName, "InnoDB")
+
+      ' Tabellenfelder definieren:
+      DBTabelle.Fields.Add("Id", db.Serial)
+      DBTabelle.Fields.Add("Vorname", db.String, 16)
+      DBTabelle.Fields.Add("Nachname", db.String, 24) ' Nachname ---> Text mit 24 Zeichen
+      DBTabelle.Fields.Add("Wohnort", db.String, 32)
+      DBTabelle.Fields.Add("PLZ", db.String, 5)
+      DBTabelle.Fields.Add("Strasse", db.String, 24)
+      DBTabelle.Fields.Add("TelefonFestnetz", db.String, 16)
+
+      DBTabelle.PrimaryKey = ["Id"]
+
+      DBTabelle.Update()
+      DBKontakte.Tables.Refresh ' Liste der Tabellen in der DB erneuern
+
+      sSQL_Anweisung = "INSERT INTO `kontakte` VALUES "
+      sSQL_Anweisung &= "('1','Arno','Adler','Arneburg','39606','Am Hafen 3','03937864322'),"
+      sSQL_Anweisung &= "('2','Bruno','Bär','Berlin','10404','Bode-Strasse 1','03094157777'),"
+      sSQL_Anweisung &= "('3','Gerda','Geier','Gera','07997','Gartenweg 23','03657788989'),"
+      sSQL_Anweisung &= "('4','Lutz','Lama','Leipzig','04103','Lessing-Allee 5','0641432222'),"
+      sSQL_Anweisung &= "('5','Maria','Meise','München','80805','Malergasse 10','0867554324'),"
+      sSQL_Anweisung &= "('6','Wolf','Walter','Werra','06766','Waldweg 6a','036922123');"
+
+      DBKontakte.Begin
+      DBKontakte.Exec(sSQL_Anweisung)
+      DBKontakte.Commit
+    Endif
+    Return True
   Endif
-    
+
 End ' ConnectDB()
 
-Public Sub DBVerbindungSchliessen()
-  If cDBVerbindung.Opened Then
-     Try cDBVerbindung.Close()
-     If Error Then Message.Error("Fehler bei Trennen der DB-Verbindung zu " & sDatenbankTyp)
+Public Sub DeleteDBKontakte()
+
+  If DBKontakte.Opened Then
+    Try DBKontakte.Close()
+    If Error Then Message.Error("Fehler bei Trennen der DB-Verbindung zu Mysql")
   Endif
+
 End

Attachment: signature.asc
Description: OpenPGP digital signature

------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Gambas-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to