Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database SQLITE编程_Database_Sqlite - Fatal编程技术网

Database SQLITE编程

Database SQLITE编程,database,sqlite,Database,Sqlite,在大学里,我学习了PL/SQL,我使用它以编程方式将数据插入/更新到表中 那么在SQLITE中有什么方法可以做到这一点呢 我有一本表格书,它有两列:readPages和currentPage。readPage包含关于我今天读了多少页的信息,currentPage显示到今天为止的总阅读页数 目前我只有ReadPage的数据,所以我想计算过去几天的currentPage,例如 readPages: 19 10 43 20 35 # I have data for 5 days cur

在大学里,我学习了PL/SQL,我使用它以编程方式将数据插入/更新到表中

那么在SQLITE中有什么方法可以做到这一点呢

我有一本表格书,它有两列:readPages和currentPage。readPage包含关于我今天读了多少页的信息,currentPage显示到今天为止的总阅读页数

目前我只有ReadPage的数据,所以我想计算过去几天的currentPage,例如

readPages:   19  10  43  20  35   # I have data for 5 days
currentPage: 19  29  72  92  127  # I want to calculate it
所以这对于编程来说很容易,但是如何使用sqlite,因为它不像plsql


如果您使用的是sqlite 3.25或更新版本,则类似于:

SELECT date, readPages
      , sum(readPages) OVER (ORDER BY date) AS total_pages_read
FROM yourTableName
ORDER BY date;

将计算正在运行的页面总数。

如果您使用的是sqlite 3.25或更高版本,例如:

SELECT date, readPages
      , sum(readPages) OVER (ORDER BY date) AS total_pages_read
FROM yourTableName
ORDER BY date;

将计算正在运行的页面总数。

行的顺序可以由id或日期确定。 列日期的问题在于其格式:“DD-MM”不可比较。 最好将其更改为类似于:“YYYY-MM-DD”。 由于您的SQLite版本不允许您使用窗口函数,因此您可以使用以下功能:

update findYourWhy 
set currentPage = coalesce(
  (select sum(f.readPage) from findYourWhy f where f.id <= findYourWhy.id), 
  0
);
update findYourWhy 
set currentPage = coalesce(
  (select sum(f.readPage) from findYourWhy f where f.date <= findYourWhy.date), 
  0
);

结果:

| id  | date  | currentPage | readPage |
| --- | ----- | ----------- | -------- |
| 1   | 06-05 | 36          | 36       |
| 2   | 07-05 | 45          | 9        |
| 3   | 08-05 | 57          | 12       |
| 4   | 09-05 | 62          | 5        |
| 5   | 10-05 | 74          | 12       |
| 6   | 11-05 | 87          | 13       |
| 7   | 12-05 | 89          | 2        |
| 8   | 13-05 | 101         | 12       |
| 9   | 14-05 | 104         | 3        |
| 10  | 15-05 | 109         | 5        |
| 11  | 16-05 | 115         | 6        |
| 12  | 17-05 | 122         | 7        |
| 13  | 18-05 | 129         | 7        |

行的顺序可以由id或日期确定。 列日期的问题在于其格式:“DD-MM”不可比较。 最好将其更改为类似于:“YYYY-MM-DD”。 由于您的SQLite版本不允许您使用窗口函数,因此您可以使用以下功能:

update findYourWhy 
set currentPage = coalesce(
  (select sum(f.readPage) from findYourWhy f where f.id <= findYourWhy.id), 
  0
);
update findYourWhy 
set currentPage = coalesce(
  (select sum(f.readPage) from findYourWhy f where f.date <= findYourWhy.date), 
  0
);

结果:

| id  | date  | currentPage | readPage |
| --- | ----- | ----------- | -------- |
| 1   | 06-05 | 36          | 36       |
| 2   | 07-05 | 45          | 9        |
| 3   | 08-05 | 57          | 12       |
| 4   | 09-05 | 62          | 5        |
| 5   | 10-05 | 74          | 12       |
| 6   | 11-05 | 87          | 13       |
| 7   | 12-05 | 89          | 2        |
| 8   | 13-05 | 101         | 12       |
| 9   | 14-05 | 104         | 3        |
| 10  | 15-05 | 109         | 5        |
| 11  | 16-05 | 115         | 6        |
| 12  | 17-05 | 122         | 7        |
| 13  | 18-05 | 129         | 7        |

这里的一个重要区别是PL/SQL在单独的服务器上运行。服务器和应用程序之间的延迟非常重要,因此直接在SQL server上执行代码是值得的,以便在某些方面获得良好的性能。现在,SQLite是嵌入在应用程序中的库。这里没有明显的延迟。那么…首先为什么要这样做呢?这里一个重要的区别是PL/SQL在一个单独的服务器上运行。服务器和应用程序之间的延迟非常重要,因此直接在SQL server上执行代码是值得的,以便在某些方面获得良好的性能。现在,SQLite是嵌入在应用程序中的库。这里没有明显的延迟。那么…首先为什么要这样做?@thesummer如果您使用的是sqlite 3.25或更高版本。您使用的是一个缺少窗口功能的过时版本。我真的很抱歉,我没有检查它。所以,如果您使用的是sqlite 3.25或更高版本,我应该删除这个问题吗?@thesummer。您使用的是一个缺少窗口功能的过时版本。我真的很抱歉,我没有检查它。所以我要删除这个问题吗?非常感谢,但我不知道coalesce函数是如何工作的。coalesce返回其参数的第一个非空值。在这种情况下,如果总和为null,则返回0。如果您确信这个总和永远不会为空,您可以省略合并并使用:update findYourWhy set currentPage=select sumf.readPage from findyourf why where f.id Oh我现在得到它。我用另一种方法做同样的事情。更新findYourWhy设置currentPage=readPage,其中id=1;更新findYourWhy设置currentPage=readPage+从findYourWhy中选择currentPage,其中findYourWhy.id-1=f.id,其中findYourWhy.id>1;为什么?第一次更新将不必要地使用第一行的readPage更新所有行。为什么要执行2次更新?此外,您无法确保ID总是相差1。如果id是autoincrement,并且删除行,则会有间隙。非常感谢,但我不知道coalesce函数是如何工作的。coalesce返回其参数的第一个非空值。在这种情况下,如果总和为null,则返回0。如果您确信这个总和永远不会为空,您可以省略合并并使用:update findYourWhy set currentPage=select sumf.readPage from findyourf why where f.id Oh我现在得到它。我用另一种方法做同样的事情。更新findYourWhy设置currentPage=readPage,其中id=1;更新findYourWhy设置currentPage=readPage+从findYourWhy中选择currentPage,其中findYourWhy.id-1=f.id,其中findYourWhy.id>1;为什么?第一次更新将不必要地使用第一行的readPage更新所有行。为什么要执行2次更新?此外,您无法确保ID总是相差1。如果id为autoincrement并删除行,则会有间隙。