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

Ubuntu中python链接本地数据库

时间:2018-08-20 01:13:15      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:utf8mb4   127.0.0.1   apt-get   本地数据库   依赖包   ldb   使用   tool   print   

由于python链接数据库需要下载DB API模块:例如你需要访问Mysql数据,你需要MySQL数据库模块。

DB-API是一个规范。 以便为不同的底层数据库系统和数据库接口程序提供一致的访问接口。

Python的DB-API,为大多数的数据库实现了接口,使用它连接各数据库后,就可以用相同的方式操作各数据库。

1,在Ubuntu安装MySQL数据库模块需要先安装依赖包,命令如下:

sudo apt-get install libmysqlclient-dev libmysqld-dev python-dev python-setuptools
2,安装完依赖包后安装mysqldb
sudo apt-get install python-mysqldb

然后进行链接测试:

1 import MySQLdb as mdb
2 conn = mdb.connect(host=127.0.0.1, port=3306, user=root, passwd=123456, db=test, charset=utf8mb4)#链接数据库
3 cursor = conn.cursor()
4 cursor.execute(select * from customer)#执行sql语句
5 r = cursor.fetchall()
6 print r#打印查询的结果
conn.close()#关闭数据库链接

 

Ubuntu中python链接本地数据库

标签:utf8mb4   127.0.0.1   apt-get   本地数据库   依赖包   ldb   使用   tool   print   

原文地址:https://www.cnblogs.com/AIHEN/p/9503401.html

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