pymysql的使用

1.安裝

pip install pymysqlmysql

2.鏈接到數據庫

先來查看一下數據庫裏有哪些數據sql

使用pymysql 下的connect方法來鏈接數據庫,先來看下須要哪些參數數據庫

進入源碼能夠看到須要的參數socket

(      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,
        autocommit=False, db=None, passwd=None, local_infile=False,
        max_allowed_packet=16*1024*1024, defer_connect=False,
        auth_plugin_map=None, read_timeout=None, write_timeout=None,
        bind_address=None, binary_prefix=False, program_name=None,
        server_public_key=None

別怕,最經常使用的參數也就是那幾個,直接看代碼fetch

import pymysql

# 建立鏈接,test1爲數據庫
conn = pymysql.connect(
    host = '127.0.0.1',
    user = 'root',
    password="123456",
    database='test1',
    port=3306,
    charset='utf8',
)
# 建立遊標
cursor = conn.cursor()

# 執行sql語句
row=cursor.execute('select * from student')
print(row)

# 關閉鏈接,遊標和鏈接都要關閉
cursor.close()
conn.close()

執行結果爲:4

3.查詢

pymysql裏查詢分爲查詢一條,查詢多條和查詢所有spa

查詢一條unix

import pymysql

# 建立鏈接
conn = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password="123456",
    database='test1',
    port=3306,
    charset='utf8',
)
# 建立遊標
cursor = conn.cursor()
# 執行sql語句 row = cursor.execute("select * from student")
# 取一條數據 print(cursor.fetchone())
# 在取一條數據 print(cursor.fetchone())

執行結果:

(1, '李六', 23)
(2, '王五', 23)code

查詢多條

import pymysql

# 建立鏈接
conn = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password="123456",
    database='test1',
    port=3306,
    charset='utf8',
)
# 建立遊標
cursor = conn.cursor()
# 執行sql語句 row = cursor.execute("select * from student")
#參數是須要查詢幾條 print(cursor.fetchmany(2))
執行結果:

((1, '李六', 23), (2, '王五', 23))

查詢全部

import pymysql

# 建立鏈接
conn = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password="123456",
    database='test1',
    port=3306,
    charset='utf8',
)
# 建立遊標
cursor = conn.cursor()
# 執行sql語句 row = cursor.execute("select * from student")
# 查詢全部 print(cursor.fetchall())

執行結果:((1, '李六', 23), (2, '王五', 23), (4, '李四', 54), (11, '張三', 33))

經過上面的查詢,發現返回的都是元組,pymysql提供了返回字典的格式,只須要在建立遊標的時候加上cursor=pymysql.cursors.DictCursor就能夠裏server

而後再來執行上面的代碼blog

import pymysql

# 建立鏈接
conn = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password="123456",
    database='test1',
    port=3306,
    charset='utf8',
)
# 建立遊標
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 執行sql語句 row = cursor.execute("select * from student")
# 查詢全部 print(cursor.fetchall())

執行結果:[{'id': 1, 'name': '李六', 'age': 23}, {'id': 2, 'name': '王五', 'age': 23}, {'id': 4, 'name': '李四', 'age': 54}, {'id': 11, 'name': '張三', 'age': 33}]

3.插入數據

import pymysql

# 建立鏈接
conn = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password="123456",
    database='test1',
    port=3306,
    charset='utf8',
)
# 建立遊標
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 插入一條數據 cursor.execute("insert into student(name,age) values('haohao',26)") data = [ ('M1', 26), ('M2', 22), ('M3', 30) ] # 一次性插入多條數據 cursor.executemany("insert into student(name,age) values(%s,%s)", data)
# 插入 刪除 更新 必定要commit() conn.commit() cursor.close() conn.close()
相關文章
相關標籤/搜索