gpt4 book ai didi

python - Sqlite3不从数据库读取

转载 作者:行者123 更新时间:2023-11-29 20:57:20 24 4
gpt4 key购买 nike

我有一个有两列的数据库。一列是主键自动增量,另一列是网址列。第一个名为“id”,第二个名为“longurls”。我运行了以下代码来检索 longurls = 'X' value 的 'id' 记录。

import sqlite3
long_url = 'http://www.google.com'
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT id FROM stuff WHERE longurls=?",(long_url,))
k = c.fetchall()
for stuff in k:
id = stuff[0]
print (id)

This is a picture showing the url exists in database 。仅打印“无”

最佳答案

我还没有在 Python 中使用过 sqlite,但你的代码中有一点对我来说看起来很奇怪

for stuff in k:
id = stuff[0]
print (id)

如果在循环外定义 id 会发生什么?

id = 0
for stuff in k:
id = stuff[0]
print (id)

如果您可以返回 id 列表,那么这将需要进一步修改

关于python - Sqlite3不从数据库读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37522919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com