最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python之pymysql的使用小結(jié)

 更新時間:2019年07月01日 11:49:24   作者:liubinsh  
這篇文章主要介紹了Python之pymysql的使用小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

在python3.x中,可以使用pymysql來MySQL數(shù)據(jù)庫的連接,并實現(xiàn)數(shù)據(jù)庫的各種操作,本次博客主要介紹了pymysql的安裝和使用方法。

 PyMySQL的安裝

一、.windows上的安裝方法:

在python3.6中,自帶pip3,所以在python3中可以直接使用pip3去安裝所需的模塊:

pip3 install pymysql -i https://pypi.douban.com/simple

二、.linux下安裝方法:

1.tar包下載及解壓

下載tar包
wget https://pypi.python.org/packages/29/f8/919a28976bf0557b7819fd6935bfd839118aff913407ca58346e14fa6c86/PyMySQL-0.7.11.tar.gz#md5=167f28514f4c20cbc6b1ddf831ade772
解壓并展開tar包
tar xf PyMySQL-0.7.11.tar.gz

2.安裝

[root@localhost PyMySQL-0.7.11]# python36 setup.py install

數(shù)據(jù)庫的連接

本次測試創(chuàng)建的數(shù)據(jù)及表:

#創(chuàng)建數(shù)據(jù)庫及表,然后插入數(shù)據(jù)
mysql> create database dbforpymysql;
mysql> create table userinfo(id int not null auto_increment primary key,username varchar(10),passwd varchar(10))engine=innodb default charset=utf8;
mysql> insert into userinfo(username,passwd) values('frank','123'),('rose','321'),('jeff',666);

#查看表內(nèi)容
mysql> select * from userinfo;
+----+----------+--------+
| id | username | passwd |
+----+----------+--------+
| 1 | frank  | 123  |
| 2 | rose   | 321  |
| 3 | jeff   | 666  |
+----+----------+--------+
3 rows in set (0.00 sec)

連接數(shù)據(jù)庫:

import pymysql

#連接數(shù)據(jù)庫
db = pymysql.connect("localhost","root","LBLB1212@@","dbforpymysql")

#使用cursor()方法創(chuàng)建一個游標對象
cursor = db.cursor()

#使用execute()方法執(zhí)行SQL語句
cursor.execute("SELECT * FROM userinfo")

#使用fetall()獲取全部數(shù)據(jù)
data = cursor.fetchall()

#打印獲取到的數(shù)據(jù)
print(data)

#關閉游標和數(shù)據(jù)庫的連接
cursor.close()
db.close()

#運行結(jié)果
((1, 'frank', '123'), (2, 'rose', '321'), (3, 'jeff', '666'))

要完成一個MySQL數(shù)據(jù)的連接,在connect中可以接受以下參數(shù):

def __init__(self, host=None, user=None, password="",
       database=None, port=0, unix_socket=None,
       charset='', sql_mode=None,
       read_default_file=None, conv=None, use_unicode=None,
       client_flag=0, cursorclass=Cursor, init_command=None,
       connect_timeout=10, ssl=None, read_default_group=None,
       compress=None, named_pipe=None, no_delay=None,
       autocommit=False, db=None, passwd=None, local_infile=False,
       max_allowed_packet=16*1024*1024, defer_connect=False,
       auth_plugin_map={}, read_timeout=None, write_timeout=None,
       bind_address=None):
參數(shù)解釋:
host: Host where the database server is located  #主機名或者主機地址
user: Username to log in as  #用戶名
password: Password to use.  #密碼
database: Database to use, None to not use a particular one.  #指定的數(shù)據(jù)庫
port: MySQL port to use, default is usually OK. (default: 3306)  #端口,默認是3306
bind_address: When the client has multiple network interfaces, specify
  the interface from which to connect to the host. Argument can be
  a hostname or an IP address.  #當客戶端有多個網(wǎng)絡接口的時候,指點連接到數(shù)據(jù)庫的接口,可以是一個主機名或者ip地址
unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
charset: Charset you want to use.  #指定字符編碼
sql_mode: Default SQL_MODE to use. 
read_default_file:
  Specifies my.cnf file to read these parameters from under the [client] section.
conv:
  Conversion dictionary to use instead of the default one.
  This is used to provide custom marshalling and unmarshaling of types.
  See converters.
use_unicode:
  Whether or not to default to unicode strings.
  This option defaults to true for Py3k.
client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT.
cursorclass: Custom cursor class to use.
init_command: Initial SQL statement to run when connection is established.
connect_timeout: Timeout before throwing an exception when connecting.
  (default: 10, min: 1, max: 31536000)
ssl:
  A dict of arguments similar to mysql_ssl_set()'s parameters.
  For now the capath and cipher arguments are not supported.
read_default_group: Group to read from in the configuration file.
compress; Not supported
named_pipe: Not supported
autocommit: Autocommit mode. None means use server default. (default: False)
local_infile: Boolean to enable the use of LOAD DATA LOCAL command. (default: False)
max_allowed_packet: Max size of packet sent to server in bytes. (default: 16MB)
  Only used to limit size of "LOAD LOCAL INFILE" data packet smaller than default (16KB).
defer_connect: Don't explicitly connect on contruction - wait for connect call.
  (default: False)
auth_plugin_map: A dict of plugin names to a class that processes that plugin.
  The class will take the Connection object as the argument to the constructor.
  The class needs an authenticate method taking an authentication packet as
  an argument. For the dialog plugin, a prompt(echo, prompt) method can be used
  (if no authenticate method) for returning a string from the user. (experimental)
db: Alias for database. (for compatibility to MySQLdb)
passwd: Alias for password. (for compatibility to MySQLdb)

cursor其實是調(diào)用了cursors模塊下的Cursor的類,這個模塊主要的作用就是用來和數(shù)據(jù)庫交互的,當你實例化了一個對象的時候,你就可以調(diào)用對象下面的各種綁定方法:

class Cursor(object):
  """
  This is the object you use to interact with the database.
  """
  def close(self):
    """
    Closing a cursor just exhausts all remaining data.
    """
  def setinputsizes(self, *args):
    """Does nothing, required by DB API."""

  def setoutputsizes(self, *args):
    """Does nothing, required by DB API."""    
  def execute(self, query, args=None):
    """Execute a query

    :param str query: Query to execute.

    :param args: parameters used with query. (optional)
    :type args: tuple, list or dict

    :return: Number of affected rows
    :rtype: int

    If args is a list or tuple, %s can be used as a placeholder in the query.
    If args is a dict, %(name)s can be used as a placeholder in the query.
    """
  def executemany(self, query, args):
    # type: (str, list) -> int
    """Run several data against one query

    :param query: query to execute on server
    :param args: Sequence of sequences or mappings. It is used as parameter.
    :return: Number of rows affected, if any.

    This method improves performance on multiple-row INSERT and
    REPLACE. Otherwise it is equivalent to looping over args with
    execute().
    """
  def fetchone(self):
    """Fetch the next row"""
  def fetchmany(self, size=None):
    """Fetch several rows"""
  def fetchall(self):
    """Fetch all the rows"""
  ......

數(shù)據(jù)庫操作

一、數(shù)據(jù)庫增刪改操作

commit()方法:在數(shù)據(jù)庫里增、刪、改的時候,必須要進行提交,否則插入的數(shù)據(jù)不生效。

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES('jack','123')"
cursor.execute(sql)
db.commit() #提交數(shù)據(jù)
cursor.close()
db.close()
或者在execute提供插入的數(shù)據(jù)
import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES(%s,%s)"
cursor.execute(sql,("bob","123")) 
db.commit() #提交數(shù)據(jù)
cursor.close()
db.close()

小知識點,mysql的注入問題:

在mysql中使用"--"代表注釋,比如現(xiàn)在來實現(xiàn)一個用戶登錄的小程序:
用戶名和密碼都存在表userinfo中,表內(nèi)容如下:
mysql> select * from userinfo;
+----+----------+--------+
| id | username | passwd |
+----+----------+--------+
| 1 | frank  | 123  |
| 2 | rose   | 321  |
| 3 | jeff   | 666  |
+----+----------+--------+
3 rows in set (0.00 sec)
小程序代碼如下:
import pymysql
user = input("username:")
pwd = input("password:")
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
sql = "select * from userinfo where username='%s' and passwd='%s'" %(user,pwd)
result=cursor.execute(sql)
cursor.close()
db.close()
if result:
  print('登錄成功')
else:
  print('登錄失敗')
#正確登錄的運行結(jié)果
username:frank
password:123
result: 1
登錄成功
#錯誤登錄的運行結(jié)果
username:frank
password:1231231
result: 0
登錄失敗
看起來沒有什么問題,但是試試下面的方式吧
----------------------------------------------
username:' or 1=1 -- 
password:123
result: 3
登錄成功
----------------------------------------------
咦~也登錄成功了.
為什么呢?可以看一下現(xiàn)在的執(zhí)行的sql語句:
select * from userinfo where username='' or 1=1 -- ' and passwd='123'
這里--后面的會被注釋,所以where一定會成功,這里等于查看了所有行的內(nèi)容,返回值也不等于0,所以就登錄成功了。
解決方法就是將變量或者實參直接寫到execute中即可:
result=cursor.execute(sql,(user,pwd))
在鍵入類似' or 1=1 -- 的時候就不會登錄成功了。 

executemany():用來同時插入多條數(shù)據(jù):

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES(%s,%s)"
cursor.executemany(sql,[("tom","123"),("alex",'321')])
db.commit() #提交數(shù)據(jù)
cursor.close()
db.close()

execute()和executemany()都會返回受影響的行數(shù):

sql = "delete from userinfo where username=%s"
res = cursor.executemany(sql,("jack",))
print("res=",res)
#運行結(jié)果
res= 1

當表中有自增的主鍵的時候,可以使用lastrowid來獲取最后一次自增的ID:

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES(%s,%s)"
cursor.execute(sql,("zed","123"))
print("the last rowid is ",cursor.lastrowid)
db.commit() #提交數(shù)據(jù)
cursor.close()
db.close()

#運行結(jié)果
the last rowid is 10

二、數(shù)據(jù)庫的查詢操作

這里主要介紹三個綁定方法:

  • fetchone():獲取下一行數(shù)據(jù),第一次為首行;
  • fetchall():獲取所有行數(shù)據(jù)源
  • fetchmany(4):獲取下4行數(shù)據(jù)

先來查看表的內(nèi)容:

mysql> select * from userinfo;
+----+----------+--------+
| id | username | passwd |
+----+----------+--------+
| 1 | frank  | 123  |
| 2 | rose   | 321  |
| 3 | jeff   | 666  |
| 5 | bob   | 123  |
| 8 | jack   | 123  |
| 10 | zed   | 123  |
+----+----------+--------+
6 rows in set (0.00 sec)

使用fetchone():

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor()
sql = "SELECT * FROM userinfo"
cursor.execute(sql)
res = cursor.fetchone() #第一次執(zhí)行
print(res)
res = cursor.fetchone() #第二次執(zhí)行
print(res)
cursor.close()
db.close()

#運行結(jié)果
(1, 'frank', '123')
(2, 'rose', '321')

使用fetchall():

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor()
sql = "SELECT * FROM userinfo"
cursor.execute(sql)
res = cursor.fetchall() #第一次執(zhí)行
print(res)
res = cursor.fetchall() #第二次執(zhí)行
print(res)
cursor.close()
db.close()
#運行結(jié)果
((1, 'frank', '123'), (2, 'rose', '321'), (3, 'jeff', '666'), (5, 'bob', '123'), (8, 'jack', '123'), (10, 'zed', '123'))
()

可以看到,第二次獲取的時候,什么數(shù)據(jù)都沒有獲取到,這個類似于文件的讀取操作。

默認情況下,我們獲取到的返回值是元組,只能看到每行的數(shù)據(jù),卻不知道每一列代表的是什么,這個時候可以使用以下方式來返回字典,每一行的數(shù)據(jù)都會生成一個字典:

cursor = db.cursor(cursor=pymysql.cursors.DictCursor) #在實例化的時候,將屬性cursor設置為pymysql.cursors.DictCursor

使用fetchall獲取所有行的數(shù)據(jù),每一行都被生成一個字典放在列表里面:

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
sql = "SELECT * FROM userinfo"
cursor.execute(sql)
res = cursor.fetchall()
print(res)
cursor.close()
db.close()
#運行結(jié)果
[{'id': 1, 'username': 'frank', 'passwd': '123'}, {'id': 2, 'username': 'rose', 'passwd': '321'}, {'id': 3, 'username': 'jeff', 'passwd': '666'}, {'id': 5, 'username': 'bob', 'passwd': '123'}, {'id': 8, 'username': 'jack', 'passwd': '123'}, {'id': 10, 'username': 'zed', 'passwd': '123'}]

這樣獲取到的內(nèi)容就能夠容易被理解和使用了!

在獲取行數(shù)據(jù)的時候,可以理解開始的時候,有一個行指針指著第一行的上方,獲取一行,它就向下移動一行,所以當行指針到最后一行的時候,就不能再獲取到行的內(nèi)容,所以我們可以使用如下方法來移動行指針:

cursor.scroll(1,mode='relative') # 相對當前位置移動
cursor.scroll(2,mode='absolute') # 相對絕對位置移動

第一個值為移動的行數(shù),整數(shù)為向下移動,負數(shù)為向上移動,mode指定了是相對當前位置移動,還是相對于首行移動

例如:

sql = "SELECT * FROM userinfo"
cursor.execute(sql)
res = cursor.fetchall()
print(res)
cursor.scroll(0,mode='absolute') #相對首行移動了0,就是把行指針移動到了首行
res = cursor.fetchall() #第二次獲取到的內(nèi)容
print(res)

#運行結(jié)果
[{'id': 1, 'username': 'frank', 'passwd': '123'}, {'id': 2, 'username': 'rose', 'passwd': '321'}, {'id': 3, 'username': 'jeff', 'passwd': '666'}, {'id': 5, 'username': 'bob', 'passwd': '123'}, {'id': 8, 'username': 'jack', 'passwd': '123'}, {'id': 10, 'username': 'zed', 'passwd': '123'}]
[{'id': 1, 'username': 'frank', 'passwd': '123'}, {'id': 2, 'username': 'rose', 'passwd': '321'}, {'id': 3, 'username': 'jeff', 'passwd': '666'}, {'id': 5, 'username': 'bob', 'passwd': '123'}, {'id': 8, 'username': 'jack', 'passwd': '123'}, {'id': 10, 'username': 'zed', 'passwd': '123'}]

上下文管理器

在python的文件操作中支持上下文管理器,在操作數(shù)據(jù)庫的時候也可以使用:

import pymysql
config={
  "host":"127.0.0.1",
  "user":"root",
  "password":"LBLB1212@@",
  "database":"dbforpymysql"
}
db = pymysql.connect(**config)
with db.cursor(cursor=pymysql.cursors.DictCursor) as cursor: #獲取數(shù)據(jù)庫連接的對象
  sql = "SELECT * FROM userinfo"  
  cursor.execute(sql)
  res = cursor.fetchone()
  print(res)
  cursor.scroll(2,mode='relative')
  res = cursor.fetchone()
  print(res)
  cursor.close()
db.close()

#運行結(jié)果
{'id': 1, 'username': 'frank', 'passwd': '123'}
{'id': 5, 'username': 'bob', 'passwd': '123'}

上下文管理器可以使代碼的可讀性更強。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 使用Python實現(xiàn)首頁通知功能

    使用Python實現(xiàn)首頁通知功能

    這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)首頁通知功能,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學習一下
    2024-02-02
  • python下10個簡單實例代碼

    python下10個簡單實例代碼

    最近學python比較順手,找到感覺了,所以,我想把我用來練習的實例題目分享出來,有興趣的朋友可以關注一下。 文章分為10篇,每篇10題,共100道實例。后續(xù)如果需要可以增加
    2017-11-11
  • Python argparse模塊應用實例解析

    Python argparse模塊應用實例解析

    這篇文章主要介紹了Python argparse模塊應用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • Python post請求實現(xiàn)代碼實例

    Python post請求實現(xiàn)代碼實例

    這篇文章主要介紹了Python post請求實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • 十分鐘搞定pandas(入門教程)

    十分鐘搞定pandas(入門教程)

    這篇文章主要介紹了十分鐘搞定pandas(入門教程),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • Python圖像處理庫處理步驟

    Python圖像處理庫處理步驟

    這篇文章主要介紹了Python圖像處理探索之Python圖像處理庫,我們將學習使用不同的 Python 庫實現(xiàn)一些常見的圖像處理、變換和可視化技術,這些技術通??梢杂米鞲鼜碗s的圖像處理任務的基本預處理/后處理步驟,需要的朋友可以參考下
    2023-04-04
  • python 獲取文件列表(或是目錄例表)

    python 獲取文件列表(或是目錄例表)

    在python的應用過程中,經(jīng)常會用到獲取文件列表的方法,常規(guī)的做法是這樣的
    2009-03-03
  • Python3幾個常見問題的處理方法

    Python3幾個常見問題的處理方法

    今天小編就為大家分享一篇關于Python3幾個常見問題的處理方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Python安裝Bs4及使用方法

    Python安裝Bs4及使用方法

    這篇文章主要介紹了Python安裝Bs4及使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Python適配器模式代碼實現(xiàn)解析

    Python適配器模式代碼實現(xiàn)解析

    這篇文章主要介紹了Python適配器模式代碼實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08

最新評論

阳新县| 井陉县| 长乐市| 和林格尔县| 滦南县| 正安县| 巴南区| 宿松县| 阜康市| 灌云县| 军事| 上林县| 改则县| 彝良县| 丰顺县| 长治县| 嘉鱼县| 玉门市| 平潭县| 新昌县| 儋州市| 禄丰县| 克拉玛依市| 柏乡县| 白山市| 宿迁市| 卢湾区| 尚志市| 郧西县| 原平市| 墨竹工卡县| 延津县| 隆回县| 阿巴嘎旗| 承德县| 行唐县| 新野县| 天津市| 民权县| 永宁县| 犍为县|