gpt4 book ai didi

python sqlite 'NoneType' 对象不可迭代

转载 作者:行者123 更新时间:2023-12-01 02:50:06 25 4
gpt4 key购买 nike

我在 python 中使用sqlite3

我已经在数据库中创建了一个表。该表有一些 NULL

import sqlite3
engine = sqlite3.connect('Practice')
BST = pd.DataFrame({'N':[1,3,5,7,2,6,4,8,10,12,14,9,13,11,15],'P':[2,2,6,6,4,4,15,9,9,13,13,11,11,15,'NULL']})
BST.to_sql(name = "BST", con=engine, if_exists = 'replace', index = False)

然后我想创建这个表的 View

pd.read_sql_query("CREATE VIEW V1 as select P from BST",con=engine)  

但它会抛出错误TypeError: 'NoneType' object is not iterable

我想知道当表包含 NULL 值时,如何在 sqlite 中创建表的 View ?

最佳答案

代码应该是这样的:

import sqlite3
import pandas as pd
engine = sqlite3.connect('Practice')
BST = pd.DataFrame({'N':[1,3,5,7,2,6,4,8,10,12,14,9,13,11,15],'P':[2,2,6,6,4,4,15,9,9,13,13,11,11,15,'NULL']})
BST.to_sql(name = "BST", con=engine, if_exists = 'replace', index = False)

engine.execute("CREATE VIEW V1 as select P from BST")

print(pd.read_sql_query('SELECT * from V1', con=engine))

关于python sqlite 'NoneType' 对象不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892519/

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