【发布时间】:2019-11-04 03:12:32
【问题描述】:
我正在尝试从 spyder 插入内容。我成功连接到数据库。然后错误发生了。
connet = psycopg2.connect(database="sample",
user="users",password="123456",host="localhost",port="5432")
cursor = connet.cursor()
def downloadPage(url):
content=''
response = requests.get(url)
response.encoding = 'UTF-8'
bs = BeautifulSoup(response.text,'lxml')
title = bs.find('h1').text
div = bs.find('div',attrs={'class':'neirong'})
ps = div.findAll('p')
for p in ps:
content+=p.text+'\n'
return title,content
for url in urls:
title,content = downloadPage(url)
print(title)
sql = "insert into wenzhang(title,conten) values(title,content)";
cursor.execute(sql)
错误:
psycopg2.errors.UndefinedColumn: column "title" does not exist
LINE 1: insert into wenzhang(title,conten) values(title,content)
^
提示:“wenzhang”表中有一个名为“title”的列,但不能从这部分查询中引用。
【问题讨论】:
-
您能详细说明您要插入的值吗?它们在哪里定义?
-
sql = "插入文章(title,conten) 值(%s,%s)".format(title,conten).使用这种方式将变量插入查询中,我觉得错误是因为值而不是因为列名。
-
@Murali 不要那样做。这是 SQL 注入的秘诀。
-
@melpomene 是的,对不起,我的错,他可以通过它来执行你所说的功能
标签: python postgresql