Python3.6實(shí)現(xiàn)連接mysql或mariadb的方法分析
本文實(shí)例講述了Python3.6實(shí)現(xiàn)連接mysql或mariadb的方法。分享給大家供大家參考,具體如下:
python3.6的安裝查看前面一篇文章http://www.dbjr.com.cn/article/108938.htm
mysql或mariadb數(shù)據(jù)庫的安裝查看以前的相關(guān)文章,這里不再贅述
首先在mariadb數(shù)據(jù)庫中創(chuàng)建相應(yīng)的庫和表:
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 #連接數(shù)據(jù)庫,host、賬號(hào)、密碼、庫 db = pymysql.connect('localhost','root','redhat','oracle') #創(chuàng)建游標(biāo)使用的cursor方法 cursor = db.cursor() #使用execute方法執(zhí)行sql語句 cursor.execute('select version()') #使用fetchone方法獲取單條數(shù)據(jù) data = cursor.fetchone() print('Database version:%s' % data) #關(guān)閉游標(biāo),并關(guān)閉數(shù)據(jù)庫 cursor.close() db.close()
[root@wadeson scripts]# python connectmysql.py Database version:5.5.55-MariaDB
note:
Python查詢Mysql使用 fetchone()
方法獲取單條數(shù)據(jù), 使用fetchall()
方法獲取多條數(shù)據(jù)。
fetchone()
: 該方法獲取下一個(gè)查詢結(jié)果集。結(jié)果集是一個(gè)對象
fetchall()
: 接收全部的返回結(jié)果行.
rowcount
: 這是一個(gè)只讀屬性,并返回執(zhí)行execute()方法后影響的行數(shù)。
note:如果使用以上方法安裝報(bào)錯(cuò):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
然后驗(yàn)證是否安裝成功:
[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 >>>
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺談pandas中Dataframe的查詢方法([], loc, iloc, at, iat, ix)
下面小編就為大家分享一篇淺談pandas中Dataframe的查詢方法([], loc, iloc, at, iat, ix),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Windows下用py2exe將Python程序打包成exe程序的教程
這篇文章主要介紹了Windows下用py2exe將Python程序打包成exe程序的教程,文中主要針對Python3.x版本進(jìn)行說明,需要的朋友可以參考下2015-04-04Python使用Vagrant搭建開發(fā)環(huán)境的具體步驟
使用 Vagrant 搭建開發(fā)環(huán)境是一個(gè)非常方便的方式,它可以幫助你快速創(chuàng)建、配置和管理虛擬機(jī),確保開發(fā)環(huán)境的一致性,以下是使用 Vagrant 搭建開發(fā)環(huán)境的具體步驟,需要的朋友可以參考下2024-09-09python對驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了python對驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python中dump與dumps實(shí)現(xiàn)序列化
這篇文章就來介紹python中dump與dumps實(shí)現(xiàn)序列化,文章將圍繞dump與dumps實(shí)現(xiàn)序列化展開內(nèi)容且簡精,需要的朋友可以參考一下,希望對你有所幫助2021-10-10