python3.6版本的安装查看上一篇文章

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, 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
>>>

python3.6连接mysql或者mariadb的更多相关文章

  1. python3.4连接mysql数据库的方法

    python3.4连接mysql数据库的方法 发布时间:2014-08-04编辑:www.jbxue.com 本文介绍了python3.4连接mysql数据库的方法,在python3.4中不能用mys ...

  2. python3.6 连接mysql数据库问题

    最近有个项目欲安装MySQL-python/1.2.5一直失败. 环境: win7 python3.6.2 报错信息如下: Creating library build\temp.win32-3.6\ ...

  3. Python3.6连接mysql(一)

    初次学习python,因为python连接mysql的时候,需要安装mysql驱动模块 之前按照廖雪峰网站上的方法安装mysql驱动的方法: MySQL官方提供了mysql-connector-pyt ...

  4. python3.6 连接mysql数据库

    ==================pymysql=================== 由于 MySQLdb 模块还不支持 Python3.x,所以 Python3.x 如果想连接MySQL需要安装 ...

  5. python3 连接 mysql和mariadb的模块

    是pymysql python2中是MySQL-python sudo pip install pymysql 参考博客https://www.jb51.net/article/140387.htm

  6. Python3.5连接Mysql

    由于mysqldb目前仅支持到python3.4,所以这里选择pymysql. pymysql下载地址: https://pypi.python.org/packages/source/P/PyMyS ...

  7. python3 django连接mysql,同步表结构

    第一步:安装PyMySQ代替MySQLdb pip3 install PyMySQL 然后在工程目录的__init__.py中填写下面两句话   import pymysql pymysql.inst ...

  8. python3.4连接mysql

    参考:http://www.blogjava.net/huyi2006/articles/247966.html 开发环境:win7_x64 + python3.4.3 + mysql5.6.23 准 ...

  9. python2和python3 分别连接MySQL的代码

    python2中的写法如下: #coding=utf-8 import MySQLdb try: conn = MySQLdb.connect(host='localhost', port=3306, ...

随机推荐

  1. JS中的call、apply、bind 用法解疑

    JS中的caller  arguments.callee  call  apply  bind方法 一.call()和apply()方法 1.方法定义 call方法: 语法:call([thisObj ...

  2. KVM虚拟机添加硬盘

    1,创建硬盘 qemu-img create -f raw /opt/GlusterFS1_data.img 30G 硬盘名称为GlusterFS1_data.img 大小为30G 2,编辑虚拟机配置 ...

  3. Zipline Risk and Performance Metrics

    Risk and Performance Metrics 风险和性能指标 The risk and performance metrics are summarizing values calcula ...

  4. Python3: Command not found(Mac OS)

    1. 第一步:查看以下路径是否安装有 Python 3.x # 打开以下目录, 版本号有可能不同 cd /usr/local/Cellar/python/3.5.2_3/bin # 查看当前目录的内容 ...

  5. shell 从文件中读取批量文件名并做命令行操作

    222文件内容: /home/zhangsuosheng/Desktop/9-30/9_30/1bak/1538291162.png /home/zhangsuosheng/Desktop/9-30/ ...

  6. 【opencv】projectPoints 三维点到二维点 重投影误差计算

    今天计算rt计算误差——重投影误差 用solvepnp或sovlepnpRansac,输入3d点.2d点.相机内参.相机畸变,输出r.t之后 用projectPoints,输入3d点.相机内参.相机畸 ...

  7. Visual Studio 2017 新特性

    1. out-variables(Out变量) 以前,我们使用out变量的时候,需要在外部先申明,然后才能传入方法,类似如下: string ddd = ""; //先申明变量 c ...

  8. python脚本前两行

    1. 第一行指定解释器路径 推荐写法: #!/usr/bin/env python 详细说明: #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python ...

  9. 009-Shell 函数

    一.函数定义 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; ...

  10. Spring第三弹—–编码剖析Spring管理Bean的原理

    先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...