MySQLdb返回结果格式

数据表格式

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> 

 

查询数据库

self.cur.execute('select * from BlogPassage')
return self.cur.fetchall()

 

结果

'alu01', 6L, 'hello world', 'my first blog', datetime.datetime(2016, 3, 10, 13, 54, 18)

从结果来看,int型转为了长整形,时间转为了python的datetime.datetime,均不是我期望的

 

解决方法

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()

 

结果

'alu01', 6, 'hello world', 'my first blog', '2016-03-10 13:54:18'

 

参考:http://mysql-python.sourceforge.net/MySQLdb.html

posted on 2016-03-10 14:35  onmyway227  阅读(550)  评论(0编辑  收藏  举报

导航