python增删改查mysql_python对数据库mysql的操作(增删改查)
#coding=utf-8
import pymysql
# #查詢(xún)
# def connMySQL():
# try:
# conn=pymysql.connect(‘localhost‘,‘root‘,‘a(chǎn)dmin‘)
# conn.select_db(‘five‘)
# except Exception as e:
# return e.args
# else:
# cur=conn.cursor()
# cur.execute("select * from users")
# data=cur.fetchall()
# # for item in data:
# # print(item)
# #列表推導(dǎo)式
# db=[item for item in data]
# print(db)
# # 打開(kāi)之后進(jìn)行關(guān)閉,不然會(huì)占用資源
# finally:
# cur.close()
# conn.commit()
# conn.close()
#
# print(connMySQL())
# #插入數(shù)據(jù)
# def insertMySql():
# try:
# conn=pymysql.connect(‘localhost‘,‘root‘,‘a(chǎn)dmin‘)
# conn.select_db(‘five‘)
# except Exception as e:
# return e.args
# else:
# cur=conn.cursor()
# #單條插入
# # sql=‘insert into users values (%s,%s,%s,%s)‘
# # params=(12,‘wei‘,99,‘changjiang‘)
# #多條插入
# sql = ‘insert into users values (%s,%s,%s,%s)‘
# params=[(13,‘wei‘,99,‘changjiang‘),(14,‘wei‘,99,‘changjiang‘),(15,‘wei‘,99,‘changjiang‘)]
# cur.executemany(sql,params)
# conn.commit()
# # 打開(kāi)之后進(jìn)行關(guān)閉,不然會(huì)占用資源
# finally:
# cur.close()
# conn.close()
#
# insertMySql()
#插入數(shù)據(jù)
# #刪除數(shù)據(jù)
# def deleteMySql():
# try:
# conn=pymysql.connect(‘localhost‘,‘root‘,‘a(chǎn)dmin‘)
# conn.select_db(‘five‘)
# except Exception as e:
# return e.args
# else:
# cur=conn.cursor()
# #多條插入
# cur.execute = ‘delete * from users where id= 1‘
# conn.commit()
# # 打開(kāi)之后進(jìn)行關(guān)閉,不然會(huì)占用資源
# finally:
# cur.close()
# conn.close()
#
# deleteMySql()
class MySqlHelper:
def conn(self):
con=pymysql.connect(
host=‘127.0.0.1‘,
user=‘root‘,
passwd=‘a(chǎn)dmin‘,
db=‘a(chǎn)imin‘)
return con
def get_one(self,sql,params):
cur=self.conn().cursor()
data=cur.execute(sql,params)
result=cur.fetchone()
return result
def checkValid(username,password):
opera=MySqlHelper()
sql=‘select * from login where username=%s and password=%s‘
params=(username,password)
return opera.get_one(sql=sql,params=params)
def info():
username=input(‘請(qǐng)輸入用戶(hù)名:\n‘)
password=input(‘請(qǐng)輸入密碼:\n‘)
result=checkValid(username,password)
if result:
print(‘登錄成功,昵稱(chēng):{0}‘.format(username))
else:
print(‘失敗‘)
if __name__==‘__main__‘:
info()
python對(duì)數(shù)據(jù)庫(kù)mysql的操作(增刪改查)
標(biāo)簽:username???passwd???man???__name__???cep???commit???inpu???and???數(shù)據(jù)
本條技術(shù)文章來(lái)源于互聯(lián)網(wǎng),如果無(wú)意侵犯您的權(quán)益請(qǐng)點(diǎn)擊此處反饋版權(quán)投訴
本文系統(tǒng)來(lái)源:https://www.cnblogs.com/fengzixian/p/13343200.html
總結(jié)
以上是生活随笔為你收集整理的python增删改查mysql_python对数据库mysql的操作(增删改查)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle11g创建闪回分区多大,Or
- 下一篇: 语音识别热词_出门问问 TWS 耳机语音