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. eslint常规语法检

    "no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...

  2. CLR via 笔记4.2 类型转换 is 与 as 区别

    is 和 as 操作符是用来进行强制类型转换的 is : 检查一个对象是否兼容于其他指定的类型,并返回一个Bool值,永远不会抛出异常 object o = new object(); if (o i ...

  3. use Properties objects to maintain its configuration Writing Reading System Properties 维护配置 系统变量

    System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment) https:/ ...

  4. Storm-源码分析-Topology Submit-Worker

    1 mk-worker 和其他的daemon一样, 都是通过defserverfn macro来创建worker (defserverfn mk-worker [conf shared-mq-cont ...

  5. JUnit4.12 源码分析之TestClass

    1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...

  6. Storm简介及使用

    一.Storm概述 网址:http://storm.apache.org/ Apache Storm是一个免费的开源分布式实时计算系统.Storm可以轻松可靠地处理无限数据流,实现Hadoop对批处理 ...

  7. 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活--hdu2191(多重背包模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 标准的多重背包 题目 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是 ...

  8. 005-maven坐标和依赖

    1.何为Maven坐标 groupId.artifactId.version.packaging.classifier 中央仓库:http://repol.maven.org/maven22.坐标详解 ...

  9. python logging模块介绍

    1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上 INFO:确认一切按预期运行 ...

  10. cpu-》内存-》磁盘

    cpu相当于计算机大脑负责计算以及发送执行命令:内存相当于人的记忆是临时存储:磁盘相当于笔记本,负责永久存储数据: 当系统需要调用硬盘当中的数据时,会将硬盘数据读入内存供cpu进行处理.cpu只会读取 ...