SQLiteJDBC and PreparedStatement to use pragma tab

2019-06-28 09:08发布

问题:

I'm utilizing Java and SQLiteJDBC to work with SQLite. I need to access name of columns for a given table and I've found that I could accomplish this with the following command:

pragma table_info(myTable)

However, when attempting to do the following I get an error.

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

java.sql.SQLException: NYI

I have no idea what NYI means and furthermore, I'm not sure if I can do what I'm trying to do. Any suggestions on how I can accomplish getting the column names?

回答1:

NYI means "not yet implemented."

I would guess that the command "pragma table_info" probably can't be executed directly as a prepared statement.

There's an example of executing that pragma statement in the code for the SQLite JDBC driver, class org.sqlite.Metadata, methods such as getColumns() and getPrimaryKeys().

I can't excerpt the code and post it here, because doing so would not be compatible with the Creative Commons license used by StackOverflow. So please go to that link and take a look.



回答2:

There's this excerpt of code from 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);
    }

I'm guessing NYI means "Not Yet Implemented".

If the pragma thing isn't working out,

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)

You can grab the actual column definitions from the sqlite_master table and parse them out yourself.