今天在用 TorMySQL 的时候遇到了一个问题,就是无法忽略表不存在的异常:
我的代码如下:
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/join", RegisterHandler),
(r"/", MainHandler),
(r"/websocket", EchoWebSocket),
]
settings = dict(
cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE_",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookie=True,
debug=True,
schema=os.path.join(os.path.dirname(__file__), "schema.sql"),
)
self.pool = tormysql.ConnectionPool(
max_connections = 20,
idle_seconds = 7200,
wait_connection_timeout = 3,
host = "127.0.0.1",
user = "chat",
password = "chat",
db = "chat",
charset = "utf8"
)
super(Application, self).__init__(handlers, **settings)
self.may_create_db()
@gen.coroutine
def may_create_db(self):
with (yield self.pool.Connection()) as conn:
try:
with conn.cursor() as cursor:
yield cursor.execute("SELECT * FROM user")
except pymysql.err.ProgrammingError as e:
logging.error(e)
subprocess.Popen(['mysql',
'--host={}'.format('127.0.0.1'),
'--database={}'.format('chat'),
'--user={}'.format('chat'),
'--password={}'.format('chat')],
stdin=open(self.settings['schema']))
except:
yield conn.rollback()
else:
yield conn.commit()
yield self.pool.close()
在 may_create_db
函数中,我会尝试去连接数据库,如果对应的表不存在的话,那么就会创建相关的表,但现在运行的时候遇到了一点问题,就是那个pymysql.err.ProgrammingError
还是会显示出来。
运行结果如下:
➜ /home/yundongx/gitroom/chat (dev) ✗ [chat]$ python app.py
2016-10-04 21:27:10,677 INFO: Server starts on port 8888
2016-10-04 21:27:10,704 ERROR: (1146, "Table 'chat.user' doesn't exist")
mysql: [Warning] Using a password on the command line interface can be insecure.
2016-10-04 21:27:10,710 ERROR: Future <tornado.concurrent.Future object at 0x7f39862ca2e8> exception was never retrieved: Traceback (most recent call last):
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/tornado/gen.py", line 1021, in run
yielded = self.gen.throw(*exc_info)
File "app.py", line 94, in may_create_db
yield cursor.execute("SELECT * FROM test")
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/tornado/gen.py", line 1015, in run
value = future.result()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/tornado/concurrent.py", line 237, in result
raise_exc_info(self._exc_info)
File "<string>", line 3, in raise_exc_info
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/tormysql/util.py", line 14, in finish
result = fun(*args, **kwargs)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 835, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 1019, in _read_query_result
result.read()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 1302, in read
first_packet = self.connection._read_packet()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 981, in _read_packet
packet.check_error()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1146, "Table 'chat.test' doesn't exist")
may_create_db
的方式是否正确?这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.