python 连接mysql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 连接mysql相关的知识,希望对你有一定的参考价值。
安装python后,还需要安装pymysql,安装步骤:
打开https://pypi.python.org/pypi/PyMySQL#downloads,下载PyMysql,下载后解压:
解压后,使用cmd,进入pymsql目录:
然后执行 python setup.py install 命令:
会看到命令窗会自动安装,等待安装成功。
安装成功后,进入python ide(eclipse)或者其他编辑器进行验证,示例代码:
import pymysql #地址 端口 用户 密码 数据库名 编码 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="afbxV1.0", charset="UTF8"); cur = conn.cursor();#连接 cur.execute("select * from tb_policy_info limit 10");#执行查询 for c in cur: #遍历游标 print(c); cur.close(); conn.close();
(1, ‘1234567‘, 113, 351, None, 237, -2, datetime.datetime(2016, 9, 8, 18, 14, 8), datetime.datetime(2016, 9, 9, 18, 14, 8), None, Decimal(‘10000.00‘), 59, 100, None, 1, None, datetime.datetime(2016, 9, 8, 18, 9, 21), datetime.datetime(2016, 9, 8, 18, 14, 10), None)
(3323, ‘123456‘, 94, 319, None, 355, 3, datetime.datetime(2016, 10, 17, 21, 24, 34), datetime.datetime(2016, 7, 18, 21, 25, 6), None, Decimal(‘123.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 18, 21, 26, 35), datetime.datetime(2016, 7, 18, 21, 28, 15), None)
(3324, ‘123456‘, 95, 320, None, 355, 3, datetime.datetime(2016, 10, 17, 21, 24, 34), None, None, Decimal(‘0.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 18, 21, 26, 35), datetime.datetime(2016, 7, 18, 21, 28, 15), None)
(3330, ‘12‘, 95, 320, None, 354, 3, datetime.datetime(2016, 10, 17, 17, 55, 28), datetime.datetime(2016, 7, 19, 10, 29, 47), None, Decimal(‘12.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 10, 31, 9), datetime.datetime(2016, 7, 19, 10, 31, 9), None)
(3347, ‘846513‘, 95, 320, None, 233, 3, datetime.datetime(2016, 7, 19, 11, 19, 57), None, None, Decimal(‘0.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 11, 43, 12), datetime.datetime(2016, 7, 19, 11, 45, 51), None)
(3348, ‘2345‘, 94, 319, None, 233, 3, datetime.datetime(2016, 7, 19, 11, 58, 3), datetime.datetime(2016, 7, 19, 11, 58, 19), None, Decimal(‘123.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 12, 0, 57), datetime.datetime(2016, 7, 19, 12, 1, 3), None)
(3349, ‘456423‘, 95, 320, None, 233, 3, datetime.datetime(2016, 7, 19, 11, 58, 3), datetime.datetime(2016, 7, 19, 11, 59, 3), None, Decimal(‘12.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 12, 0, 57), datetime.datetime(2016, 7, 19, 12, 1, 3), None)
(3350, ‘fghfjhfg‘, 94, 319, None, 233, 3, datetime.datetime(2016, 7, 19, 10, 31, 14), datetime.datetime(2016, 7, 19, 12, 0, 44), None, Decimal(‘123.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 12, 2, 6), datetime.datetime(2016, 7, 19, 12, 2, 6), None)
(3351, ‘E001‘, 94, 319, None, 308, 3, datetime.datetime(2016, 7, 19, 12, 10, 42), datetime.datetime(2016, 7, 19, 12, 11, 42), None, Decimal(‘100.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 12, 13, 23), datetime.datetime(2016, 7, 19, 12, 13, 42), None)
(3352, ‘E002‘, 95, 320, None, 308, 3, datetime.datetime(2016, 7, 19, 12, 10, 42), datetime.datetime(2016, 7, 19, 12, 12, 20), None, Decimal(‘111.00‘), 59, 104, None, 3, None, datetime.datetime(2016, 7, 19, 12, 13, 23), datetime.datetime(2016, 7, 19, 12, 13, 42), None)
打印数据,验证成功。
以上是关于python 连接mysql的主要内容,如果未能解决你的问题,请参考以下文章