how to loop over the tables of a database?

泪湿孤枕 提交于 2019-12-07 09:01:07

问题


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> 

回答1:


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>

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>



回答2:


have you tried CFDBINFO? It supports a type of 'tabes' and ought to return you a query of table name, type and remarks for each table.




回答3:


How about

<cfoutput query="q">
#tables#
<cfoutput>


来源:https://stackoverflow.com/questions/21711894/how-to-loop-over-the-tables-of-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!