我在向 SQLite 数据库发送查询时遇到了问题。
问题是数据库(来自excel表)有空单元格,其中应该有值,从上面的单元格中给出的值,在同一列中。
换句话说,如果值没有改变,excel 表的制作者会将同一列中的单元格留空。我实际上需要这些单元格中的值!
由于工作表/数据库的大小,我无法手动执行此操作,有没有办法以编程方式执行此操作?
如果有的话,我真的更喜欢 sql-query 方式!
最好的祝愿,托尔
you can do this in excel. I answered a similar question here:
Blank Values in Excel File From CSV (not just rows)
You can also do this entirely in excel:
Select column (or whatever range you're working with), then go to Edit>Go To (Ctrl+G) and click Special. Check Blanks & click OK. This will select only the empty cells within the list. Now type the = key, then up arrow and ctrl-enter.
This will put a formula in every blank cell to equal the cell above it. You could then copy ^ paste values only to get rid of the formulas.
与@maneesha 提出的类似方法是按空白单元格进行过滤,然后编写“等于上部单元格”公式,然后清除过滤器。
但我认为@maneeshas 的解决方案更酷。
您查询整个数据库,保持原始排序顺序,然后遍历每一行。如果某些内容为空,则使用上面的值更新它。
编辑:你可以在这里用类似的东西来做:
SQLiteDatabase db = SQLiteDatabase.openDatabase("myDb.db", null, SQLiteDatabase.OPEN_READWRITE);
Cursor c = db.query("table", null, null, null, null, null, "the column that defines the sort order");
if (c != null) {
int columnCount = c.getColumnCount();
String[] lastValues = new String[columnCount];
while (c.moveToNext()) {
ContentValues cv = new ContentValues();
for (int i = 0; i < columnCount; i++) {
String value = c.getString(i);
if (TextUtils.isEmpty(value)) {
// if that happens on first column you are screwed anyways.
cv.put(c.getColumnName(i), lastValues[i]);
} else {
lastValues[i] = value;
}
// we need to save some id here
}
if (cv.size() > 0) {
// use the id here
db.update("table", cv, "column=?", new String[] { "id" });
}
}
c.close();
}
但我建议你使用 Excel 来修复数据库。这更容易,你不需要理解上面的代码。