溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python使用pymsql

發(fā)布時(shí)間:2020-07-22 13:56:49 來源:網(wǎng)絡(luò) 閱讀:486 作者:蔣將將 欄目:編程語言

pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。

1、執(zhí)行SQL

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pymysql

# 創(chuàng)建連接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='jiangjj')

# 創(chuàng)建游標(biāo)

cursor = conn.cursor()

# 執(zhí)行SQL,并返回收影響行數(shù)

effect_row = cursor.execute("update hosts set host = '1.1.1.2'")

# 提交,不然無法保存新建或者修改的數(shù)據(jù)

conn.commit()

# 關(guān)閉游標(biāo)

cursor.close()

# 關(guān)閉連接

conn.close()

2、獲取新創(chuàng)建的數(shù)據(jù)

import pymysql

# 創(chuàng)建連接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='jiangjj')

# 創(chuàng)建游標(biāo)

cursor = conn.cursor()

#參數(shù)傳遞,必須使用參數(shù)的形式

# inp=input("請輸入班級:")

# r = cursor.execute('insert into calss(caption) values(%s)',inp)

# print(r)

#方式1

sid=input("請輸入ID:")

gender=input('請輸入性別:')

class_cid=input("請輸入班級id:")

sname=input("請輸入姓名:")

r = cursor.execute('insert into student(sid,gender,class_cid,sname) values(%s,%s,%s,%s)',(sid,gender,class_cid,sname))

# 提交,不然無法保存新建或者修改的數(shù)據(jù)

conn.commit()

# 關(guān)閉游標(biāo)

cursor.close()

# 關(guān)閉連接

補(bǔ)充:采用元組或列表添加數(shù)據(jù)

l = [

(18,'男',2,'測試1'),

(19,'男',2,'測試2'),

(20,'男',2,'測試3')

]

r = cursor.executemany('insert into student(sid,gender,class_cid,sname) values(%s,%s,%s,%s)',l)

print(r)

#更新數(shù)據(jù)

cursor.execute('update student set sname=%s where sid=%s',('將將將',1))

#刪除數(shù)據(jù)

cursor.execute('delete from student where sid=%s',(20))

3、獲取查詢數(shù)據(jù)

#查詢數(shù)據(jù)

r = cursor.execute('select * from student')

print(r)

# result = cursor.fetchall()        #獲取所有數(shù)據(jù)

# print(result)

# result = cursor.fetchone()        #根據(jù)指針,獲取第一行數(shù)據(jù)

# print(result)

result = cursor.fetchmany(3)      #獲取前n行數(shù)據(jù)

print(result)

注:在fetch數(shù)據(jù)時(shí)按照順序進(jìn)行,可以使用cursor.scroll(num,mode)來移動游標(biāo)位置,如:

  • cursor.scroll(1,mode='relative')  # 相對當(dāng)前位置移動

  • cursor.scroll(2,mode='absolute') # 相對絕對位置移動

4、獲取新創(chuàng)建數(shù)據(jù)自增

import pymysql

# 創(chuàng)建連接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='authority')

# 創(chuàng)建游標(biāo),將游標(biāo)設(shè)置為字典類型

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

#sql

user = input("請輸入用戶名:")

password = input('請輸入用戶密碼:')

cursor.execute('select nid,username from user where username=%s and password=%s',(user,password))

reslut = cursor.fetchall()

print(reslut)

# 提交,不然無法保存新建或者修改的數(shù)據(jù)

conn.commit()

#獲取新創(chuàng)建數(shù)據(jù)自增ID

nid = cursor.lastrowid

print(nid)

# 關(guān)閉游標(biāo)

cursor.close()

# 關(guān)閉連接

conn.close()


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI