[Gambas-user] How do I get the database name from Connection.Databases
ron
ronstk at ...239...
Wed Apr 19 01:49:05 CEST 2006
On Tuesday 18 April 2006 14:11, Timothy Marshal-Nichols wrote:
>
> 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
----8<---
OK, I do have more or less the same now.
A explanation of the problem.
The internal help says: PROPERTY Databases AS Object <single>
The website says: PROPERTY Databases AS .ConnectionDatabases <multiple>
and for .ConnectionDatabases it inherits .SubCollection
None of them have the .Name property.
For the property name it hints for multiple Database objects.
DIM myDBs as Databases ' Databases does not exist
DIM myDBs as Database[] ' Database[] is wrong
myDBS = hConn.Databases 'not allowed for virtual class
myDB =myDBs[index] \__ can't be done
Print myDB.Name /
myDB =hConn.Databases[index] 'OK
Print myDB.Name 'OK
You can't assing a variable with the content of a vitual class but
can use the construction 'hConn.Databases' as the variable
DIM db AS Database
db = hConn.Databases[sKey]
Now here the sKey is a String, say the help, I asume here it is the name
of the Database to use as in other implementations/languages is done.
The help would be more correct if told indexed by a String or Integer.
If it is an index _number_ in the array it would/should be
db = hConn.Databases[iIndex]
If I must use DIM myDB as Object then
print myDB.???? , the .Name does not exist for object
Using Object for variable names is with the completion not good
working by design :)
Because I did not get a clear (data)type for Databases I did
DIM v as Variant
v=hConn.Databases[sKey]
Using single step with gambas and a breakpoint at the v=hCo...
I was able to see clearly that v was changed from Variant to Database.
Now I know what I can use for the Properties/Methods/Events of the object
under test and in this case a Database (no events (yet)).
The missing point here is the explanation for .ConnectionDatabases and the Object.
PROPERTY Databases AS .ConnectionDatabases
The .ConnectionDatabases consists of a .Subcollection of multiple Database
objects and a manager to add and remove a Database to the .SubCollection.
.ConnectionDatabases AS Object
|
+--- the .SubCollection of 1 or more Database objects
| |
| --- .Exists()
|
---- and manager to .Add and .Remove from .SubCollection
For using a database you can use hConnection.DataBase[sKey as String]
or the FOR EACH xx in hConnection.Databases method.
Examples
DIM myDB as Database
myDB = hConnection.Database[sKey]
DIM yourDB as Database
FOR EACH yourDB in hConnection.Databases
Print yourDB.Name
'calling a subroutine ShowTables(yourDB.Tables)
NEXT
Anyway thanks for the example as it can still be usefull for others to.
----
Ron
More information about the User
mailing list