【发布时间】:2014-02-05 05:42:11
【问题描述】:
这是我的代码,它使调用(除了插入的最后一行之外,一切都很好)所有必需的导入都在那里并且工作。查询一定有问题。
db = Database()
soup = bs(mytrades)
for row in soup.findAll("tr"):
cols = row.findAll("td")
data = []
for col in cols:
data.append(col.text)
query = """INSERT INTO zulutrades VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s), (128391,"""+data[0]+""","""+data[1]+""","""+data[2]+""","""+data[3]+""","""+data[4]+""","""+data[5]+""","""+data[6]+""","""+data[7]+""","""+data[8]+""","""+data[9]+""","""+data[10]+""")"""
db.insert(query)
*“错误”(我没有发布它是因为我认为它没有多大意义)*
Exception in thread Thread-192 (most likely raised during interpreter shutdown):Exception in thread Thread-2 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 763, in run
File "/usr/local/lib/python2.7/dist-packages/windmill-1.6-py2.7.egg/windmill/server/https.py", line 401, in start
File "/usr/lib/python2.7/SocketServer.py", line 280, in handle_request
File "/usr/lib/python2.7/SocketServer.py", line 291, in _handle_request_noblock
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'error'
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 763, in run
File "/usr/lib/python2.7/SocketServer.py", line 597, in process_request_thread
File "/usr/lib/python2.7/SocketServer.py", line 471, in shutdown_request
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'error'
我正在使用以下 mysql 数据库类:
class Database:
host = 'localhost'
user = 'wind'
password = 'mill'
db = 'windmill'
def __init__(self):
self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db)
self.cursor = self.connection.cursor()
def insert(self, query):
try:
self.cursor.execute(query)
self.connection.commit()
except:
self.connection.rollback()
def query(self, query):
cursor = self.connection.cursor( MySQLdb.cursors.DictCursor )
cursor.execute(query)
return cursor.fetchall()
def __del__(self):
self.connection.close()
这是 mysql 表
CREATE TABLE IF NOT EXISTS `zulutrades` (
`id` int(10) NOT NULL,
`currency` varchar(8) NOT NULL,
`type` varchar(8) NOT NULL,
`std_lots` int(8) NOT NULL,
`date_open` varchar(20) NOT NULL,
`date_closed` varchar(20) NOT NULL,
`open_close` varchar(20) NOT NULL,
`high` float NOT NULL,
`low` float NOT NULL,
`roll` float NOT NULL,
`profit` varchar(10) NOT NULL,
`total` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
【问题讨论】:
-
您的问题是什么?我的是“最后一行到底发生了什么?”
-
在那里,我发布了回溯——尽管我认为它意义不大。就像我写的那样,问题似乎出在查询上。没有任何东西被插入到数据库中。
-
finaldata=[1278391]+data[:10] "插入 zulutrades 值 (%s,%s,%s,%s,%s,%s,%s,%s,% s,%s,%s,%s)"%tuple(finaldata)
-
我试过你的行,它给出了错误:TypeError: not enough arguments for format string
-
zulutrades表的结构是什么?