Problema com select em tabela de informaçoes do mysql
-
Estou tentando executar o seguinte codigo:
@QSqlQuery *query = new QSqlQuery(*base);
QString sqlvars = "set @schema='%schema%';" "set @table='%table%';"; QString sqltables = "select TABLE_NAME, TABLE_TYPE from information_schema.TABLES where TABLE_SCHEMA = @schema;"; sqlvars.replace("%schema%","sakila"); bool ok = query->exec(sqlvars+sqltables); if(ok) while (query->next()) qDebug()<< query->value("TABLE_NAME").toString();@
mas ele não me retorna resultados e nao sei se posso fazer isso:
@ QString sqlvars = "set @schema='%schema%';"
"set @table='%table%';";@alguem?
-
Por que usar uma string e fazer replace se você pode fazer bind de variáveis?
@
QSqlQuery query;
query.prepare("select TABLE_NAME, TABLE_TYPE from information_schema.TABLES where TABLE_SCHEMA = :schema");
query.bindValue(":schema", "sakila");bool ok = query->exec();
if(ok)
while (query->next())
qDebug()<< query->value("TABLE_NAME").toString();
@ -
Foi mal pela demora.....
O "driver do mysql do qt":https://qt.gitorious.org/qt/qt/source/b510e64cc5405c0664200bf471b5ccbbe3b4d081:src/sql/drivers/mysql/qsql_mysql.cpp#L900 usa a função "mysql_stmt_prepare()":http://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-prepare.html que na sua documentação diz:
"The string must consist of a single SQL statement. You should not add a terminating semicolon (“;”) or \g to the statement."
Tradução livre minha:
"A string consiste de um simples SQL. Você não deve adicionar o terminador ";" ou \g."
Por isso que não funciona.