Python3.6实现连接mysql或mariadb的方法分析

时间:2022-01-09 23:31:54

本文实例讲述了Python3.6实现连接mysqlmariadb的方法。分享给大家供大家参考,具体如下:

python3.6的安装查看前面一篇文章http://www.zzvips.com/article/123285.html

mysql或mariadb数据库的安装查看以前的相关文章,这里不再赘述

首先在mariadb数据库中创建相应的库和表:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
MariaDB [(none)]> create database oracle default character set utf8 default collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MariaDB [oracle]> create table oracle_indexmonitor( index_name varchar(200) not null, ipaddress varchar(39) not null, tnsname varchar(100) not null, insert_time timestamp default current_timestamp, primary key(index_name) ) engine=InnoDB default charset=utf8;
Query OK, 0 rows affected (0.01 sec)
MariaDB [oracle]> desc oracle_indexmonitor;
+-------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+-------------------+-------+
| index_name | varchar(200) | NO | PRI | NULL | |
| ipaddress | varchar(39) | NO | | NULL | |
| tnsname | varchar(100) | NO | | NULL | |
| insert_time | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------------+--------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)

安装需要用到的模块pymysql:

?
1
2
3
4
5
6
[root@wadeson Python-3.6.1]# /usr/local/python36/bin/pip3 install PyMysql
Collecting PyMysql
Downloading PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
100% |¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€¨€| 81kB 87kB/s
Installing collected packages: PyMysql
Successfully installed PyMysql-0.7.11

检测模块是否安装成功:

?
1
2
3
4
5
6
[root@wadeson Python-3.6.1]# python
Python 3.6.1 (default, Jul 13 2017, 15:41:38)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>> exit()

然后编写py脚本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@wadeson Python-3.6.1]# cd /root/tools/scripts/
[root@wadeson scripts]# vim connectmysql.py
#!/usr/bin/python
#coding=utf8
import pymysql
#连接数据库,host、账号、密码、库
db = pymysql.connect('localhost','root','redhat','oracle')
#创建游标使用的cursor方法
cursor = db.cursor()
#使用execute方法执行sql语句
cursor.execute('select version()')
#使用fetchone方法获取单条数据
data = cursor.fetchone()
print('Database version:%s' % data)
#关闭游标,并关闭数据库
cursor.close()
db.close()
?
1
2
[root@wadeson scripts]# python connectmysql.py
Database version:5.5.55-MariaDB

note:

Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。

fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
fetchall(): 接收全部的返回结果行.
rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。

note:如果使用以上方法安装报错:ssl模块不可用

那么可以使用编译安装:

?
1
2
3
4
5
wget https://pypi.python.org/packages/f5/d9/976c885396294bb1c4ca3d013fd2046496cde2efbb168e4f41dd12552dd9/PyMySQL-0.7.6.tar.gz#md5=d1353d9ad6e6668c3c463603b12cadb0
tar xf PyMySQL-0.7.6.tar.gz
cd PyMySQL-0.7.6
python setup.py build
python setup.py install

然后验证是否安装成功:

?
1
2
3
4
5
6
[root@oracle PyMySQL-0.7.6]# python
Python 3.6.1 (default, Jul 13 2017, 14:31:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>>

希望本文所述对大家Python程序设计有所帮助。

原文链接:http://www.cnblogs.com/jsonhc/p/7161032.html