- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个金钱支出监视器,并从 db 开始。我有一个用于构建表格的脚本:
def create_table(name):
conn = db.connect(dbname='Money_Tracker' ,user="",password="",host="",port="")
cur = conn.cursor()
cur.execute(f"CREATE TABLE IF NOT EXISTS {name}(ID SERIAL PRIMARY KEY ,F_Name VARCHAR, L_Name VARCHAR, Expense INT, Category VARCHAR )")
conn.commit()
conn.close()
并创建了一个表
create_table("money_spent")
其次,我构建了一个函数来将数据插入表中:
def add_money_spent(Firstname, Lastname, Amount, Cat):
conn = db.connect(dbname='Money_Tracker' ,user="",password="",host="",port="")
cur = conn.cursor()
cur.execute("INSERT INTO money_spent VALUES(%s, %s, %s, %s)",(Firstname,Lastname,Amount,Cat))
conn.close()
我尝试像这样使用它:
add_money_spent("Michael","Ben-Haym",15,"Cofee")
当我运行代码时,出现错误sycopg2.errors.InvalidTextRepresentation:整数类型的输入语法无效:“Cofee”第 1 行: ... INTO Money_spent VALUES(15, 'Michael', 'Ben-Haym', 'Cofee')
看来代码认为 cofee 应该是一个整数,即使我在表中指定类别应该是 varchar
有人可以向我解释一下我该如何解决这个问题吗?
谢谢:)
最佳答案
看来列顺序是随机的。所以你必须指定它:
cur.execute("""INSERT INTO money_spent (F_name, L_Name, Expense, Category)
VALUES(%s, %s, %s, %s);""",(Firstname,Lastname,Amount,Cat))
然后请确保提交您的更改:
conn.commit()
在关闭连接之前。
关于python - 我无法在 python 中将行插入到我的 psycopg2 脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59705614/
目前,我正在尝试配置 Django 以在项目中使用,并且在尝试运行 python manage.py syncdb 时遇到持续错误。 File "/x/x/x/x/x/x/base.py", line
sqlite 的 Python db-api 实现有一个方便的方法 executescript() 来执行多语句 SQL 脚本。它对于创建数据库非常有用。参见 sqlite driver docume
我有一个时间戳,我正在尝试使用 psycopg 将其插入到 postgres 中,但我无法解决问题,我认为这是因为我正在尝试使用 python 的字符串格式将日期放入正确的位置。我有这样的东西 val
我试图生成一些有效的 Postgresql 语句,但是 cursor.execute 方法以一种奇怪的方式解释我的参数。 这是我的代码切割: for key in data_dict.keys():
我在尝试插入数据库时遇到了问题: ur_psql.execute("""insert into smth(data, filedate, filedby)""" "
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 有关您编写的代码问题的问题必须在问题本身中描述具体问题——并包含有效代码以重现它。见
我们使用一个对象来保持与 PostgreSQL 数据库的连接并创建新的游标来处理请求。我观察到奇怪的行为:即使读取响应并关闭游标,请求仍然卡在数据库中,阻止更新表等。 当连接关闭时,它就消失了。 我了
我在更改我的 postgres 数据库中的表时遇到了一些问题。我正在使用 psycopg2 并使用 Python。我试图添加一个串行主键。它花了很长时间(大表),并且没有抛出任何错误,所以它做了一些事
当使用 psycopg 连接到 postgresql 数据库时,我拉了网线,没有出现任何错误。我如何在代码中检测到这一点以通知用户? 最佳答案 psycopg 无法检测到网络发生了什么。例如,如果您拔
我不断收到这个 error: psycopg2.ProgrammingError: column "someentry" does not exist. 当 someentry 不是列时,错误表明 s
我是 python 的初学者。我们使用这段代码来执行 SQL 命令。 cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100,
我正在处理 Udacity 的在线项目。我正在使用他们配置的 vagrant 来运行包含数据库的服务器。不幸的是,当我试图赋予代码持久性时,服务器每次都会返回错误。我是 python 的新手,所以请原
我用psycopg2创建数据库失败,语法错误,但在前几行使用相同的语法是行得通的,为什么要问?两行代码,为什么是第二行语法错误? [代码] db_name = 'series_id' self._cu
这个问题在这里已经有了答案: ValueError: operation parameter must be str or unicode (1 个回答) 关闭 4 年前。 我最近修改了我的插入查询
如何修复 Python 中的 SQL 语句? 数据库连接有效。但是,cur.execute 返回 none,这是错误的。 我的代码 import os, pg, sys, re, psycopg2 t
我正在尝试运行这样的代码: query = "copy (select email from my_table) TO 'STDOUT' WITH (FORMAT csv, DELIMITER '|
在 OSX Mojave 上安装了 Python3 和 Postgres 11.3,运行 pip install psycopg2 并收到以下冗长的错误消息。 据我所知,满足 psycopg 的要求,
有没有办法让 psycopg 和 postgres 无需重新建立连接就可以处理错误,比如 MySQLdb?下面的注释版本适用于 MySQLdb,注释使其适用于 Psycopg2: results =
我收到了一个来自野外的 unicode 字符串,它导致我们的一些 psycopg2 语句失败。 我已将问题简化为 SSCE: import psycopg2 conn = psycopg2.conne
我正在尝试在 Windows(Windows 7、64 位)下安装 psycopg2。我正在使用 Python 2.7.2 from Python(x,y)和 PostgreSQL 9.2.1。 我的
我是一名优秀的程序员,十分优秀!