|
我编写的QT中执行sql语句的部分代码如下:
QString ied_info_sql = " with recursive vl(cb, appid, pied, pp, sied, sp, cable, level) as ( ";
ied_info_sql.append(" select v.cb, v.appid, v.pub_ied, v.pub_port, v.sub_ied, v.sub_port, v.cable, 0 ");
ied_info_sql.append(" from s_vir_link as v ");
ied_info_sql.append(" where v.sub_ied = '"+ sub_ied +"' and appid = '"+appid+"' union ");
ied_info_sql.append(" select v.cb, v.appid, v.pub_ied, v.pub_port, v.sub_ied, v.sub_port, v.cable, vl.level+1 ");
ied_info_sql.append(" from s_vir_link as v JOIN vl ON v.sub_ied = vl.pied and v.appid = vl.appid ) ");
ied_info_sql.append(" select cb, appid, pied, pp, sied, sp, cable, level from vl order by level desc;");
qDebug()<<ied_info_sql;
sql_query.prepare(ied_info_sql);
if(!sql_query.exec(ied_info_sql))
{
qDebug()<<sql_query.lastError();
}
else
{
while(sql_query.next())
{
qDebug()<<sql_query.value(2).toString();
}
}
如果把SQL语句换成select之类的简单查询,是可以正常执行的。
我把调试过程中打印出来的sql递归查询语句复制如下:
with recursive
vl(cb, appid, pied, pp, sied, sp, cable, level) as(
select v.cb, v.appid, v.pub_ied, v.pub_port, v.sub_ied, v.sub_port, v.cable, 0
from s_vir_link as v
where v.sub_ied = 'CF2201' and appid = '1217'
union
select v.cb, v.appid, v.pub_ied, v.pub_port, v.sub_ied, v.sub_port, v.cable, vl.level+1
from s_vir_link as v JOIN vl
ON v.sub_ied = vl.pied and
v.appid = vl.appid
)
select * from vl order by level desc;
用sqliteSpy之类的数据库访问工具执行,是可以查出结果的,但是用QT来执行SQLQuery就会报错。
错误如下:
QSqlError(1, "Unable to execute statement", "near "with": syntax error")
|
|