往返SQL数据尤其是日期时间 [英] Roundtrip SQL data especially datetime

查看:60
本文介绍了往返SQL数据尤其是日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用dbapi和SQL查询从数据库获取数据时,您通常如何回转数据?特别是日期时间?


SQL datetime列很好地转换为Python日期时间(惊喜),

然后转换成类似''2005-的字符串08-03 07:32:48''。没问题

有了这个 - 一切都很顺利,直到你试图把数据放回

其他方式。


没有明显的方法将该字符串解析回日期时间,并且

这样做没有明显的方法将其推回到SQL日期时间列。

我错过了什么?


[我特别想避免陷入SQL本地设置

。]


DavidY

When getting data from a database using the dbapi and an SQL query, how do
you in general round trip the data? Especially date-time?

An SQL datetime column translates nicely into a Python datetime (surprise),
which then translates into a string like ''2005-08-03 07:32:48''. No problem
with that -- all works quite nicely, until you try to put data back the
other way.

There is no obvious way to parse that string back into a datetime, and
having done so no obvious way to push that back into a SQL datetime column.
Am I missing something?

[I would particularly like to avoid getting trapped by SQL "local settings"
too.]

DavidY

推荐答案

在15 dic,07:44," dyork" < reverse york ... @ david.comwrote:
On 15 dic, 07:44, "dyork" <reverse york...@david.comwrote:

使用dbapi和SQL查询从数据库获取数据时,如何使用

你一般来回往返数据?特别是日期时间?


SQL datetime列很好地转换为Python日期时间(惊喜),

然后转换成类似''2005-的字符串08-03 07:32:48''。没问题

这个 - 一切都很好,直到你试图以其他方式将数据放回


When getting data from a database using the dbapi and an SQL query, how do
you in general round trip the data? Especially date-time?

An SQL datetime column translates nicely into a Python datetime (surprise),
which then translates into a string like ''2005-08-03 07:32:48''. No problem
with that -- all works quite nicely, until you try to put data back the
other way.



不要转换为字符串并保留日期时间对象。


-

Gabriel Genellina

Dont convert to string and keep the datetime object.

--
Gabriel Genellina


dyork写道:
dyork wrote:

使用dbapi和SQL从数据库获取数据时查询,怎么办

你一般往返数据?特别是日期时间?


SQL datetime列很好地转换为Python日期时间(惊喜),

然后转换成类似''2005-的字符串08-03 07:32:48''。
When getting data from a database using the dbapi and an SQL query, how do
you in general round trip the data? Especially date-time?

An SQL datetime column translates nicely into a Python datetime (surprise),
which then translates into a string like ''2005-08-03 07:32:48''.



它不会自我翻译。你翻译了它。正如加布里埃尔所说,

不要这样做。

It doesn''t translate itself. You translated it. As Gabriel has said,
don''t do that.


没问题

带有 - 一切都很顺利,直到你试图以其他方式将数据放回


No problem
with that -- all works quite nicely, until you try to put data back the
other way.


没有明显的方法将该字符串解析回日期时间,
There is no obvious way to parse that string back into a datetime,



我想这一切都取决于你明显的定义:-)


构造函数是datetime.datetime(年,.....,秒)所以

以下(一直回到Python 2.3)似乎不太好

对我来说不明显:


| >>导入日期时间

| >> s ='''2005-08-03 07:32:48''

| >> a = map(int,s.replace('' - '','''').replace('':'','''')。split())

| >> a

| [2005,8,3,7,32,48]

| >> dt = datetime.datetime(* a)

| >> dt

| datetime.datetime(2005,8,3,7,32,48)


如果你有Python 2.5,你可以使用它:


| >> datetime.datetime.strptime(s,''%Y-%m-%d%H:%M:%S'')

| datetime.datetime(2005,8,3,7,32,48)

I suppose it all depends on your definition of obvious :-)

The constructor is datetime.datetime(year, ....., second) so the
following (which works all the way back to Python 2.3) seems not too
obscure to me:

| >>import datetime
| >>s = ''2005-08-03 07:32:48''
| >>a = map(int, s.replace(''-'', '' '').replace('':'', '' '').split())
| >>a
| [2005, 8, 3, 7, 32, 48]
| >>dt = datetime.datetime(*a)
| >>dt
| datetime.datetime(2005, 8, 3, 7, 32, 48)

If you have, as you should, Python 2.5, you can use this:

| >>datetime.datetime.strptime(s, ''%Y-%m-%d %H:%M:%S'')
| datetime.datetime(2005, 8, 3, 7, 32, 48)




这样做没有明显的推动方法回到SQL datetime列。
and
having done so no obvious way to push that back into a SQL datetime column.



如何将str或float对象推回到

适当类型的SQL列?有什么不同?您的数据库API应该非常透明地处理这个

。尝试一下,看看会发生什么。


HTH,

John

How do you push a str or float object back into an SQL column of
appropriate type? What''s the difference? Your DB API should handle this
quite transparently. Try it and see what happens.

HTH,
John


谢谢加布里埃尔,但当我说往返时我的意思是:将所有

转换成字符串的方式,然后一直回来,所以你的善意建议并非全部

有用。我需要字符串才能访问非Python对象或网页。


DY


" Gabriel Genellina" < ga ****** @ yahoo.com.arwrote in message

news:11 ********************* @ l12g2000cwl.googlegro ups.com ...
Thanks Gabriel, but when I said "round trip" I really did mean: convert all
the way to string and all the way back again, so your kind advice is not all
that helpful. I need string to get to a non-Python object or a Web page.

DY

"Gabriel Genellina" <ga******@yahoo.com.arwrote in message
news:11*********************@l12g2000cwl.googlegro ups.com...

在15 dic,07:44," dyork" < reverse york ... @ david.comwrote:
On 15 dic, 07:44, "dyork" <reverse york...@david.comwrote:

>使用dbapi和SQL查询从数据库获取数据时,
如何做
你一般来回往返数据?特别是日期时间?

一个SQL datetime列很好地转换为Python日期时间
(惊喜),
然后转换为类似''2005-08-03 07的字符串:32:48 ''。没有
问题
这一切 - 一切都很好,直到你试图把数据放回
其他方式。
>When getting data from a database using the dbapi and an SQL query, how
do
you in general round trip the data? Especially date-time?

An SQL datetime column translates nicely into a Python datetime
(surprise),
which then translates into a string like ''2005-08-03 07:32:48''. No
problem
with that -- all works quite nicely, until you try to put data back the
other way.



不要转换为字符串并保留日期时间对象。


-

Gabriel Genellina


Dont convert to string and keep the datetime object.

--
Gabriel Genellina



这篇关于往返SQL数据尤其是日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆