flask postgresql错误:“

时间:2018-09-27 15:36:07

标签: python postgresql sqlalchemy psycopg2

我对flask和dev还是陌生的,所以这个错误是很主观的,即使在stackoverflow上看到几则帖子后我也无法搜索到google,我无法弄清一切正确,从字面上看,我正在拉起头发

@app.route("/bookPage/<all>" , methods=["GET" , "POST"])
def bookPage(all):
    global all2
    if request.method == "POST":

        #variables
        list1 = ['101','milk','3','jack','2018-09-27 16:33:58.553349' ]

        db.execute("INSERT INTO review_table (id, review, rate , user , time_t) VALUES (:id, :review, :rate , :user , :time_t)" , {"id" : list1[0], "review" : list1[1], "rate": list1[2] ,"user":list1[3] , "time_t" :list1[4] })
        db.commit()
        print( f"review has been added of value : ,{review} \n ,and rate of :  , { rate_i} ,\n , by user : , { user_i }, \n, and Id : , {id_i}" )
    else:

        print("\n\n",all,"\n\n")
        all1 = all.split(",")
        all2 =[a.strip("()").replace("'","") for a in all1]




    return render_template("bookPage.html",all1=all2)

每次执行时,都会出现此错误(错误:“用户”处或附近的语法错误),他指出

中的“用户”
"INSERT INTO review_table (id, review, rate , user , time_t)

一切正确,我是nw,但是我已经执行了多次数据库

db中的表是列的空PostgreSQL

列类型注释

  • id整数
  • 查看字符为NULL
  • 将整数设为NULL
  • 用户字符为NULL
  • time_t无时区NULL的时间

    请让我知道为什么这种情况不断发生 我已经用常量字符串“ test”更改了用户输入,但我遇到了同样的错误... 我认为错误会误导

2 个答案:

答案 0 :(得分:0)

您需要先将值转换为正确的类型,然后再将其发送到数据库。

    user_i = ''
    rate_i = request.form.get("rate", type=int)
    user_i = session['user']
    time_i = datetime.now()
    id_i = int(all2[0])

    db.execute("INSERT INTO review_table (id, review, rate , user , time_t) VALUES (?, ?, ? , ? , ?)" , (id_i, review_i, rate_i , user_i , time_i ))

答案 1 :(得分:-1)

让您的数据库在字段中填写时间和日期,并将其添加到您的CREAT TABLE中:

time_t time without time zone DEFAULT current_timestamp

在您的INSERT中不要使用time_t:

    db.execute("INSERT INTO review_table (id, review, rate , user ) VALUES (:id, :review, :rate , :user )" , {"id" : list1[0], "review" : list1[1], "rate": list1[2] ,"user":list1[3] })

您可以找到此解决方案的其他变体:Sqlite: CURRENT_TIMESTAMP is in GMT, not the timezone of the machine