Autore Topic: hConnection = New Connection ([DatabaseURL As String])  (Letto 1548 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
hConnection = New Connection ([DatabaseURL As String])
« il: 26 Agosto 2013, 10:39:08 »
Riporto questa discussione apparsa nella M.L.I.:


" What syntax for "DatabaseURL As String"?

I'm trying to create a connection to an SQLite db in a different gambas
project source directory:

Codice: gambas [Seleziona]
$conn = New Connection("/share/projects/gambas3_proj/horse/horseBO/.connection/bometa.connection")

(and various other path strings to the connection definition).

All I get is "Malformed URL" error.

?
tia
Bruce
"


" This will allow you to open a connection anywhere. All you have to change is
   the "sName" and the .Host entry
   Enjoy!
   paul
Put these at the  beginning of your program on your form named fMain:

Codice: gambas [Seleziona]
Public $hConn As New Connection
   Public sMyPath As String
   Public sAppPath As String
   ' Your routine to open the connection:
   Public Sub Open_DB()
       ' If you want to see the commands sent to the database then uncomment this line:
       ' DB.Debug = True
          Print   "$hConn.Opened   from   fMain   Form  (s/b  F)  =  "  &
   Str($hConn.Opened)    ' For debug purposes only

        sName = "MyDatabaseName.sqlite"
        Print "sName = " & sName             ' For debug purposes only

        With $hConn
            .Type = "sqlite3"         ' Choose DB type for library database since users won't know anything
            .Host = "/share/projects/gambas3_proj/horse/
            .Name = sName             ' "MyDatabaseName.sqlite"
        End With

         sTryMessage  =  "OpenConn"       ' I had multiple kinds of catch statements. This is for debugging the opening of a connection
        Print "Try to open connection for use"
        Try $hConn.Open
          Print   "$hConn.Opened   from   fMain  Form  (s/b  T)   =  "  &
   Str($hConn.Opened)        ' For debug purposes only
        Print "Connection Host = " &
   $hConn.Host                                     ' For debug purposes only
        Catch
        If sTryMessage = "OpenConn" Then
            If errorMessageHeader = "" Then
                errorMessageHeader = "Could not open DB connection in fMain
   Form Open on "  ' & sName & " On " & sMyPath
                 Print "Connection Open Error in fMain Form Open " & Error.Text
                 Error.Raise(Error.Text & " in  fMain Form Open")
            End If
        Endif
   End
« Ultima modifica: 28 Agosto 2013, 11:59:41 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: hConnection = New Connection ( [ DatabaseURL As String ] )
« Risposta #1 il: 26 Agosto 2013, 12:46:05 »
...continua...


"
Codice: gambas [Seleziona]
$TYPE://$USER@$HOST:$PORT/$DB

where the :$PORT part is optional.
So to connect to something like

Codice: gambas [Seleziona]
hConn = New Connection
With hConn
  .Type = "mysql"
  .Login = "root"
  .Password = "toor"
  .Host = "localhost"
  .Port = 3306
  .Name = "test"
  .Open()
End With

you could write
Codice: gambas [Seleziona]
hConn = New Connection("mysql://root@localhost:3306/test")
hConn.Passwort = "toor"
hConn.Open()

Regards,
Tobi
"


" And then if you configure the connection ide you can use :
Codice: gambas [Seleziona]
Connections["Connection1"].Open()

Fabien Bodard "


" Bruce and Fabien,
   My  email a few days back gave the code that I am using to open sqlite
   databases. That code opens a db in any directory you have access to. Is
   there something I am not understanding about Bruce's question?

   paul
"


" Yes, Bruce wanted to know how the constructor of Connection works not how to
establish a connection.

Regards,
Tobi
"
« Ultima modifica: 28 Agosto 2013, 12:25:00 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: hConnection = New Connection ([DatabaseURL As String])
« Risposta #2 il: 28 Agosto 2013, 14:01:02 »
...continua...


" First off, thanks everyone for your answers - all are correct.

My original question was purely about the syntax of the Connection
constructor, specifically about the optional DatabaseURL parameter.

Clarification 1: According to the help there are three ways to
initialise a connection:
a) by directly setting the connection properties, (as Paul describes)
b) by using a predefined connection, (as Fabian describes)
c) by using the constructor DatabasURL parameter (as Tobi describes).

Regarding the latter, the help is somewhat silent as to the syntax of
that parameter.  Thanks to Tobi, I now see it to be:

Codice: gambas [Seleziona]
$TYPE://$USER@$HOST:$PORT/$DB

Clarification 2: This syntax also provides a clue that this method can
only work where the underlying database is a server process e.g. mysql
or postgresql and I don't think it can work for sqlite.

In fact, contrary to Tobi's experience, I still cannot get this to work
(for a postgresql database).  Here I have a server machine "bluecow"
that has three postgresql server processes running on it (called say
"prod", "dev" and "test" each using a separate port). So, if I
understand Tobi's answer:

Codice: gambas [Seleziona]
$conn = New Connection("postgresql://bb@bluecow:5432/horse")

where the database name is "horse" and the dev server is using port
5432, should create a valid connection, but I still get this "Malformed
URL" error???

I have no problem connecting to said database using the direct or
predefined methods. Further, looking at the CConnection.c code all the
constructor appears to do is split the string parameter and populate the
connection properties. It does not appear to validate the values
provided for those properties. In other words, I would expect that
something like:

Codice: gambas [Seleziona]
$conn = New Connection("badtype://xyz@noserver:15432/nosuchdb")

should still work (albeit creating an invalid connection). However, this
still gives the malformed URL error.

Clarification 3: Fabian was able to read between the lines and provide
the clue to my real problem. For predefined connections, i.e. the ones
defined in the IDE, the proper way to set up the connection is:

Codice: gambas [Seleziona]
$conn = Connections["conn_name"]

In other words, the key is just the name of the connection as seen in
the IDE.  This works fine for connections defined in the current
project.

FINALLY(!) here's my real problem.

What I am trying to do is use a predefined connection in a different
gambas project.  In other words, I want to get at the
".../otherproject/.connection/someconn.connection" file inside another
gambas project.

Why would anyone in their right mind want to do that?  Well...
I have a library that is used by "many" apps that manages database
access.  The library is database independent, i.e. it does not know the
connection parameters that are to be used.  The apps define the
database. In other words, App1.gambas uses "this" database and
App2.gambas uses "that" database. So I need the library to be able to
use the connection files in the particular "AppX" project.  (This is
simplifying things a bit as when we get to executable stage, the
connection is defined inside the executable archive. But I have solved
that part of the problem by copying the file into the runtime /tmp
directory.)

I suppose the final answer is that I need to read the /tmp/.../xyz.
connection file and create the connection using the direct method.  What
I was hoping for was a way to do something like:

Codice: gambas [Seleziona]
sConnFile = File.Path(Temp()) &/ "connmain.connection"
        $conn = Connections[sConnFile]

In other words:
Codice: gambas [Seleziona]
Connections[path as String]

but it looks like this is not possible.

Bruce
"


" It does work like $TYPE://$PATH because for sqlite, the Host part is a
directory and the DB is a file relative to the directory. The parser takes
the last part of the string (after the last /) as the DB name, the stuff
before as Host. It seems like the $USER path is also optional... I just
tested it:

Codice: gambas [Seleziona]
$hConn = New Connection("sqlite3:///home/tab/Kontakte.sqlite")

I just installed postgresql and the following works. I followed the Arch
Linux wiki article for setting it up (created the DB user 'root') and the
following does indeed work (with the 'test' database created via Database
example of Gambas):

Codice: gambas [Seleziona]
$hConn = New Connection("postgresql://root@localhost:5432/test")

I don't know what's wrong on your side, sorry...

Regards,
Tobi
"
« Ultima modifica: 28 Agosto 2013, 15:46:12 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: hConnection = New Connection ([DatabaseURL As String])
« Risposta #3 il: 29 Agosto 2013, 13:54:13 »
...continua...


" Well, I finally grasped at the last possible straw and made a complete
new copy of the gb source repository, compiled and installed it and now
it works just like it should.

? ? ? ?

The thing that is   r e a l l y  weird is that all the sources are
exactly the same.  Something strange with the make is all I can guess.

thanks for the info anyway (and also for the help update!)

Bruce
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »