本文介绍了JDBC SQLite尝试获取可空列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Java中的SQLite Database
检索所有可为空的列.
I am trying to retrieve all nullable columns from an SQLite Database
in Java.
我不知道为什么当有两列具有not null
属性的列时,JDBC
为什么只返回主键为Not NULL
.
I don't know why JDBC
is returning that only the primary key is Not NULL
when there are two columns with not null
property.
CREATE TABLE `univDB` (
`x` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`FirstColumn` TEXT NOT NULL,
`SecondColumn` TEXT
);
以及当我运行以下代码时:
and when I run the following code:
ResultSet rs = stmt.executeQuery("SELECT * FROM "+table);
for (int i=1; i<= rs.getMetaData().getColumnCount(); i++) {
System.out.println("NULLABLE column "+i+" "+rs.getMetaData().isNullable(i));
if (ResultSetMetaData.columnNoNulls == rs.getMetaData().isNullable(i)) {
System.out.println("Column Not Null "+rs.getMetaData().getColumnName(i));
}
}
我知道x
是DB
的唯一不可为空的列,但也应该有FirstColumn
.
I get that x
is the only non-nullable column of the DB
but there should be FirstColumn
too.
推荐答案
我在sqlite版本sqlite-jdbc-3.8.11.2
I had the same issue with sqlite version sqlite-jdbc-3.8.11.2
您可以改用此方法(来源:文档):
You can try this instead (source: Documentation):
try {
DatabaseMetaData meta = dbConn.getMetaData();
ResultSet rs_columns = meta.getColumns(null, null, table, null);
while(rs_columns.next()) {
String columnName = rs_columns.getString(4); // Columnname
String nullable = rs_columns.getString(18); // Nullable returns smth like "NO" or "YES"
System.out.println("clmn: " + columnName);
System.out.println("IsNullable: " + nullable);
}
// OR directly
String columnName = "A_CLMM_NAME";
ResultSet rs_column = meta.getColumns(null, null, table, columnName);
if(rs_column.next()) {
String nullable = rs_column.getString(18); // Nullable
}
} catch (Exception e) {
e.printStackTrace();
}
这篇关于JDBC SQLite尝试获取可空列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!