使用SQLiteJDBC和PreparedStatement来使用pragma table_info

4
我正在使用Java和SQLiteJDBC来处理SQLite。我需要访问给定表的列名,并发现我可以使用以下命令完成此操作:
pragma table_info(myTable)

然而,当尝试执行以下操作时,我遇到了错误。
PreparedStatement _pstmt =
    this._DBConnection.prepareStatement("pragma table_info( '?' );",
         new String[] {_tableName} );

java.sql.SQLException: NYI

这句话意思是出现了java.sql.SQLException异常:NYI。我不知道NYI具体指什么,而且我也不确定我是否能做到我想做的事情。请问有什么建议可以获取列名吗?

2个回答

3

NYI 意味着“尚未实现”。

我猜想,命令“pragma table_info”可能无法直接作为准备好的语句执行。

在 SQLite JDBC 驱动程序的代码中有一个执行 pragma 语句的示例,类 org.sqlite.Metadata,方法如 getColumns()getPrimaryKeys()

我无法摘录代码并在此处发布,因为这样做将与 StackOverflow 使用的创作共用许可证不兼容。所以请访问该链接并查看。


我看了代码,我明白你的意思。谢谢。 - Frank V

2
以下是代码块来自于 SQLiteJDBC 源代码:(原文链接)
 public PreparedStatement prepareStatement(String sql, int autoC)
        throws SQLException { throw new SQLException("NYI"); }
    public PreparedStatement prepareStatement(String sql, int[] colInds)
        throws SQLException { throw new SQLException("NYI"); }
    public PreparedStatement prepareStatement(String sql, String[] colNames)
        throws SQLException { throw new SQLException("NYI"); }
    public PreparedStatement prepareStatement(String sql, int rst, int rsc) 
                                throws SQLException {
        return prepareStatement(sql, rst, rsc,
                                ResultSet.CLOSE_CURSORS_AT_COMMIT);
    }

我猜 NYI 的意思是“尚未实现”。 如果 pragma 不起作用,
sqlite> CREATE TABLE a(col1, col2, col3);
sqlite> CREATE TABLE b(w, x, y, z);
sqlite> SELECT * FROM sqlite_master;
table|a|a|2|CREATE TABLE a(col1, col2, col3)
table|b|b|3|CREATE TABLE b(w, x, y, z)

sqlite> SELECT sql FROM sqlite_master;
CREATE TABLE a(col1, col2, col3)
CREATE TABLE b(w, x, y, z)

您可以从sqlite_master表中获取实际的列定义,并自行解析它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接