大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说python对数据库mysql的操作(增删改查)「终于解决」,希望您对编程的造诣更进一步.
#coding=utf-8
import pymysql
# #查询
# def connMySQL():
# try:
# conn=pymysql.connect(‘localhost‘,‘root‘,‘admin‘)
# 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)
# #列表推导式
# db=[item for item in data]
# print(db)
# # 打开之后进行关闭,不然会占用资源
# finally:
# cur.close()
# conn.commit()
# conn.close()
#
# print(connMySQL())
# #插入数据
# def insertMySql():
# try:
# conn=pymysql.connect(‘localhost‘,‘root‘,‘admin‘)
# 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()
# # 打开之后进行关闭,不然会占用资源
# finally:
# cur.close()
# conn.close()
#
# insertMySql()
#插入数据
# #删除数据
# def deleteMySql():
# try:
# conn=pymysql.connect(‘localhost‘,‘root‘,‘admin‘)
# 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()
# # 打开之后进行关闭,不然会占用资源
# finally:
# cur.close()
# conn.close()
#
# deleteMySql()
class MySqlHelper:
def conn(self):
con=pymysql.connect(
host=‘127.0.0.1‘,
user=‘root‘,
passwd=‘admin‘,
db=‘aimin‘)
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(‘请输入用户名: ‘)
password=input(‘请输入密码: ‘)
result=checkValid(username,password)
if result:
print(‘登录成功,昵称:{0}‘.format(username))
else:
print(‘失败‘)
if __name__==‘__main__‘:
info()
代码100分
python对数据库mysql的操作(增删改查)
原文地址:https://www.cnblogs.com/fengzixian/p/13343200.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/7254.html