PDA

View Full Version : i want show all databases names in my project?



gmaa45
16th December 2009, 15:32
hi

i need to show all databases in my mysql engine

1-open database is ok,

QSqlQuery get_query(QSqlDatabase::database("MY_DB_1")); get_query.prepare("show databases");
get_query.exec(); // is ok
get_query.size(); // = -1 ??? i have many databases

but in mysql command 'cmd' , the query 'show databases' is run normal and show all databases;

what is the problem??

plase help me. ;)

Lykurg
16th December 2009, 16:50
Which database do you use? Some don't have the possibility to get the affected rows... Just try to print the names out and see if they appear:

while (get_query.next())
qWarning() << get_query.value(0).toString();

gmaa45
16th December 2009, 17:33
I have tried
but result no thing

example

1-open database is ok,

QSqlQuery get_query(QSqlDatabase::database("MY_DB_1"));
get_query.prepare("select * from students");
get_query.exec(); // is ok
get_query.size(); // = 23 :D
get_query.next(); // returned true

very nice
but

QSqlQuery get_query(QSqlDatabase::database("MY_DB_1"));
get_query.prepare("show databases");
get_query.exec(); // is ok
get_query.size(); // = -1 :(
get_query.next(); // returned false

but in mysql cmd the two cases is ok,

what is the problem in qt code in case 'show databases'? :confused:

toreo
17th December 2009, 08:58
I'm doing pretty much the same as you in a project, and the "show databases" statement works fine. Are you logging in as the same user both on the command line and in your Qt code? Could there be some issues with permissions? Performing a query and listing databases requires different grants in MySQL.

gmaa45
17th December 2009, 16:35
thanks
i am used root user , the root user have all rights
,plase show me example for grant statment for 'show databases' query.

toreo
18th December 2009, 12:21
This is described in MySQL's reference manual on the GRANT syntax. Basically you have to GRANT SHOW DATABASES ON mydb.* TO 'someuser'@'somehost';

gmaa45
19th December 2009, 10:59
thanks
I have tried
i have all rights in root user, and in cmd is all queries works in root user , the problem in qt code.

the queries 'select , insert , delete , create update' is works,
but 'show databases' and 'describe tb1' and 'show tables' not works in QSqlQuery Functions

Concluded
the Functions " size(), next() , first(), last(), value(int) " is works in select statment only.
in other words
the Previous functions is works in table data created from 'select query', but not works in table data created from 'show queries' and 'describe'.

Am I right?

Tanuki-no Torigava
19th December 2009, 12:47
Can you post the error you get? You can fetch the error using this:


QString someQuery = "select whatever you want;"
query.exec(someQuery);
if (query.lastError().isValid())
{
qDebug() << "Error: " << query.lastError().text()
<< " in query " << query.lastQuery();
// ...
}

gmaa45
19th December 2009, 13:23
hi

I tried

see
get_query2.prepare("show databases"); // returned 1 is ok
get_query2.exec(); // returned 1 is ok
get_query2.next(); // returned false ??
get_query2.size(); // -1 ??
QSqlError _sqlError = get_query2.lastError();
QString _error = _sqlError.text();
// _error now "" empty
the Functions " size(), next() , first(), last(), value(int) " is works in table data created from 'select query'.

ineed to 'show databases' an 'show tables' query. !!!!

Tanuki-no Torigava
20th December 2009, 04:08
It seems to me that select from system tables is the only way to get the list. Also you can check the code of any open source MySQL managers for a hint.

Regards,
-- tanuki

gmaa45
26th December 2009, 09:48
It seems to me that select from system tables is the only way to get the list. Also you can check the code of any open source MySQL managers for a hint.

Regards,
-- tanuki


May be this is solution
thanks for all

gmaa45
26th December 2009, 09:51
If i am found a solution will tell you