简体   繁体   English

Android sqlite - 如何计算sqlite中2个REAL条目之间的差异?

[英]Android sqlite - how to calculate the difference between 2 REAL entries in sqlite?

In Android/Java, I am trying to compute and store the difference between 2 values in sqlite when one value is entered.在 Android/Java 中,当输入一个值时,我试图在 sqlite 中计算和存储两个值之间的差异。 My table looks like this:我的桌子看起来像这样:

在此处输入图片说明

When weights are added/stored in the table in the column 'Weight', the column 'Diff_Weight' shall receive "Weight(N) - Weight(N-1)".当权重添加/存储在表中的“重量”列中时,“Diff_Weight”列应接收“重量(N)-重量(N-1)”。 Example: the last cell of Diff_Weight (row 6) = 88.0 - 55.2 = 32.8.示例:Diff_Weight 的最后一个单元格(第 6 行)= 88.0 - 55.2 = 32.8。 // Row 5 shall get '-0.7' etc. Their type is REAL (col Weight & col Diff_Weight). // 第 5 行应为“-0.7”等。它们的类型是 REAL(col Weight & col Diff_Weight)。

This 32.8 should be calculated and added at the same time when 88.0 is added to the table.这个32.8应该在88.0加入表的同时计算并加入。

So far, I have read lots of tutorials and can't figure how to proceed.到目前为止,我已经阅读了很多教程,但不知道如何继续。 (My code to create and insert in the DB is fine, but reading is somehow more complex). (我在数据库中创建和插入的代码很好,但读取在某种程度上更复杂)。

My code to read the entry is very bad because I don't see how to set it up:我读取条目的代码非常糟糕,因为我不知道如何设置它:

public Bouble getData() {
        String selectQuery= "SELECT * FROM " + TABLE_NAME2 + " ORDER BY COL_4 DESC LIMIT 1";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(TABLE_NAME2, null);
           result2 = Double.valueOf(cursor.getString(cursor.getColumnIndex("Weight")));
           result1 = Double.valueOf(cursor.getString(cursor.getColumnIndex("Weight")-1));
           insertdata(result2-result1);  //insert in row 6 of Diff_Weight
        return
    }

Can anybody help there?有人可以帮忙吗?

If that is unclear, I was needing some help for the sqlite command AND the java to get the difference result.如果不清楚,我需要一些关于 sqlite 命令和 java 的帮助来获得差异结果。

Simplistically you can get the data by joining to the same table简单地说,您可以通过加入同一个表来获取数据

SELECT a.id, a.weight, b.weight, (b.weight - a.weight)  FROM TABLE_NAME2 a
join TABLE_NAME2 b on (b.id = a.id + 1);

One way is to use the lag() window function to get the value of the previous row (As ordered by id ; using timestamps would be better but between splitting up the date and time into different columns and not using a date format that can be meaningfully sorted, this is easier.):一种方法是使用lag()窗口函数来获取前一行的值(按id排序;使用时间戳会更好,但在将日期和时间分成不同的列和不使用日期格式之间有意义的排序,这更容易。):

SELECT id, weight,
       round(coalesce(weight - lag(weight, 1) OVER (ORDER BY id), weight), 1) AS diff_weight
FROM example
ORDER BY id

which gives这使

id          weight      diff_weight
----------  ----------  -----------
1           22.0        22.0
2           22.2        0.2
3           55.0        32.8
4           55.9        0.9
5           55.2        -0.7
6           88.0        32.8

You can make a view of this query use that like a normal table if you like.如果您愿意,您可以像普通表一样使用此查询的视图 Generating the differences dynamically like this has the advantage that if an existing weight value changes, everything that depends on it doesn't have to be updated.像这样动态生成差异的优点是,如果现有权重值发生变化,则不必更新依赖于它的所有内容。

ok, after a long search, here is a possible result (sqlite + java):好的,经过长时间的搜索,这是一个可能的结果(sqlite + java):

  • first, you need to query the last row of the table...首先,您需要查询表的最后一行...
  • ...and handle the case if there is no row in your table (blank or new table) ...如果表中没有行(空白或新表),则处理这种情况
  • then you must query the 'Weight' value from the known column 'Weight' (Y) and the row with ID you already have (X)那么您必须从已知列“权重”(Y)和具有您已经拥有的 ID 的行(X)中查询“权重”值
  • and when you have your value (last_weight), you need to write the difference (weight-last_weight) in the column 'Diff_Weight'.并且当您获得值 (last_weight) 时,您需要在“Diff_Weight”列中写入差值 (weight-last_weight)。

Here is the code:这是代码:

SQLiteDatabase db = this.getWritableDatabase();
//query the last row of the table
Cursor cursor = db.rawQuery("SELECT ID FROM TABLE_NAME2 ORDER BY ID DESC LIMIT 1", null);
cursor.moveToLast();
int lastID;
//handle the case if there is no row in your table
try {
    lastID = cursor.getInt(0);
} catch (Exception e) {
    lastID = 0;
}
Double lastWeight = 0.0;
//query the 'Weight' value
if (lastID >= 1) {
   Cursor cursor2 = db.rawQuery("SELECT Weight FROM TABLE_NAME2 WHERE ID=" + lastID, null);
   if (cursor2.moveToFirst()) { //this is boundary otherwise 'lastWeight' doesn't get the value
        lastWeight= cursor2.getDouble(0);
   }
} else {
    lastWeight = 0.0;
}
//write the difference in the Diff_Weight column (=COL_5)
ContentValues cValues2 = new ContentValues();
//add your data in COL_1 to COL_4 here...
cValues2.put(COL_5, weight - lastWeight);
long id2 = db.insert(TABLE_NAME2, null, cValues2);

... And so you get the red figures in the column Diff_Weight from the table photo in the question. ... 所以你从问题的表格照片中得到 Diff_Weight 列中的红色数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM