您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用Python3.6怎么連接mysql或mariadb,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
首先在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è)對(duì)象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 >>>
Python是一種編程語言,內(nèi)置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強(qiáng)大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門的大數(shù)據(jù)分析,人工智能,Web開發(fā)等。
上述就是小編為大家分享的使用Python3.6怎么連接mysql或mariadb了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。