SQLiteJDBC和PreparedStatement使用pragma table_info

时间:2009-08-28 01:32:55

标签: java sqlite sqlexception sqlitejdbc

我正在使用Java和SQLiteJDBC来使用SQLite。我需要访问给定表的列名,我发现我可以使用以下命令完成此操作:

pragma table_info(myTable)

但是,尝试执行以下操作时出现错误。

PreparedStatement _pstmt =
    this._DBConnection.prepareStatement("pragma table_info( '?' );",
         new String[] {_tableName} );
  

java.sql.SQLException:NYI

我不知道NYI意味着什么,而且,我不确定我是否可以做我想做的事情。关于如何获得列名的任何建议?

2 个答案:

答案 0 :(得分:3)

NYI意味着“尚未实施。”

我猜测命令“pragma table_info”可能无法直接作为预处理语句执行。

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

我无法摘录代码并在此处发布,因为这样做与StackOverflow使用的Creative Commons许可证不兼容。所以请转到该链接并查看。

答案 1 :(得分:2)

来自SQLiteJDBC source code的代码摘录:

 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表中获取实际的列定义并自行解析。

相关问题