Python學(xué)習(xí)18-連接數(shù)據(jù)庫

查看所有Python相關(guān)學(xué)習(xí)筆記

本文包含內(nèi)容

  1. 連接mysql數(shù)據(jù)庫
  2. 連接db2數(shù)據(jù)庫

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

安裝相關(guān)庫

# 此處指定mysqlclient版本為1.3.12
pip3 install mysqldb==1.3.12
# 如果上述命令無效,可以使用下面的這個命令安裝
pip install mysqlclient

python中連接mysql數(shù)據(jù)庫

import MySQLdb
# 設(shè)置參數(shù)
connection = MySQLdb.connect\
    (host = 'xx.xx.xx.xx', # 指定ip
     user = 'root',  # 用戶名
     passwd = '1qaz@WSX', # 密碼
     db = 'plesson',  #連接的庫
     charset = 'utf8' # 指定數(shù)據(jù)庫對應(yīng)的編碼
     )
c = connection.cursor()  # 建立游標(biāo)
c.execute('select * from sq_course')  # 注釋sql,查詢

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

  • sql格式
# 查詢?nèi)頂?shù)據(jù)
select * from 表名;

# 查詢符合條件的數(shù)據(jù)
select * from 表名 where id =7 and name='dd';

# 查詢符合條件的數(shù)據(jù)的id
select id from 表明 where id =7;
  • 返回最近一次操作的行數(shù)
numrows = c.rowcount # 返回最近一次操作到的行數(shù)
print(numrows)
  • 獲取數(shù)據(jù)

    單步執(zhí)行獲取數(shù)據(jù)fetchone,fetchmany,fetchall

    • 獲取單行
    for x in range(numrows):
    # 返回的是一個元組
        row= c.fetchone() # 獲取單行
        print(row)
        
    # 執(zhí)行結(jié)果
    (1, 'Python', 'Python基礎(chǔ)與實戰(zhàn)', 1)
    (2, 'Selenium', 'web自動化', 2)
    
    • 獲取多行數(shù)據(jù)(可指定行數(shù))fetchmany(n)

    類似于切片:查詢到到結(jié)果[:n] (默認(rèn)顯示一行數(shù)據(jù))

    data = c.fetchmany(2) # 獲取多行(可指定行數(shù))
    print(data)
    data = c.fetchmany(1) # 獲取多行(可指定行數(shù))
    print(data)
    
    # 執(zhí)行結(jié)果
    ((1, 'Python', 'Python基礎(chǔ)與實戰(zhàn)', 1), (2, 'Selenium', 'web自動化', 2))
    ((1, 'Python', 'Python基礎(chǔ)與實戰(zhàn)', 1),)
    
    • 獲取所有數(shù)據(jù)
    datas = c.fetchall() # 獲取所有數(shù)據(jù)
    print(datas)
    
    # 執(zhí)行結(jié)果
    ((1, 'Python', 'Python基礎(chǔ)與實戰(zhàn)', 1), (2, 'Selenium', 'web自動化', 2))
    

插入數(shù)據(jù)

  • sql格式:
insert into 表名 values(值1,值2...);
insert into 表名(列1,列2...) values(值1,值2...);
  • 執(zhí)行插入操作時記得要提交commit
c.execute("insert into sq_course values(7,'dddddd','dddd','2')")
connection.commit()  #提交
print(f'插入數(shù)據(jù)的行數(shù):{c.rowcount}') # 查詢插入了多少條數(shù)據(jù)

# 執(zhí)行結(jié)果
插入數(shù)據(jù)的行數(shù):1

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

  • sql格式:
# 刪除符合條件的數(shù)據(jù)
delete from 表名 where xxx ; 

# 刪除整張表內(nèi)的數(shù)據(jù)
delete from 表名 ; 
  • 執(zhí)行刪除操作時記得要提交commit
# 刪除id=7的數(shù)據(jù)
c.execute("delete from sq_course where id=7")
connection.commit()  #提交
print(f'刪除數(shù)據(jù)的行數(shù):{c.rowcount}') 

# 執(zhí)行結(jié)果
刪除數(shù)據(jù)的行數(shù):1

修改數(shù)據(jù)

  • sql格式:
# 更新整張表的數(shù)據(jù)
update 表名 set 列名1=值1;

# 更新符合條件的數(shù)據(jù)
update 表名 set 列名1=值1,列名2=值2 where id=7;
  • 執(zhí)行修改操作時記得要提交commit
c.execute("update sq_course set name='dddd' where id=7")
connection.commit()  #提交d
print(f'修改數(shù)據(jù)的行數(shù):{c.rowcount}')

# 執(zhí)行結(jié)果
修改數(shù)據(jù)的行數(shù):1

關(guān)閉數(shù)據(jù)庫的連接

connection.close()

問題匯總

啟動教管系統(tǒng)服務(wù)時報錯django.db.utils.OperationalError: (1193, "Unknown system variable 'storage_engine'")
> mysql server 5.7
  • 更改setting文件中的相關(guān)配置
# 原配置
"OPTIONS":{"init_command":"SET storage_engine=INNODB;"}
# 更改后的配置
"OPTIONS":{"init_command":"SET default_storage_engine=INNODB;"}

修改mysql_config時如果提示沒有權(quán)限,可以通過which mysql_config找到該文件,右鍵查看簡介,修改權(quán)限為“讀與寫”,文件內(nèi)容修改后,再改為“只讀”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容