Switch Database with ZConnection.Database

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
anse123
Junior Boarder
Junior Boarder
Posts: 26
Joined: 23.02.2006, 22:28

Switch Database with ZConnection.Database

Post 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
zippo
Silver Boarder
Silver Boarder
Posts: 322
Joined: 12.10.2005, 18:01
Location: Slovenia

Post 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" 
anse123
Junior Boarder
Junior Boarder
Posts: 26
Joined: 23.02.2006, 22:28

easyy....

Post 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
Post Reply