码迷,mamicode.com
首页 > 数据库 > 详细

python3连接mysql

时间:2016-10-14 22:48:57      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

1. 安装PyMysql;

2.mac版如下:

 1 # -*- coding: utf-8 -*-
 2 import pymysql
 3 # 密码我用*打马赛克啦 哇哈哈
 4 conn = pymysql.connect(host = 127.0.0.1, unix_socket = /tmp/mysql.sock, user = root , password = ******, db = mysql, charset = utf8s)
 5 cur = conn.cursor()
 6 cur.execute("USE mysql")
 7 
 8 cur.execute("select * from user")
 9 print(cur.fetchone())
10 cur.close()
11 conn.close()

3. windows版如下:

注意事项:1.port后面写int而不是str;

     2.添加charset=‘utf8‘以保证查出的中文字符能正常显示;

 1 # -*- coding: utf-8 -*-
 2 import pymysql
 3 
 4 conn = pymysql.connect(host = XXXXXX, port = 1234, user = **** , password = *****, db = xxxx, charset = utf8)
 5 cur = conn.cursor()
 6 cur.execute("USE xxxx")
 7 
 8 cur.execute("SELECT * from  xxxx where id = 1039")
 9 print(cur.fetchone())
10 cur.close()
11 conn.close()

 

python3连接mysql

标签:

原文地址:http://www.cnblogs.com/vivivi/p/5961972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!