Page 1 of 1

Switch Database with ZConnection.Database

Posted: 27.02.2006, 21:44
by anse123
Hello,

I have a ZConnection1 and a ZQuery1 but I can only define the Database once.

Code: Select all

ZConnection1.Database := 'test1';
ZQuery1.SQL.Add('SELECT * FROM table1');
ZQuery1.Open(); // This runs as expected!
ZQuery1.Close();
ZConnection1.Database := 'test2';
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('SELECT * FROM table2'); // no error till here
ZQuery1.Open(); // This returns "SQL-error: Table test1.table2 does not exist"
Shouldn't the ZConnection have recognized that I want to select the table2 in database2, not in database1 ?

Greetings,
Anse

Posted: 28.02.2006, 00:12
by zippo

Code: Select all

..
ZQuery1.Open(); // This runs as expected!
ZQuery1.Close();
ZConnection1.connected := false;
ZConnection1.Database := 'test2';
ZConnection1.connected := true;
ZQuery1.SQL.Clear;
..
But if you are using a SQL database (like MySQL), then you can just execute an SQL command "Use test2", and that's it.

Code: Select all

ZConnection1.Database := 'test1';
ZQuery1.SQL.Add('SELECT * FROM table1');
ZQuery1.Open(); // This runs as expected!
ZQuery1.Close();
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('USE test2'); // nthis switches to another DB
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('SELECT * FROM table2'); // no error till here
ZQuery1.Open(); // This returns "SQL-error: Table test1.table2 does not exist" 

easyy....

Posted: 28.02.2006, 08:36
by anse123
Ok, thanks a lot. That was too easy :)
I always thought, setting the database-property would automatically do a "USE dbname" ...

Greetings,
Anse