简体   繁体   English

使用Sqlite3和python运行查询时'NoneType'对象不可迭代错误

[英]'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.

相关问题 python sqlite&#39;NoneType&#39;对象不可迭代 - python sqlite 'NoneType' object is not iterable Python sqlite3 NoneType错误 - Python sqlite3 NoneType error 'NoneType' object is not iterable error in Python - 'NoneType' object is not iterable error in Python Python-“&#39;NoneType&#39;对象不可迭代”错误 - Python - “'NoneType' object is not iterable” error Python Pandas read_sql_query “'NoneType' object 不可迭代”错误 - Python Pandas read_sql_query “'NoneType' object is not iterable” error Python Sqlite3 TypeError: 'NoneType' object 不可下标 - Python Sqlite3 TypeError: 'NoneType' object is not subscriptable 尝试使用熊猫读取SQL查询时出现“&#39;NoneType&#39;对象不可迭代”错误 - “'NoneType' object is not iterable” error when trying to read sql query with pandas 提取 rar 文件时出现 python 错误:“NoneType”对象不可迭代 - python error when extracting a rar file: 'NoneType' object is not iterable TypeError:“ NoneType”对象不可迭代 当我在python中使用生成器时 - TypeError: 'NoneType' object is not iterable | when I using generator in python 使用文件输入 python 模块从文件中读取行时出现“'NoneType' object 不可迭代”错误 - “ 'NoneType' object is not iterable” error when using fileinput python module to read lines from file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM