1、基本使用mysql
# 導入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 )ENGINE=innodb DEFAULT CHARSET=utf8; """ # 執行SQL語句 cursor.execute(sql) # 關閉光標對象 cursor.close() # 關閉數據庫鏈接 conn.close()
注意:sql
charset="utf8",編碼不要寫成"utf-8"數據庫
2、增刪改查操做ide
一、增fetch
# 導入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 # 執行SQL語句 cursor.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()
# 導入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() # 提交以後,獲取剛插入的數據的ID last_id = cursor.lastrowid except Exception 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", 21)] try: # 批量執行多條插入SQL語句 cursor.executemany(sql, data) # 提交事務 conn.commit() except Exception 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 = "DELETE FROM USER1 WHERE id=%s;" try: cursor.execute(sql, [4]) # 提交事務 conn.commit() except Exception as e: # 有異常,回滾事務 conn.rollback() cursor.close() conn.close()
三、改spa
# 導入pymysql模塊 import pymysql # 鏈接database conn = pymysql.connect(host=「你的數據庫地址」, user=「用戶名」,password=「密碼」,database=「數據庫名」,charset=「utf8」) # 獲得一個能夠執行SQL語句的光標對象 cursor = conn.cursor() # 修改數據的SQL語句 sql = "UPDATE USER1 SET age=%s WHERE name=%s;" username = "Alex" age = 80 try: # 執行SQL語句 cursor.execute(sql, [age, username]) # 提交事務 conn.commit() except Exception as e: # 有異常,回滾事務 conn.rollback() cursor.close() conn.close()
四、查code
# 導入pymysql模塊 import pymysql # 鏈接database conn = pymysql.connect(host=「你的數據庫地址」, user=「用戶名」,password=「密碼」,database=「數據庫名」,charset=「utf8」) # 獲得一個能夠執行SQL語句的光標對象 cursor = conn.cursor() # 查詢數據的SQL語句 sql = "SELECT id,name,age from USER1 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語句的光標對象 cursor = conn.cursor() # 查詢數據的SQL語句 sql = "SELECT id,name,age from USER1;" # 執行SQL語句 cursor.execute(sql) # 獲取多條查詢數據 ret = cursor.fetchall() cursor.close() conn.close() # 打印下查詢結果 print(ret)
3、進階用法對象
一、cursor.fetchmany(3) # 能夠獲取指定數量的數據blog
二、cursor.scroll(1,mode="absolute") # 光標按絕對位置移動1
三、cursor.scroll(1,mode="relative") # 光標按照相對位置(當前位置)移動1