简体   繁体   English

Python和SQLite:在字符串格式化错误期间,并非所有参数都已转换

[英]Python and SQLite: Not all arguments converted during string formatting error

I have seen a few of these errors on Stackoverflow but non of the answers seem to be working when I implement them. 我在Stackoverflow上看到了一些这样的错误,但是当我实现它们时,所有答案似乎都不起作用。

import csv, sqlite, sys

conn = sqlite.connect('Database_Practice.db')
mouse = conn.cursor()

input = open('DATA.csv','rb')
my_reader = csv.reader(input, delimiter = ',',quotechar='|')

mouse.execute("DROP TABLE IF EXISTS Example_Input;")
mouse.execute("CREATE TABLE Example_Input (ID INTEGER, Name VARCHAR, Job VARCHAR, Salary INTEGER);")

try:
    to_database = [entry for entry in my_reader]
    for block in to_database:
        mouse.execute("INSERT INTO Example_Input (ID,Name,Job,Salary) VALUES (?,?,?,?)", block)
finally:
    conn.commit()
    input.close()

I know the variable is getting the variables, as if I comment out the INSERT line I get the output: 我知道变量正在获取变量,就像我注释掉INSERT行一样,得到输出:

['1', 'Billy', 'Tech', '888']
['2', 'Jane', 'Sales', '777']
['3', 'Scott', 'Tech', '667']
['4', 'Harris', 'Tech', '444']
['5', 'Gray Skull', 'Sales', '333']
['6', 'Barbarosa', 'Assistant', '200']
['7', 'Nick', 'Janitor', '100']

but after a long time of trying to tweak it, I can't seem to get it to stop giving me the error: 但是经过长时间的尝试来调整它之后,我似乎无法阻止它给出错误:

 File "Another_Try.py", line 18, in ?
    mouse.execute("INSERT INTO Example_Input (ID,Name,Job,Salary) VALUES (?,?,?,?)", block[0:4])
  File "/usr/lib64/python2.4/site-packages/sqlite/main.py", line 255, in execute
    self.rs = self.con.db.execute(SQL % parms)
TypeError: not all arguments converted during string formatting

Any advice? 有什么建议吗?

EDIT: 编辑:

try:
    to_database = [entry for entry in my_reader]
    for block in to_database:
#       mouse.execute("INSERT INTO Example_Input (ID,Name,Job,Salary) VALUES (?,?,?,?)", block)
        print block

I ran your code, replaced sqlite with sqlite3, and appended this: 我运行了您的代码,将sqlite替换为sqlite3,并附加了以下内容:

print mouse.execute("select * from Example_Input").fetchall()

It appears to work with reasonable CSV. 它似乎可以与合理的CSV一起使用。 Maybe you should check if the CSV is actually what you are expecting. 也许您应该检查CSV是否确实是您所期望的。 If the quotes aren't right, you could end up getting more than four columns, and that would explain the error. 如果引用不正确,最终可能会得到四列以上,这将解释该错误。

Incidently, it is handier to replace the loop with this: 顺便说一句,用以下代码替换循环比较方便:

try:
    mouse.executemany("INSERT INTO Example_Input (ID,Name,Job,Salary) VALUES (?,?,?,?)", my_reader)
finally:
    conn.commit()
    i.close()

暂无
暂无

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

相关问题 SQLite和Python,TypeError:并非在字符串格式化期间转换了所有参数 - SQLite and Python, TypeError: not all arguments converted during string formatting 带有Selenium错误TypeError的Python:在字符串格式化期间并非所有参数都已转换 - Python with Selenium error TypeError: not all arguments converted during string formatting MYSQLdb / Python-并非在字符串格式化错误期间转换了所有参数? - MYSQLdb/Python - not all arguments converted during string formatting error? python错误TypeError:并非所有参数都在字符串格式化期间转换 - python error TypeError: not all arguments converted during string formatting Python错误:在字符串格式化期间,并非所有参数都已转换 - Python Error: Not all arguments converted during string formatting TypeError: not all arguments converted during string formatting 错误 python - TypeError: not all arguments converted during string formatting error python Python 错误:类型错误:并非所有参数都在字符串格式化期间转换 - Python Error: TypeError: not all arguments converted during string formatting Python Folium 错误:“并非所有 arguments 在字符串格式化期间都已转换” - Python Folium Error: “not all arguments converted during string formatting” Python错误:在字符串格式化期间,并非所有参数都已转换 - Python error: Not all arguments converted during string formatting TypeError:在字符串格式化期间并非所有参数都转换为元组python中的错误 - TypeError: not all arguments converted during string formatting Error in tuple python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM