python--pymysql


前言:關於pymysql是python與數據庫的連用,在pychrm中將mysql應用(增刪改查)。

基本操作:
  1、pychrm中引入數據庫

    首先在pychrm中的View中找到Tool Windows中的Database,點擊Database后pychrm右邊找到Database,在點擊打開Database,在左上方有加號符點擊找到Data Success找到Mysql並點擊,在出現的頁面上(這里是在有數據庫的情況下)就能輸入數據庫名和user(通常:root)及password(123456),完了以后點擊Test Connection(測試連接是否成功)有變出現success時表名連接成功。在點擊OK即可。

    或者在沒有數據庫的情況下:當點擊Mysql后直接點擊Test Coonection 彈出輸入用戶名和密碼,此時輸入root用戶名和123456密碼(自己設置的mysql密碼)點擊ok再點擊OK生成以和location右擊鼠標Table是新建表,Schema是新建數據庫新建表。
pymysql應用

Python連接MySQL數據庫

一,Python3連接Mysql

1,pyMysql介紹

  Pymysql是在python3.x版本中用於連接MySQL服務器的一個庫,Python2中則使用mysqldb。

  Django中也可以使用PyMSQL連接MuSQL數據庫。

2、PyMSQL安裝

<1>在cmd中先輸入命令python -V(大寫)或pip -V查看pychrm版本。

<2>安裝:pip install pymysql

3,連接數據庫

  <1>注意事項

    在進行文本一下內容之前需要注意:

  有一個Mysql數據庫,並且已經啟動。

  可以連接該數據庫的用戶名和密碼

  有一個有權限操作的database。

  <2>基本使用

# 導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '本地地址',user = ‘用戶名’,password = ‘密碼’,database = ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行的SQL語句的光標對象
cursor = conn.cursor()
#定義要執行的SQL語句
sql = ‘create table user1(id int auto_increment primary key,
                          name char(10) not null unique(唯一索引),
                          age tinyint(最小整形) not null,
                          password char(20) not null
                         )engine = innodb default charset= utf8;
      ’                   
#執行SQL語句
cursor.execute(sql)
#關閉光標對象
cursor.close()
#斷開數據連接
conn.close()

4,增刪改查操作

<1>增

#導入模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database= ‘數據庫名’,charset= ‘utf8’)
#得到一個可以執行的SQL語句的光標對象
cursor = conn.cursor()
sql = 'insert into user1(name,age) vaules (%s,%s)'
username = ‘Alex’
age = 18
#執行SQL語句
crusor.execute(sql,[username,age])
#提交事件
conn.commit()
cursor.close()
conn.close()

插入數據失敗回滾

#導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database = ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行SQL語句的光標對象
cursor = conn.cursor()
sql = 'insert into user1(name,age) values(%s,%s);'
username = 'Alex'
age = 18
try:
    #執行SQL語句
    cursor.execute(sql,[username,age])
    #提交事務
    conn.commit()
except Exception as e:
    #有異常,回滾事務
    conn.rollback()
cursor.close()
conn.close()

獲取插入數據的ID(關聯操作是會用到)

#導入模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database= ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行SQL語句的光標對象
cursor = conn.cursor()
sql = 'insert into(name,age) values(%s,%s);'
usernamee = 'Alex'
age = 18
try:
    #執行SQL語句
    cursor.execute(sql,[username,age])
    #提交事務
    conn.commit()
    #提交之后,獲取剛插入的數據ID
    last_id = cursor.lastrwid
except Exceprion as e:
    #有異常,回滾事務
    conn.rollback()
cursor.close()
conn.close()

批量執行

#導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user= ‘用戶名’,password =‘密碼’,database = ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行SQL語句的光標對象
cursor = conn.cursor()
sql = 'insert into user1(name,age) values(%s,%s);'
data = [('Alex'.18),('Egon',20),('Yuan',20)]
try:
    #批量執行多條插入SQL語句
    cursor.exectemany(sql,data)
    #提交事務
    conn.commit()
except Exception as e:
    #有異常,回滾事務
    conn.rollback()
cursor.close()
conn.close()

<2> 刪

#導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database = ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行SQL語句的光標對象
cursor = conn.cursor()
sql = 'delete from user1 where id = %s;'
try:
    cursor.execute(sql,[4])
    #提交事務
    conn.commit()
execpt Exception as e:
    #有異常,回滾事務
    conn.rollback()
cursor.close()
conn.close()

<3>改

#導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database= ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行的SQL語句的光標對象
cursor = conn.cursor()
#修改數據庫的語句
sql = 'updata user1 set age = %s where name = %s;'
username = 'Alex'
age = 80
try:
    #執行SQL語句
    cursor.execute(sql,[username])
    #提交事務
    conn.commit()
except Exception as e:
    #有異常,回滾事務
    conn.rollback()
cursor.close()
conn.close()

<4> 查

查詢單條數據

#導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database = ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行SQl語句的光標對象
cursor = conn.cursor()
#查詢數據的SQL語句
sql = 'select id,name,age from user! where id = 1;'
#執行SQl語句
cursor.execute(sql)
#獲取單條查詢數據
ret = cursor.fetchone()
cursor.close()
conn.close()
#打印下查詢結果
print(ret)

查詢多條數據

#導入pymysql模塊
import pymysql
#連接database
conn = pymysql.connect(host = '你的數據庫地址',user = ‘用戶名’,password = ‘密碼’,database = ‘數據庫名’,charset = ‘utf8’)
#得到一個可以執行Sql語句的光標對象
corsor = conn.corsor()
#查詢數據的SQL語句
sql = 'select id ,name,age from user1;'
#執行SQl語句
cursor.execte(sql)
#獲取多條查詢數據
ret = cursor.fetchall()
cursor.close()
conn.close()
#打印下查詢結果
print(ret)

進階用法

#可以獲取指定數量的數據
cursor.fetcmany(3)
#光標按絕對位置移動1
cursor.scroll(1,mode = 'absolute')
#光標按照相對位置(當前位置)移動1
cursor.scroll(1,mode = 'relative')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM