[英]SQLite: How insert document with not whole fill columns?
In my java aplication I use SQLite. 在我的Java应用程序中,我使用SQLite。 I have table with 62 columns.
我有62列的表格。 And here code that try to insert document in this table.
这里是试图在此表中插入文档的代码。
@Override
protected int insertToDB(Connection connection, String tableName, int pkDocsID, Map < Integer, String > fieldMap) throws SQLException {
PreparedStatement ps = null;
try {
String parametersAsQMarks = DBManager.getInsertParametersAsQMarks(fieldMap.size() + COUNT_RESERVED_FIELDS);
ps = connection.prepareStatement("INSERT INTO " + tableName + " VALUES (" + parametersAsQMarks + ")");
ps.setInt(COUNT_RESERVED_FIELDS, pkDocsID); // reserved field
int step = COUNT_RESERVED_FIELDS + 1;
for (int index = 0; index < fieldMap.size(); index++) {
String fieldValue = fieldMap.get(index + 1);
ps.setString(index + step, fieldValue);
}
return ps.executeUpdate();
} finally {
if (ps != null) {
ps.close();
}
}
}
But I get error: 但是我得到了错误:
[java] [23.01.2018 20:57:28.312]
md.deeplace.ca.db.SQLiteStrategy.insertM2Document(SQLiteStrategy.java:125) ERROR:
[java] table OUTDocsBS has 62 columns but 37 values were supplied
[java] java.sql.SQLException: table OUTDocsBS has 62 columns but 37 values were supplied
[java] at org.sqlite.DB.throwex(DB.java:288)
[java] at org.sqlite.NestedDB.prepare(NestedDB.java:115)
[java] at org.sqlite.DB.prepare(DB.java:114)
[java] at org.sqlite.PrepStmt.<init>(PrepStmt.java:37)
[java] at org.sqlite.Conn.prepareStatement(Conn.java:231)
[java] at org.sqlite.Conn.prepareStatement(Conn.java:224)
[java] at org.sqlite.Conn.prepareStatement(Conn.java:213)
[java] at com.myproject.ca.db.SQLiteStrategy.insertToDB(SQLiteStrategy.java:64)
I want to insert document when fill not all columns. 我想在不填充所有列时插入文档。 In this case - 37 columns.
在这种情况下-37列。 How I can do this?
我该怎么做?
You'll need to name the columns. 您需要命名列。 Instead of:
代替:
..."INSERT INTO " + tableName + " VALUES (" + parametersAsQMarks + ")");
try 尝试
..."INSERT INTO " + tableName + "(columnName1, columnName2, ... columnName37) VALUES (" + parametersAsQMarks + ")");
where each named column matches the corresponding fieldMap
value to be inserted. 其中每个命名列与要插入的相应
fieldMap
值匹配。 Based on the code, it might be useful to change fieldMap
to Map<String, String>
, where the key is the column name (instead of column number). 根据代码,将
fieldMap
更改为Map<String, String>
可能很有用,其中键是列名(而不是列号)。
Of course, the other remaining columns must allow null. 当然,其他其余列必须允许为null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.