# frist.py import pymysql # 建立數據庫對象 db = pymysql.connect(host="localhost", user="root", password="123456", database="db4", charset="utf8") # 利用db方法建立遊標對象 cur = db.cursor() # 利用遊標對象的execute()方法執行SQL命令 cur.execute("insert into sheng values\ (16,300000,'臺灣省');") # 提交到數據庫 db.commit() # 關閉遊標對象 cur.close() # 斷開數據庫連接 db.close() # 增 刪 改 import pymysql # 建立數據庫連接 # 連接到db4庫 db = pymysql.connect(host="localhost", user="root", password="123456", database="db4", charset="utf8") # 建立遊標 cur = db.cursor() try: # 添加記錄 cur.execute("insert into sheng values (17,168800,'浙江');") # 修改記錄 cur.execute("update sheng set id=666 where id=17;") # 刪除記錄 cur.execute("delete from sheng where s_name='浙江';") # 截獲EXception類型錯誤 except Exception as e: # 出現異常後回滾 db.rollback() # 輸出錯誤 print("Error ", e) else: # 提交數據 db.commit() # 關閉遊標 cur.close() # 斷開數據庫連接 db.close() # 查詢 import pymysql # 建立數據庫連接 db = pymysql.connect(host="localhost", user="root", password="123456", database="db4", charset="utf8") # 建立遊標 cur = db.cursor() try: # 查找 cur.execute("select * from sheng;") # 取出一條記錄就少一條 print("***************************") data1 = cur.fetchone() print(data1) print("***************************") data2 = cur.fetchmany(3) for i in data2: print(i) print("***************************") # 遍歷取出數據 data3 = cur.fetchall() for x in data3: print(x) # 提交數據 db.commit() except Exception as e: db.rollback() print("Error ", e) # 關閉遊標 cur.close() # 斷開數據庫連接 db.close() # 參數化 import pymysql # 建立數據庫連接 db = pymysql.connect(host="localhost", user="root", password="123456", database="db4", charset="utf8") # 建立遊標 cur = db.cursor() try: s_id = input("請輸入省的編號") s_name = input("請輸入省的名字") # 用佔位符參數化數據 sql_insert = "insert into sheng(s_id,s_name) values(%s,%s)" # execute方法 傳參必須是列表 cur.execute(sql_insert, [s_id, s_name]) # 提交數據 db.commit() except Exception as e: db.rollback() print("Error ", e) # 關閉遊標 cur.close() # 斷開數據庫連接 db.close()
# mysqlpython.py # 導入mysql模塊 from pymysql import * class MysqlPython: def __init__(self, database, # 庫 host="127.0.0.1", # ip地址 user="root", # 用戶名 password="123456", # 密碼 port=3306, # 端口 charset="utf8"): # 字符集 self.host = host self.database = database self.user = user self.password = password self.port = port self.charset = charset def open(self): # 建立數據庫連接函數 self.db = connect(host=self.host, database=self.database, user=self.user, password=self.password, port=self.port, charset=self.charset) self.cur = self.db.cursor() # 建立遊標對象 def close(self): # 建立斷開數據庫連接 關閉遊標函數 self.cur.close() self.db.close() def zhixing(self, sql, L=[]): # 建立pymysql.execute() 方法函數 try: self.open() # 連接數據庫 self.cur.execute(sql, L) # 參數化執行SQL命令 self.db.commit() # 提交數據 print("ok") except Exception as e: self.db.rollback() # 出錯取消提交 print("Failed", e) self.close() # 斷開數據庫連接 關閉遊標 def all(self, sql, L=[]): try: self.open() self.cur.execute(sql, L) result = self.cur.fetchall() return result except Exception as e: print("Failed", e) self.close()
# frist.py from mysqlpython import MysqlPython # 建立數據庫連接 sqlh = MysqlPython("db4") # 建立數據庫對象 sql_update = "update sheng set s_name='遼寧省'\ where s_name='雲南省';" # 調用xiugai函數 執行SQL命令:sql_update sqlh.zhixing(sql_update) sql_select = "select * from sheng where id=%s;" # 調用all函數 執行SQL命令:sql_select date = sqlh.all(sql_select, [1]) print(date)
from mysqlpython import Mysqlpython from hashlib import sha1 uname = input("請輸入用戶名:") pwd = input("請輸入密碼:") # 用sha1給pwd加密 s1 = sha1() # 建立sha1加密對象 s1.update(pwd.encode("utf8")) # 指定編碼 pwd2 = s1.hexdigest() # 返回16進制加密結果 sqlh = Mysqlpython("db4") select = "select password from user where \ username=%s;" result = sqlh.all(select, [uname]) # print(result) # (('7c4a8d09ca3762af61e59520943dc26494f8941b',),) if len(result) == 0: print("用戶名不存在") elif result[0][0] == pwd2: print("登陸成功") else: print("密碼錯誤")
# 建立一張表 # 鏈接數據庫的模塊 from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String engine = create_engine("mysql+pymysql://root:123456@localhost/db4", encoding="utf8") Base = declarative_base() # orm基類 class User(Base): # 繼承Base基類 __tablename__ = "t123" id = Column(Integer, primary_key=True) name = Column(String(20)) address = Column(String(40)) Base.metadata.create_all(engine)