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

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

python3.6的安装查看前面一篇文章https://www.oudahe.com/p/41016/

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

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

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

[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

检测模块是否安装成功:

[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脚本:

[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()

[root@wadeson scripts]# python connectMysqL.py 
Database version:5.5.55-MariaDB

note:

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

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

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

那么可以使用编译安装:

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

然后验证是否安装成功:

[root@oracle PyMysqL-0.7.6]# python 
Python 3.6.1 (default,14:31:18) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help","credits" or "license" for more information.
>>> import pyMysqL
>>>

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python常见数据库操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

使用爬虫利器 Playwright,轻松爬取抖查查数据 我们先分析登...
轻松爬取灰豚数据的抖音商品数据 调用两次登录接口实现模拟登...
成功绕过阿里无痕验证码,一键爬取飞瓜数据 飞瓜数据的登录接...
一文教你从零开始入门蝉妈妈数据爬取,成功逆向破解数据加密...
抽丝剥茧成功破解红人点集的签名加密算法 抽丝剥茧破解登录签...
轻松绕过 Graphql 接口爬取有米有数的商品数据 有米有数数据...