windows下python3.6连接mysql数据库

windows下python3.6连接mysql数据库

转载 2017年07月14日 14:38:15
  • 752

pymysql支持python3.6

通过pip安装pymysql:

进入python根目录Scrips目录下

 

[plain]  view plain copy
 
  1. pip install pymysql  
pymysql自动安装完成

示例代码:

 

[python]  view plain copy
 
  1. # -*- coding: UTF-8 -*-  
  2. import pymysql  
  3. import  pymysql.cursors  
  4.   
  5. connection = pymysql.connect(host='localhost',  
  6.                              user='root',  
  7.                              password='root',  
  8.                              db='test',  
  9.                              port=3306,  
  10.                              charset='utf8')#注意是utf8不是utf-8  
  11. try:  
  12.     with connection.cursor() as cursor:  
  13.         sql_1 = 'select * from student'  
  14.         cout_1=cursor.execute(sql_1)  
  15.         print("数量: "+str(cout_1))  
  16.         for row in cursor.fetchall():  
  17.             print("id:",str(row[0]),'name',str(row[1]),'age',str(row[2]))  
  18.           
  19.         sql_2 = 'insert into student(name,age,sex) value("田晓霞",26,"男")'  
  20.         cout_2=cursor.execute(sql_2)  
  21.         print("数量: "+str(cout_2))  
  22.         connection.commit()  
  23. finally:  
  24.     connection.close()  

注意:

connection 连接设置编码是utf8,而不是utf-8

 

转载于:https://www.cnblogs.com/Bob-zwj/p/8228236.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值