python访问mysql_python连接mysql
首先需要導(dǎo)入驅(qū)動(dòng)模塊
打開cmd導(dǎo)入模塊 pip install mysqldb(pymysql)
python2.x版本--------->mysqldb
python3.x版本---------->pymysql
python調(diào)用MySQL數(shù)據(jù)庫總共五步
1、導(dǎo)入模塊(驅(qū)動(dòng)) pymysql? mysqldb
2、獲取連接 connect
3、獲取游標(biāo)cursor
4、執(zhí)行sql execute 并且返回結(jié)果
5、關(guān)閉資源
====================================================================
#python 調(diào)用mysql幫助類 ?連接數(shù)據(jù)庫 創(chuàng)建數(shù)據(jù)查詢更新的方法
#創(chuàng)建一個(gè)數(shù)據(jù)庫幫助類
#導(dǎo)入pymysql模塊
import pymysql
#創(chuàng)建類
class sqlHelper:
#初始化屬性
def __init__(self,host,db,user,pwd):
self.host = host
self.port = 3306
self.db = db
self.user = user
self.pwd = pwd
self.charset = 'utf8'
#創(chuàng)建數(shù)據(jù)庫連接 創(chuàng)建游標(biāo)
def connection(self):
self.conn = pymysql.connect(host = self.host,port = self.port,db = self.db,user = self.user,
password = self.pwd,charset = self.charset)
self.cursor = self.conn.cursor()
return 'OK'
#創(chuàng)建查詢一條數(shù)據(jù)的方法
def queryOne(self,sql,params):
#首先連接數(shù)據(jù)庫 獲取游標(biāo)
try:
self.connection()
#執(zhí)行sql語句
count = self.cursor.execute(sql,params)
#返回執(zhí)行結(jié)果
res = self.cursor.fetchone()
return? count,res
except Exception as? ex:
print('失敗,失敗信息是:',ex)
finally:
#調(diào)用關(guān)閉資源的方法
self.closes()
#創(chuàng)建查詢多條數(shù)據(jù)的方法
def queryAll(self,sql,params):
try:
#連接數(shù)據(jù)庫
self.connection()
#執(zhí)行sql語句
count = self.cursor.execute(sql,params)
#獲取結(jié)果
res = self.cursor.fetchall()
return count,res
except Exception as e:
print('查詢失敗 失敗信息是:',e)
finally:
#調(diào)用關(guān)閉資源的方法
self.closes()
#創(chuàng)建修改數(shù)據(jù)庫的方法
def update(self,sql,params):
try:
# 連接數(shù)據(jù)庫
self.connection()
# 執(zhí)行sql語句
count = self.cursor.execute(sql,params)
#把事物(更新的信息寫入數(shù)據(jù)庫)
self.conn.commit()
return count
except Exception as e:
print('查詢失敗 失敗信息是:', e)
#如果更新失敗 數(shù)據(jù)庫就返回到更新之前的數(shù)據(jù)
self.conn.rollback()
finally:
# 調(diào)用關(guān)閉資源的方法
self.closes()
#定義關(guān)閉資源的方法
def closes(self):
if self.cursor != None:
self.cursor.close()
if self.conn != None:
self.conn.close()
================================================================================================
測(cè)試數(shù)據(jù)庫幫助類的文檔:
'''
python調(diào)用mysql分為五個(gè)步驟
1、導(dǎo)入pymysql模塊
2、獲取連接 connect
3、獲取游標(biāo) curs
4、執(zhí)行SQL execute 并返回結(jié)果
5、關(guān)閉資源
'''
#定義用戶操作數(shù)據(jù)庫的一些方法
import sqlHelper
import sys
#創(chuàng)建連接對(duì)象? host,db,user,pwd
helper = sqlHelper.sqlHelper('localhost','db_py1712','root','root')
#print(helper.connection())
# a,s = helper.queryOne('select * from t_stumessage',[])
# print(s)
# a = helper.update('insert into t_stumessage VALUES (uuid() ,%s,%s,%s,%s,%s)',['陳樂樂',123456,21,'男','萬方學(xué)院'])
# print(a)
# a,s = helper.queryAll('select * from t_stumessage',[])
# print(s)
#用戶注冊(cè)
def register(user):
#首先查詢數(shù)據(jù)庫中是否存在該用戶名
#查詢表中所有用戶名
count,res = helper.queryAll('select username from t_stumessage',[])
#遍歷元組 判斷輸入的用戶名是否存在
bn = False
for u in res:
# print(u)
# print(type(u))
if u[0] != user:
bn = True
if bn == False:
pwd = int(input('請(qǐng)輸入密碼'))
age = int(input('請(qǐng)輸入年齡'))
sex = input('請(qǐng)輸入性別')
school = input('請(qǐng)輸入學(xué)校名稱')
#把用戶輸入的信息寫如到數(shù)據(jù)庫表中
# s = helper.connection()
# print(s)
count = helper.update('insert into t_stumessage VALUES (uuid() ,%s,%s,%s,%s,%s)',[user,pwd,age,sex,school])
if count > 0:
print('注冊(cè)成功')
else:
print('注冊(cè)失敗')
else:
print('你輸入的用戶名已經(jīng)存在,請(qǐng)重新輸入')
#register('王少松')
#用戶登錄
def login(user,pwd):
#查詢表中的用戶名和密碼 判斷與用戶輸入的信息是否匹配
count,res = helper.queryAll('select username,pwd from t_stumessage ',[])
return count,res
#遍歷res
# bn = False
# for u in res:
#? ? if u[0] == user and u[1] == pwd:
#? ? ? ? bn =True
# if bn == True:
#? ? print('登錄成功')
# else:
#? ? print('用戶名或密碼錯(cuò)誤,登錄失敗')
# u = input()
# s = input()
# login(u,s)
#用戶列表 根據(jù)用戶名查看用戶自己的基本信息
def messages(user):
count,res = helper.queryOne('select * from t_stumessage where userName = %s',(user,))
return count,res
# if count > 0:
#? ? return res
# else:
#? ? print('查詢失敗')
#messages('趙嬌嬌')
#用戶修改? 根據(jù)用戶名修改密碼
def update(user,pwd):
count = helper.update('update t_stumessage set pwd = %s WHERE username = %s',[pwd,user])
#print(count)
if count > 0:
print('修改成功')
else:
print('修改失敗')
#update('趙嬌嬌','123123')
#用戶刪除 根據(jù)用戶名刪除用戶信息
def dels(user):
count = helper.update('delete from t_stumessage WHERE username = %s',(user,))
if count > 0:
print('刪除成功')
else:
print('刪除失敗')
#dels('趙嬌嬌')
#用戶退出
def exit():
print('退出')
sys.exit()
#exit()
========================================================================
總結(jié)
以上是生活随笔為你收集整理的python访问mysql_python连接mysql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 未成年人禁止“氪金”!抖音直播整改 榜一
- 下一篇: 小米618开门火爆:220秒5亿、850