python3.x中连接并使用mysql数据库
2017-03-09 13:45
417 查看
python中连接mysql主要由两个包。
其中python2.X版本使用MySQLdb,python3.X中使用pymysql。
本文主要介绍在python3.X中使用pymysql。
第一步:安装pymysql包。
1、我用的是PyCharm,点击File ->Setting-> Project Interpreter
选择右侧的符号“+”,会弹出如下界面:
搜索pymysql,选择后 选中左下角勾上,点击Install Package,等待几秒,安装成功后如下图所示:
2、如果未安装任何IDE,只是安装python,可用此方法:
从 https://pypi.python.org/pypi/PyMySQL3 下载安装包并解压到指定的路径
在命令行模式下进入安装目录,执行python
setup.py install命令
第二步:MySQL数据库连接代码
其中python2.X版本使用MySQLdb,python3.X中使用pymysql。
本文主要介绍在python3.X中使用pymysql。
第一步:安装pymysql包。
1、我用的是PyCharm,点击File ->Setting-> Project Interpreter
选择右侧的符号“+”,会弹出如下界面:
搜索pymysql,选择后 选中左下角勾上,点击Install Package,等待几秒,安装成功后如下图所示:
2、如果未安装任何IDE,只是安装python,可用此方法:
从 https://pypi.python.org/pypi/PyMySQL3 下载安装包并解压到指定的路径
在命令行模式下进入安装目录,执行python
setup.py install命令
第二步:MySQL数据库连接代码
conn = pymysql.connect(host='192.168.1.2', port=3306, user='root', passwd='test', db='test') cur = conn.cursor()
查询代码
def get_MySQLSelect(self):
conn = pymysql.connect(host='192.168.1.2', port=3306, user='root', passwd='test', db='test') cur = conn.cursor()
cur.execute("select * from table_name")
result = cur.fetchall()
return result
conn.commit()
cur.close()
conn.close()
插入代码
def get_MySQLdb(self):
conn = pymysql.connect(host='192.168.1.2', port=3306, user='root', passwd='test', db='test') cur = conn.cursor()
cur.execute("insert into table_name(xh,name,link,notes) VALUES (2,'office','3','4')")
conn.commit()
cur.close()
conn.close()
相关文章推荐
- Python3.x使用PyMysql连接MySQL数据库
- python3.x中连接并使用mysql数据库
- python3.4 使用pymysql 连接mysql数据库
- Python中使用SQLAlchemy连接Mysql数据库2(多表连接操作)
- python3使用pymysql连接mysql数据库的大坑
- python使用sqlalchemy连接mysql数据库
- 使用Python连接MySQL数据库
- python3使用PyMysql连接mysql数据库实例
- Python3.x的mysqlclient的安装、Python操作mysql,python连接MySQL数据库,python创建数据库表,带有事务的操作,CRUD
- Python进阶(二十二)-Python3使用PyMysql连接mysql数据库
- ubuntu14环境下使用python连接MySQL数据库
- Python连接mysql数据库及python使用mysqldb连接数据库教程
- 【python学习笔记】python使用sqlalchemy连接mysql数据库
- ubuntu下使用Python连接Mysql数据库和Mongodb数据库
- PyMySQL-在Python 3.x下连接MySQL数据库的方法
- python使用pymysql连接mysql数据库
- Python中使用SQLAlchemy连接Mysql数据库(单表操作)
- Python的ORM框架SQLAlchemy使用入门(二)【连接MySql数据库】
- python使用mysqldb连接mysql数据库
- Python的ORM框架SQLAlchemy使用入门(二)【连接MySql数据库】