getPrimaryKeys() does not return all columns of primary key

Issue #228 new
VIDYASAGAR MUNDROY
created an issue

The database metadata method getPrimaryKeys() returns only the last column of compound primary key. The following code creates a table containing 4 columns and a primary key containing the first 3 columns. But the getPrimaryKeys() returns only the last column of the primary key and key sequence is also not correct:

import java.sql.*;

public class TestPrimaryKeys {
    // URL: Change it as per the location of database test.sqb"
    static String url="jdbc:sqlite:/tmp/test.sqb";
    static String driverClass = "org.sqlite.JDBC";
    static String userId=null;
    static String passwd=null;

    public static void main(String args[]) {
        try{
            Class.forName(driverClass);
            Connection conn = DriverManager.getConnection(url, userId, passwd);
            System.out.println("Connected to " + url +
                    " with uid=" + userId +
                    " password=" + passwd);

            DatabaseMetaData dbmd = conn.getMetaData();
            Statement stmt = conn.createStatement();
            try {
                stmt.execute("DROP TABLE t1");
            } catch (SQLException se) {
            }
            stmt.execute("create table t1(c1 int, c2 int, c3 int, c4 int, primary key(c1, c2, c3))");
            stmt.close();
            ResultSet rs = dbmd.getPrimaryKeys(null, null, "t1");
            ResultSetMetaData rsmd = rs.getMetaData();
            // Get result set column labels for primary key column name and key sequence.
            System.out.println(rsmd.getColumnLabel(4) + "\t" + rsmd.getColumnLabel(5));
            // Print dashes below column names.
            System.out.println(new String(new char[rsmd.getColumnLabel(4).length()]).replace("\0", "-") 
                    + "\t" + new String(new char[rsmd.getColumnLabel(5).length()]).replace("\0", "-"));
            // Print primary key column name and its sequence in the key.
            while (rs.next()) {
                System.out.println(rs.getString(4) + "\t\t" + rs.getShort(5));
            }
            conn.close();
        } catch (ClassNotFoundException cne) {
            System.out.println("Error: " + driverClass + ":"
                        + cne.toString());
        } catch (SQLException se) {
            System.out.println("Error: " 
                    + se.toString());
        }

    }

}

Output of the program:

Connected to jdbc:sqlite:/tmp/test.sqb with uid=null password=null
COLUMN_NAME KEY_SEQ
----------- -------
c3      0

Expected correct output should be:

Connected to jdbc:sqlite:/tmp/test.sqb with uid=null password=null
COLUMN_NAME KEY_SEQ
----------- -------
c1            1
c2            2
c3            3

JDBC driver used: sqlite-jdbc-3.18.0.jar Downloaded today from bitbucket

Note:- Old JDBC driver sqlite-jdbc-3.7.2.jar gives list of all columns but key sequence is still 0. Output of the program with this JDBC driver is as following:

Connected to jdbc:sqlite:/tmp/test.sqb with uid=null password=null
COLUMN_NAME KEY_SEQ
----------- -------
c1      0
c2      0
c3      0

Used JDK 1.7.0_79 and Ubuntu 16.04

Comments (0)

  1. Log in to comment