[英]'NoneType' object is not iterable Error when using Sqlite3 and python to run query
I wanted to create a table in a database on Jupyter Notebook using Python and sqlite3.我想使用 Python 和 sqlite3 在 Jupyter Notebook 上的数据库中创建一个表。 To avoid the table already exists error, I used the following code:
为了避免表已存在错误,我使用了以下代码:
q1 = '''
CREATE TABLE IF NOT EXISTS person (
person_id TEXT PRIMARY KEY,
first_name TEXT,
last_name TEXT
);
'''
run_query(q1)
However I received this error and I can't get rid of it.但是我收到了这个错误,我无法摆脱它。
TypeErrorTraceback (most recent call last)
<ipython-input-25-948d42611e56> in <module>()
7
8 '''
----> 9 run_query(q1)
<ipython-input-13-67c680d27fbe> in run_query(q)
3 def run_query(q):
4 with sqlite3.connect(db) as conn:
----> 5 return pd.read_sql(q,conn)
6
7 def run_command(c):
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/io/sql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
398 sql, index_col=index_col, params=params,
399 coerce_float=coerce_float, parse_dates=parse_dates,
--> 400 chunksize=chunksize)
401
402 try:
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/io/sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1443 args = _convert_params(sql, params)
1444 cursor = self.execute(*args)
-> 1445 columns = [col_desc[0] for col_desc in cursor.description]
1446
1447 if chunksize is not None:
TypeError: 'NoneType' object is not iterable
The table was created when I checked and the columns are there.该表是在我检查时创建的,并且列在那里。
Any help would be greatly appreciated.任何帮助将不胜感激。
run_query()
uses read_sql()
which was created to read rows from database, not to create tables or insert rows. run_query()
使用read_sql()
从数据库读取行,而不是创建表或插入行。 read_sql()
expects that query will get rows from database and it try to iterate these rows but CREATE
returns None
- and you get 'NoneType' object is not iterable
read_sql()
期望查询将从数据库中获取行并尝试迭代这些行,但CREATE
返回None
- 你得到'NoneType' object is not iterable
You should rather use directly execute(query)
- ie.您应该直接使用
execute(query)
- 即。
with sqlite3.connect(db) as conn:
conn.execute(q1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.