Python+;Sqlite:无法确定提供的绑定数量不正确的原因错误
我会马上开始的-我首先为自己创建了一个本地数据库:Python+;Sqlite:无法确定提供的绑定数量不正确的原因错误,python,sqlite,Python,Sqlite,我会马上开始的-我首先为自己创建了一个本地数据库: import sqlite3 conn = sqlite3.connect("tofire.db") # cursor = conn.cursor() # create a table cursor.execute("""CREATE TABLE incidents (Id INTEGER PRIMARY KEY, prime_street text, cross_street text, dispa
import sqlite3
conn = sqlite3.connect("tofire.db") #
cursor = conn.cursor()
# create a table
cursor.execute("""CREATE TABLE incidents
(Id INTEGER PRIMARY KEY, prime_street text, cross_street text, dispatch_time text,
incident_number text, incident_type text, alarm_level text, area text, dispatched_units text, date_added text)
""")
这一切顺利完成——下一部分是我的函数,它使用漂亮的汤将一张桌子刮成一个列表。然后,我尝试将每个子列表中的信息写入sqlite数据库
# Toronto Fire Calls
import urllib2
import sqlite3
import time
import csv
import threading
from bs4 import BeautifulSoup
# Beautiful Soup imports the URL as html
def getincidents ():
response = urllib2.urlopen('http://www.toronto.ca/fire/cadinfo/livecad.htm')
html = response.read()
# We give the html its own variable.
soup = BeautifulSoup(html)
# Find the table we want on the Toronto Fire Page
table = soup.find("table", class_="info")
# Find all the <td> tags in the table and assign them to variable.
cols = table.find_all('td')
# Find the length of rows, which is the number of <font> tags, and assign it to a variable num_cols.
num_cols = len(cols)
# Create an empty list to hold each of the <font> tags as an element
colslist = []
totalcols = 0
# For each <font> in cols, append it to colslist as an element.
for col in cols:
colslist.append(col.string)
totalcols = len(colslist)
# Now colslist has every td as an element from [0] to totalcols = len(colslist)
# The First 8 <font> entries are always the table headers i.e. Prime Street, Cross Street, etc.
headers = colslist[0:8]
# Prime Street
# Cross Street
# Dispatch Time
# Incident Number
# Incident Type
# Alarm Level
# Area
# Dispatched Units
# Get the indexes from 0 to the length of the original list, in steps of list_size, then create a sublist for each.
# lists = [original_list[i:i+list_size] for i in xrange(0, len(original_list), list_size)]
list_size = 8
i = 0
incidents = [colslist[i:i+list_size] for i in xrange(0, len(colslist), list_size)]
# Works!
num_inci = len(incidents) # Get the number of incidents
added = time.strftime("%Y-%m-%d %H:%M")
update = 'DB Updated @ ' + added
# SQL TIME, Connect to our db.
conn = sqlite3.connect("tofire.db")
cursor = conn.cursor()
lid = cursor.lastrowid
# Now we put each incident into our database.
for incident in incidents[1:num_inci]:
incident.append(added)
to_db = [(i[0:10]) for i in incident]
import ipdb; ipdb.set_trace()
cursor.executemany("INSERT INTO incidents (prime_street, cross_street, dispatch_time, incident_number, incident_type, alarm_level, area, dispatched_units, date_added) VALUES (?,?,?,?,?,?,?,?,?)", to_db)
conn.commit()
print update
print "The last Id of the inserted row is %d" % lid
threading.Timer(300, getincidents).start()
getincidents()
多伦多火警电话
导入urllib2
导入sqlite3
导入时间
导入csv
导入线程
从bs4导入BeautifulSoup
#Beauty Soup将URL导入为html
def getEvents():
response=urllib2.urlopen('http://www.toronto.ca/fire/cadinfo/livecad.htm')
html=response.read()
#我们为html提供了自己的变量。
soup=BeautifulSoup(html)
#在多伦多消防网页上找到我们想要的桌子
table=soup.find(“table”,class=“info”)
#找到表中的所有标记并将它们分配给变量。
cols=表。查找所有('td'))
#找到行的长度,即标记的数量,并将其分配给变量num_cols。
num_cols=len(cols)
#创建一个空列表,将每个标记作为一个元素保存
colslist=[]
totalcols=0
#对于cols中的每个元素,将其作为元素附加到colslist。
对于col中的col:
colslist.append(col.string)
totalcols=len(colslist)
#现在,colslist将每个td作为从[0]到totalcols=len(colslist)的元素
#前8个条目始终是表格标题,即Prime Street、Cross Street等。
headers=colslist[0:8]
#黄金街
#十字街
#调度时间
#事件编号
#事件类型
#警报级别
#区域
#派遣单位
#获取从0到原始列表长度的索引,以列表大小为单位,然后为每个索引创建一个子列表。
#lists=[原始列表[i:i+列表大小]用于xrange中的i(0,len(原始列表),列表大小)]
列表大小=8
i=0
事件=[colslist[i:i+列表大小]表示xrange中的i(0,len(colslist),列表大小)]
#工作!
num_inci=len(事件)#获取事件数
added=time.strftime(“%Y-%m-%d%H:%m”)
更新='DB Updated@'+添加
#SQL时间,连接到我们的数据库。
conn=sqlite3.connect(“tofire.db”)
游标=连接游标()
lid=cursor.lastrowid
#现在,我们将每个事件都存入数据库。
对于事件中的事件[1:num_inci]:
事件。追加(已添加)
事件中i的to_db=[(i[0:10])]
进口ipdb;ipdb.set_trace()
cursor.executemany(“插入事件(主要街道、交叉街道、调度时间、事件编号、事件类型、报警级别、区域、调度单元、添加日期)值(?,,,,,,,,,,,,,?)”到数据库)
康涅狄格州提交
打印更新
打印“插入行的最后一个Id为%d”%lid
threading.Timer(300,getEvents.start())
获取事件()
我总是以错误消息“提供的绑定数不正确”结束,它声称我试图在提供10个绑定时在语句中使用9。我试图缩小造成这种情况的原因,但没有成功。您为一个insert语句提供了10个元素,而您定义的insert语句只包含9个元素 或者
to_db = [(i[0:9]) for i in incident] #or
to_db = [(i[1:10]) for i in incident]
会给你九个元素。。。哪一个匹配的数字?insert语句中的标记(以及正在填充的字段数)
我假设这是您想要的您为一个insert语句提供了10个元素,而您定义的insert语句只包含9个元素 或者
to_db = [(i[0:9]) for i in incident] #or
to_db = [(i[1:10]) for i in incident]
会给你九个元素。。。哪一个匹配的数字?insert语句中的标记(以及正在填充的字段数)
我假设这就是你想要的正如Ned Batchelder最近所说,“调试的第一条规则:当有疑问时,打印更多内容。”在你将
添加到事件
后,事件本身有9项:
print(incident)
# [u'NORFINCH DR, NY', u'FINCH AVE W / HEPC', u'2012-12-09 17:32:57', u'F12118758', u'Medical - Other', u'0', u'142', u'\r\nP142, \r\n\r\n', '2012-12-09 17:46']
因此,看起来您真正需要做的就是使用incident
作为cursor.execute
的第二个参数。或者,如果您想消除u'\r\nP142\r\n\r\n'
等项目周围的一些空白,
你可以用
to_db = [i.strip() for i in incident]
正如Ned Batcheld最近所说,“调试的第一条规则:当有疑问时,打印更多内容。”在将添加到事件中后,事件本身有9项:
print(incident)
# [u'NORFINCH DR, NY', u'FINCH AVE W / HEPC', u'2012-12-09 17:32:57', u'F12118758', u'Medical - Other', u'0', u'142', u'\r\nP142, \r\n\r\n', '2012-12-09 17:46']
因此,看起来您真正需要做的就是使用incident
作为cursor.execute
的第二个参数。或者,如果您想消除u'\r\nP142\r\n\r\n'
等项目周围的一些空白,
你可以用
to_db = [i.strip() for i in incident]
你的陈述有9个问号,不是10个。你的陈述有9个问号,不是10个。谢谢你的建议-更奇怪;当我使用I[0:9]时,它会出错为“语句使用9,当提供8时”,当我使用[1:10]时,它会出错为“语句使用9,并且有1个提供”。在执行许多操作之前,请尝试打印映射(len,to_db)
。。也许是一个打印到_db
谢谢你的建议-更奇怪;当我使用I[0:9]时,它会出错为“语句使用9,当提供8时”,当我使用[1:10]时,它会出错为“语句使用9,并且有1个提供”。在执行许多操作之前,请尝试打印映射(len,to_db)
。。也许是一个print to_db
unutbu——感谢数百万人的支持——我有一种感觉,只要再做一点调整,它就可以工作了;它抛出了一个缩进错误:unindent与任何外部缩进级别都不匹配
-你有经验的眼睛能看到我可能出错的地方吗?嗯,这是一个缩进错误,这意味着我必须查看你的实际代码才能找到它。注意选项卡看起来像空格——您应该坚持使用所有选项卡或所有空格。不要把它们混在一起。您可以尝试使用python-tscript.py运行脚本,让python告诉您制表符和空格的混合是否是问题的根源