MySQL 五 (MySQL與Python互動)

2021-09-09 06:16:17 字數 2519 閱讀 1835

from pymysql import *
conn=connect(引數列表)
物件的方法
cs1=conn.cursor()
物件的方法

物件的屬性

from pymysql import *

def main():

# 建立connection連線

conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')

# 獲得cursor物件

cs1 = conn.cursor()

# 執行insert語句,並返回受影響的行數:新增一條資料

# 增加

count = cs1.execute('insert into goods_cates(name) values("硬碟")')

#列印受影響的行數

print(count)

count = cs1.execute('insert into goods_cates(name) values("光碟")')

print(count)

# # 更新

# count = cs1.execute('update goods_cates set name="機械硬碟" where name="硬碟"')

# # 刪除

# count = cs1.execute('delete from goods_cates where id=6')

# 提交之前的操作,如果之前已經之執行過多次的execute,那麼就都進行提交

conn.commit()

# 關閉cursor物件

cs1.close()

# 關閉connection物件

conn.close()

if __name__ == '__main__':

main()

from pymysql import *

def main():

# 建立connection連線

conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')

# 獲得cursor物件

cs1 = conn.cursor()

# 執行select語句,並返回受影響的行數:查詢一條資料

count = cs1.execute('select id,name from goods where id>=4')

# 列印受影響的行數

print("查詢到%d條資料:" % count)

for i in range(count):

# 獲取查詢的結果

result = cs1.fetchone()

# 列印查詢的結果

print(result)

# 獲取查詢的結果

# 關閉cursor物件

cs1.close()

conn.close()

if __name__ == '__main__':

main()

from pymysql import *

def main():

# 建立connection連線

conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')

# 獲得cursor物件

cs1 = conn.cursor()

# 執行select語句,並返回受影響的行數:查詢一條資料

count = cs1.execute('select id,name from goods where id>=4')

# 列印受影響的行數

print("查詢到%d條資料:" % count)

# for i in range(count):

# # 獲取查詢的結果

# result = cs1.fetchone()

# # 列印查詢的結果

# print(result)

# # 獲取查詢的結果

result = cs1.fetchall()

print(result)

# 關閉cursor物件

cs1.close()

conn.close()

if __name__ == '__main__':

main()

Python與MySQL程式設計基礎(五)

r星校長 第5關 python資料庫程式設計之查詢資料 在現在的軟體web開發中,越來越離不開資料庫的支援,mysql是現在最流行的關係型資料庫管理系統 rdbms relational database management system 在web開發中,mysql是最好的rdbms應用軟體之一。...

MySQL技術內幕 五 索引與演算法

一 索引概述 innodb支援兩種常見索引,一種是b 樹索引一種是雜湊索引。雜湊索引是自適應的,引1擎會根據表的使用情況自動為表生成雜湊索引,不能人為干預是否在一張表裡生成雜湊索引 b 樹索引就是傳統意義上的索引,這是關係型資料庫中最常用 有效的索引 b 樹索引的構造類似於二叉樹,根據鍵值快速找到資...

MySQL入門 五 CRUD 與資料維護

describe 是mysql資料庫提供的指令,它只能在mysql資料庫中使用,這個指令可以取得某個 的結構資訊,它的語法是這樣的 你在mysql的工具中執行 desc cmdev.dept 指令以後,mysql會傳回 cmdev.dept 的結構資訊 每乙個 在設計的時候,都會決定它有哪一些字段,...