简体   繁体   English

"Python SQLite 查询总是返回 None"

[英]Python SQLite query always returns None

I have a SQL-file (SQLite format 3) that I can query with the DB Browser for SQLite (Windows).我有一个 SQL 文件(SQLite 格式 3),可以使用 DB Browser for SQLite (Windows) 进行查询。 Whenever I use Python to access the db I get a Null result.每当我使用 Python 访问数据库时,我都会得到 Null 结果。

import sqlite3
conn = sqlite3.connect('C:/tmp/test.sql')
cursor = conn.cursor()

conn.execute('select count(*) from Player')
print("result is:", cursor.fetchone()) # result is: None
import sqlite3

connection = sqlite3.connect(database_name)
cursor = connection.cursor()

cursor.execute("select val from table_name where x = 'something';")
result = cursor.fetchone()

# directly returning result also gives null
if result:
    return result[0]  # tuple returned in result

cursor.close()
connection.close()

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

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