这是我调用的代码(一切都很好,除了插入的最后一行)所有必需的导入都在那里工作。查询一定有问题。
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;
答案 0 :(得分:1)
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]+""")"""
在这里,您要为两个行构建INSERT
查询(请注意VALUES
子句后的两个带括号的组);第一行仅包含12个占位符,而第二行由在查询字符串中直接连接的12个值组成。此查询不会成功,因为您永远不会在cursor.execute(query)
方法的Database.query
调用中为占位符提供值。
您需要重写Database.query
和Database.insert
方法,以支持将查询参数传递给cursor.execute
方法:
class Database:
...
def insert(self, query, params):
try:
self.cursor.execute(query, params)
self.connection.commit()
except:
self.connection.rollback()
def query(self, query, params):
cursor = self.connection.cursor( MySQLdb.cursors.DictCursor )
cursor.execute(query, params)
return cursor.fetchall()
但是,你的循环也是错误的。您正在为INSERT
返回的每个单元执行rows.findAll('td')
查询,不为{返回的每个行执行soup.findAll('tr')
查询{1}}。 INSERT
应该发生在外部循环中,而不是内部:
query = 'INSERT INTO zulutrades VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
for row in soup.findAll("tr"):
cols = row.findAll("td")
data = []
for col in cols:
data.append(col.text)
db.insert(query, [128391] + data)
如您所见,查询字符串本身不再需要在循环体内定义,因为它不再为您要插入的每一行更改 - 只有执行的参数现在更改,但这些参数作为一个提供将参数分隔为db.insert
。