[Gambas-user] How do I get the database name from Connection.Databases

Timothy Marshal-Nichols timothy.marshal-nichols at ...247...
Tue Apr 18 14:11:59 CEST 2006


Listing the databases on a connection and tables in a database is not as complex as you make out.  But a quick example may make things easier to understand.

PRIVATE SUB ListDatabases()
  DIM c AS NEW Connection
  DIM d AS Database
  c.Type = "mysql"
  c.Host = "localhost"
  c.Name = "" ' No need to set database name
  c.Login = "mysql"
  c.Password = "password"
  TRY c.Open
  IF ERROR THEN
    Message.Warning("Could not find databases\n\n" & ERROR.Text)
    RETURN
  END IF
  Listbox1.Clear()
  FOR EACH d IN c.Databases
    PRINT d.Name
    Listbox1.Add(d.Name)
  NEXT
  c.Close
END

PRIVATE SUB ListTables()
  DIM c AS NEW Connection
  DIM t AS Table
  c.Type = "mysql"
  c.Host = "localhost"
  c.Name = "GambasExample"
  c.Login = "mysql"
  c.Password = "password"
  TRY c.Open
  IF ERROR THEN
    Message.Warning("Could not find tables\n\n" & ERROR.Text)
    RETURN
  END IF
  Listbox1.Clear()
  FOR EACH t IN c.Tables
    PRINT t.Name
    Listbox1.Add(t.Name)
  NEXT
  c.Close
END

Thanks

8-{)} Timothy Marshal-Nichols
<mailto: timothy.marshal-nichols at ...247...>


-----Original Message-----
From: gambas-user-admin at lists.sourceforge.net
[mailto:gambas-user-admin at lists.sourceforge.net]On Behalf Of ron
Sent: Sunday, 16 April 2006 20:18
To: gambas-user at lists.sourceforge.net
Subject: [Gambas-user] How do I get the database name from
Connection.Databases



I want to get the names of the databases on a connection

dim hConn as connection

set user,pass etc and hConn.open

now I should be able to get from hConn.Dataases the names
but how ?

The local help says only: PROPERTY Databases AS Object

The website says:
-----------
Connection.Databases (gb.db)

PROPERTY Databases AS .ConnectionDatabases
-----------
the link: .ConnectionDatabases says:

.ConnectionDatabases (gb.db)
 This is a collection of all databases managed by the database server. 
 You may not see every database if the user you used to connect to the 
 server do not have sufficient rights. 

 This class inherits .SubCollection in gb - Gambas Internal native classes.

	In the link to 'gb - Gambas Internal native classes' 
	there is no sign of SubCollection ?

This class is enumerable with the FOR EACH keyword.
	Properties: Count & length
	Method: Add, Exist & Remove
note: bit stupid to generate keys for Exist() of 1...n char of a-zA-Z0-9 
-----------

for .SubCollection (gb) I get
  This virtual class is the parent class of every collection of sub-objects.
	Properties: Count & length
	Method: Exist

This class is enumerable with the FOR EACH keyword.
-----------

Both tell me the FOR EACH but how to do that excact?

FOR EACH Variable IN Expression
	Expression must be a reference to an enumerable object: for example, 
	a collection, or an array.

    The Expression is  .ConnectionDatabases or .SubCollection
    The Variable must be what kind of datatype if any

FOR EACH Expression
	This syntax must be used when Expression is a enumerable object that 
	is not a real container: for example, the result of a database query.

    FOR EACH hconn.Databases
      print hconn.Databases[key] ' the key is the thing I want
    NEXT

FOR Variable = Expression TO Expression [ STEP Expression ]

    FOR x = 0 TO hconn.Databases.length - 1
       print "database name=" & ????? hconn.Databases[x]
    NEXT

In hconn.Databases[x] x must be the key as string
If it is the name then it does not work, I don't know the names
and just want them.
If x can be a integer wich property tell me the name for that key as index?

Currently I do

PRIVATE SUB FillListBox(sql AS String, obj AS ListBox)
  DIM res AS Result 
  
  res = hconn.Exec(sql)
  WITH obj  
    .Clear
    WHILE res.Available
      .Add(res[0])
      res.MoveNext
    WEND 
  END WITH
END


PUBLIC SUB ListBoxDBUpdate()
  DIM sql AS String
  
  sql = "show databases"
  FillListBox(sql, ListboxDB)
END  

As work arround the res = hconn.Exec(sql).
However I should not use the .Exec as told in the help.

if hConn.Databases returns a String[] and the list property
of the listbox ask for a String[] the next line solved everything


ListBox1.list = hConn.Databases

For ListBox.List
	PROPERTY List AS String 

  Returns or sets the ListBox contents with a string. 
  Each element in the string must be delimited by a newline character. 

ListBox1.list = hConn.Databases.Join("\n")

ListBox1.list

So my request is what does Connection.Databases give me in real live.
In fact the Connection.Tables is the same problem.

----
mfg Ron




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user






More information about the User mailing list