MySQL 数据表中, 如果 timestamp 类型的字段, 值为 0, python 从数据库读取数据后, 得到对象是什么类型, 是否为 None 呢?
下面来测试下.
创建数据表
首先创建数据表, 其中字段 pr_rule_update_time 为 timestamp 类型, 值为 0.
- CREATE TABLE `orange_service` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `service_group_name` varchar(100) NOT NULL,
- `pr_rule` tinyint(3) NOT NULL DEFAULT '-1',
- `pr_rule_update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
- `pr_rule_reason` varchar(256) NOT NULL DEFAULT '',
- `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`),
- UNIQUE KEY `uk_group_host_port` (`service_group_name`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据
向数据表中插入一条记录.
- MySQL> insert into orange_service(service_group_name, pr_rule, pr_rule_reason) values('test', 0, '');
- Query OK, 1 row affected (0.03 sec)
- MySQL> select * from orange_service\G
- *************************** 1. row ***************************
- id: 1
- service_group_name: test
- promotion_rule: 0
- promotion_rule_update_time: 0000-00-00 00:00:00
- promotion_rule_reason:
- create_time: 2019-10-08 16:52:19
- 1 row in set (0.03 sec)
python 读取数据
通过 python 读取数据.
- import MySQLdb
- host = "10.66.99.88"
- port = 5002
- sql = "select * from cmdb.orange_service"
- user = "test"
- passwd = "test123456"
- conn = MySQLdb.connect(host=host, port=port, user=user,passwd=passwd, connect_timeout=2, charset="utf8")
- cursor = conn.cursor()
- cursor.execute(sql)
- ret = cursor.fetchone()
- print(ret)
- cursor.close()
- conn.close()
- output:
- (1, 'test', 0, None, '', datetime.datetime(2019, 10, 8, 16, 52, 19))
从输出可以看到, timestamp 类型的字段, 通过 python 读取后, 值为 None.
来源: http://www.bubuko.com/infodetail-3237711.html