Python3.6實現連線mysql或mariadb的方法分析
阿新 • • 發佈:2020-01-09
本文例項講述了Python3.6實現連線mysql或mariadb的方法。分享給大家供大家參考,具體如下:
python3.6的安裝檢視前面一篇文章//www.jb51.net/article/108938.htm
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程式設計有所幫助。