简体   繁体   English

SQLite3不从数据库读取

[英]Sqlite3 not reading from database

I have a database with two columns .One is a primary key auto increment and another is a column for urls. 我有一个包含两列的数据库。一个是主键自动递增,另一个是用于URL的列。 the first is named 'id' and second is named 'longurls'. 第一个名为“ id”,第二个名为“ longurls”。 I have runned the following code to retrieve a 'id' record where longurls = 'X' value . 我已运行以下代码来检索longids ='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 . 这是一张图片,显示url存在于数据库中 Only 'none' is printed 仅打印“无”

I've not yet used sqlite in Python but the bit of your code that looks strange to me is 我尚未在Python中使用sqlite,但您的代码对我来说有点奇怪

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

what happens if you define id outside the loop? 如果在循环外定义id会怎样?

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

If you could return a list of ids then this will need further modification 如果您可以返回ID列表,则需要进一步修改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM