數據表格式html
mysql> select * from BlogPassage; +----------+--------+-------------+---------------+---------------------+ | blogUser | blogId | blogTitle | blogContent | createTime | +----------+--------+-------------+---------------+---------------------+ | alu01 | 6 | hello world | my first blog | 2016-03-10 13:54:18 | +----------+--------+-------------+---------------+---------------------+ 1 row in set (0.00 sec) mysql> desc BlogPassage; +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | blogUser | varchar(20) | YES | MUL | NULL | | | blogId | int(11) | NO | PRI | NULL | auto_increment | | blogTitle | varchar(100) | NO | | NULL | | | blogContent | text | NO | | NULL | | | createTime | datetime | NO | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.01 sec) mysql>
查詢數據庫python
self.cur.execute('select * from BlogPassage') return self.cur.fetchall()
結果mysql
'alu01', 6L, 'hello world', 'my first blog', datetime.datetime(2016, 3, 10, 13, 54, 18)
從結果來看,int型轉爲了長整形,時間轉爲了python的datetime.datetime,均不是我指望的sql
解決方法數據庫
from MySQLdb.constants import FIELD_TYPE myConv = {FIELD_TYPE.LONG: int, FIELD_TYPE.DATETIME: str}
self.conn = MySQLdb.connect( host = 'localhost', port = 3306, user = 'root', passwd = 'admin_root', db = db, conv=myConv) self.cur.execute('select * from BlogPassage') return self.cur.fetchall()
結果fetch
'alu01', 6, 'hello world', 'my first blog', '2016-03-10 13:54:18'