简体   繁体   English

SQLLite-在python中进行类型转换

[英]SQLLite - type casting in python

I am new to python. 我是python的新手。 I am moving my table data from AWS Redshift to in memory sqllite database in python. 我将表数据从AWS Redshift移到python中的内存sqllite数据库中。

Please refer the table structure below : 请参考以下表格结构:

Redshift Table : 红移表:

s0 1

My value field in the above column is in Decimal at Redshift. 上栏中的我的值字段为Redshift时的Decimal。

When I tried to bring the data to sqllite , I found that the result which I am getting from Redshift contains Decimal keyword in the result so I was not able to insert it into the sqllite table which was also marked as Decimal ( Here is the link for the same issue which I have raised earlier ). 当我尝试将数据带到sqllite时,我发现从Redshift获得的结果中包含Decimal关键字,因此我无法将其插入也标记为Decimal的sqllite表中(这是链接对于我之前提出的相同问题)。 So I changed my datatype as varchar(30) so that my values can fit in the sqllite table. 所以我将数据类型更改为varchar(30),以便我的值可以适合sqllite表。

Please refer the table structure below for my in memory sqllite table : 请参考下面的表结构以获取我的内存sqllite表:

in memory sqllite table 在内存中的sqllite表

在此处输入图片说明

Now I wanted to do the sum of the column value on the timestamp basis in my python script, but I am not able to get the expected result. 现在,我想在我的python脚本中基于时间戳进行列值的总和,但是我无法获得预期的结果。

Here is the query which I am using in Redshift to get the sum and I am getting the expected output : 这是我在Redshift中使用的查询以获取总和,并且得到了预期的输出:

select sum(value) from table where id = 9831 and item_date = '2018-11-01' and to_char(item_datetime, 'HH24MI') between '0000' and '2359';

Here is the query which I am using in my script to type cast the value column to decimal and item_datetime to strftime to get the required output : 这是我在脚本中用于将值列强制转换为decimal ,将item_datetime强制转换为strftime以获取所需的输出:

select sum(CAST(value as decimal)) from table where id = 9831 and item_date = '2018-11-01' and strftime(item_datetime, 'HH24MI') between '0000' and '2359';

But I am getting output as None . 但是我得到的输出为None Cloud somebody explain me this behavior and what I am doing wrong here ? 云有人向我解释了这种行为,我在这里做错了什么? How to deal with this issue ? 该如何处理?

In SQLite it goes like this: 在SQLite中,它是这样的:

... AND STRFTIME('%H%M', item_datetime) BETWEEN '0000' AND '2359'

For more info see the docs . 有关更多信息,请参阅文档

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

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