如何在数据库的表上循环? [英] how to loop over the tables of a database?

查看:142
本文介绍了如何在数据库的表上循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用下面的代码获取错误 java.lang.UnsupportedOperationException 循环数据库的表。我甚至尝试过 cfloop查询和其他属性得到错误复杂的值无法转换为简单的值。任何人都可以告诉我我该如何循环这个查询?感谢。

I am trying to loop over the tables of a database using the following code getting an error java.lang.UnsupportedOperationException. i have even tried with cfloop query and other attributes got errors complex value can't convert to simple value. can anyone tell me how shall i have to loop over this query ? Thanks.

<cfquery name="q" datasource="datasource">
    SHOW TABLES FROM datasource
</cfquery>
<cfloop collection ="#q#" item="i">
   #q[i]#
</cfloop> 


推荐答案

您得到这个错误,因为cfloop collection 需要一个结构体,而不是一个查询对象。因此,UnsupportedOperation ...错误。

You are getting that error because cfloop collection expects a structure, not a query object. Hence the "UnsupportedOperation..." error.

而是应该使用查询循环。生成的列名称是动态的,基于您提供的数据库名称。您可以硬编码或动态访问它:

Instead you should use a query loop. The generated column name is dynamic, based on the database name you supply. You can either hard code it or access it dynamically:

   <cfset colNames = listToArray(q.columnList)>
   <cfoutput query="q">
      <cfloop array="#colName#" index="col">
            #q[col][currentRow]#
      </cfloop>
      <br>
   </cfoutput>

也就是说,我发现使用元数据 INFORMATION_SCHEMA 观看次数。您可以像任何表一样查询它们。然后按照惯例输出静态列名称。

That said, I find it easier to use the metadata INFORMATION_SCHEMA views. You can query them just like any table. Then output the static column names as usual.

    <cfquery name="yourQueryName" ...>
       SELECT  TABLE_NAME
       FROM    INFORMATION_SCHEMA.TABLES
       WHERE   TABLE_SCHEMA = 'YourDatabaseName'
    </cfquery>

    <cfoutput query="yourQueryName">
         #TABLE_NAME# <br>
    </cfoutput>

这篇关于如何在数据库的表上循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆