将日期时间传递到psycopg2

Ste*_*ott 2 python postgresql datetime psycopg2

我正在尝试使用Psycopg2将datetime值插入到我的postgres数据库中。

我的代码以前可以运行,但是我从%s表示法切换为{}表示法,并且代码中断了。

这是我的代码

for j in eveLists.itemList:
    tempPrice = fetchSellPrice(i, j)
    database_name = eveLists.DatabaseDict[i]
    now = datetime.datetime.utcnow()

    cur.execute("INSERT INTO {0} VALUES ({1}, {2}, {3}, NULL, {4}, {5}, NULL);".format(
                                                                                       database_name,
                                                                                       str(i),
                                                                                       str(j),
                                                                                       float(tempPrice),
                                                                                       datetime.date.today(),
                                                                                       now))
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

psycopg2.ProgrammingError: syntax error at or near "00"
LINE 1: ...0000142, 2268, 3.11, NULL, 2017-05-09, 2017-05-10 00:40:03.3...
                                                             ^
Run Code Online (Sandbox Code Playgroud)

它将日期和时间视为两个单独的对象。

我尝试了几种被注释掉的不同方式,并且都抛出了各种错误消息。我试过将日期时间用引号引起来,

now = ("'%s'") % str(datetime.datetime.utcnow())
Run Code Online (Sandbox Code Playgroud)

给出一个错误

psycopg2.ProgrammingError: column "mydate" is of type date but expression is of type integer
LINE 1: ...NTO temp_jita VALUES (30000142, 2268, 3.11, NULL, 2017-05-09...
                                                             ^
HINT:  You will need to rewrite or cast the expression.
Run Code Online (Sandbox Code Playgroud)

它认为我的日期是一个整数,即使我将其用引号引起来也是如此。

我也尝试过手动制作psycopg2时间戳:

now = psycopg2.Timestamp(now.strftime("%Y"),
                                     now.strftime("%m"),
                                     now.strftime("%d"),
                                     now.strftime("%h"),
                                     now.strftime("%M"),
                                     int(now.strftime("%-S")))
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

    int(now.strftime("%-S")))
TypeError: an integer is required (got type str)
Run Code Online (Sandbox Code Playgroud)

我不知道它如何认为这是一个字符串,尤其是因为我将其转换为整数!

任何帮助,将不胜感激。

编辑:我使用{}表示法而不是%s表示法传递变量的原因是因为我收到以下代码的错误:

  for j in eveLists.itemList:
            tempPrice = fetchSellPrice(i, j)
            database_name = eveLists.DatabaseDict[i]
            now = datetime.datetime.utcnow()
            now = str(now)

            cur.execute("INSERT INTO %s VALUES (%s, %s, %s, NULL, %s, %s, NULL);", [database_name,
                                                                                    str(i),
                                                                                    str(j),
                                                                                    float(tempPrice),
                                                                                    datetime.date.today(),
                                                                                    now
                                                                                    ])

psycopg2.ProgrammingError: syntax error at or near "'temp_jita'"
LINE 1: INSERT INTO 'temp_jita' VALUES ('30000142', '2268', 3.03, NU...
                    ^
Run Code Online (Sandbox Code Playgroud)

请参考我以前关于该主题的文章:如何在Python中为SQL查询从字符串中删除引号?

编辑:按照@Zorg从此链接(http://initd.org/psycopg/docs/sql.html)的建议,此代码对我有用:

cur.execute(sql.SQL("INSERT INTO {} VALUES (%s, %s, %s, NULL, %s, %s, NULL);").format(sql.Identifier(database_name)),[
                                                                                    str(i),
                                                                                    str(j),
                                                                                    float(tempPrice),
                                                                                    datetime.date.today(),
                                                                                    now
                                                                                    ])
Run Code Online (Sandbox Code Playgroud)

Zor*_*org 9

你读过“Psycopg2 用法”吗?

如果没有,请不要犹豫。

警告 永远、永远、永远不要使用 Python 字符串连接 (+) 或字符串参数插值 (%) 将变量传递给 SQL 查询字符串。甚至不是在枪口下。

将参数传递给查询的正确方法如下所示:

 SQL = "INSERT INTO authors (name) VALUES (%s);" # Note: no quotes
 data = ("O'Reilly", )
 cur.execute(SQL, data) # Note: no % operator
Run Code Online (Sandbox Code Playgroud)

  • 我尝试使用 %s,但如果将变量传递到 (name) 字段,它会添加引号,这会破坏 SQL 代码。这就是我使用 {} 表示法的原因。 (2认同)

Clo*_*eto 6

ASISquote_ident

from psycopg2.extensions import AsIs, quote_ident

cur.execute("""
    INSERT INTO %s
    VALUES (%s, %s, %s, NULL, %s, %s, NULL);
""", (
    AsIs(quote_ident(database_name, cur)),
    str(i),
    str(j),
    float(tempPrice),
    datetime.date.today(),
    now
))
Run Code Online (Sandbox Code Playgroud)