python操作MySQL数据库

系列文章目录

python操作MySQL数据库

模块名 pymysql

下载模块名:pip3 install pymysql;

模块的基本使用

import pymysql

conn = pymysql.connect(
	host = '192.168.15.111',
	user ='root',
	port = 3306,
	password = 'Test123!',
	db = 'username',
	charset = 'utf8')
		
#生成游标对象
cursor = conn.cursor(cursor=pymysql.cursor.DictCursor)  #让数据自动转成字典
#定义sql语句
sql= 'select * from username'
#执行sql语句,返回结果
cursor.execute.fetchall()
print(ret)  # 返回值是执行SQL语句之后受影响的行数
# 获取返回结果
#res = cursor.fetchone()
# cursor.scroll(1,'relative')  # 相对当前位置移动
cursor.scroll(0, 'absolute')  # 相对数据开头位置移动
res1 = cursor.fetchall()  # 列表套字典
# res = cursor.fetchone()  # 数据字典
# res = cursor.fetchmany(4)  列表套字典
print(res)
print(res1)


"""

1.execute返回值是执行SQL语句之后受影响的行数

2.fetchall()获取所有的结果
  fetchone()获取结果集第一个结果
  fetchmany()括号内可以指定获取几个结果集
  上述方法对数据的获取也存在光标的概念
"""


SQL注入问题

create table user(id int primary key auto_increment,
                  username char(16),
                  password varchar(16)
                 );

import pymysql

conn = pymysql.connect(
    host='192.168.15.111',
    port=3306,
    user='root',
    password='Test123!',
    db='user',
    charset='utf8',

)

# 生成游标对象
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  

user_name = input('username>>>:').strip()
password = input('password>>>:').strip()

sql = "select * from username where username=%s and password=%s"
# 针对核心数据 不要自己拼接 交由execute方法帮你筛选再拼接
cursor.execute(sql, (user_name, password))
res = cursor.fetchall()
if res:
    print(res)
    print('登录成功')
else:
    print('用户名和密码错误')

注册登录功能

#注册功能
def register(cursor):
    #用户输入
    user_name = input('username>>>:').strip()
    password = input('password>>>:').strip()
    # 定义SQL验证用户名是否存在
    sql = 'select * from username where username=%s'
    #执行SQL语句
    cursor.execute(sql, (user_name,))
    #获取返回结果
    res = cursor.fetchall()
    #如果不存在
    if not res:
        #使用数据库数据插入语句写入到数据库中
        sql1 = 'insert into username(username,password) values(%s,%s)'
        #执行SQL语句
        cursor.execute(sql1, (user_name, password))
        #打印用户登录成功
        print('用户:%s注册成功' % user_name)
    else:
        print('用户已存在')

#登录功能
def login(cursor):
    user_name = input('username>>>:').strip()
    password = input('password>>>:').strip()
    # 定义SQL语句,获取用户输入数据和数据库数据进行比较
    sql = 'select * from username where username=%s'
    #执行SQL语句
    cursor.execute(sql, (user_name,))
    #获取返回结果
    res = cursor.fetchall()
    if res:
        real_res = res[0]
        if password == str(real_res.get('password')):
            print('登录成功')
        else:
            print('密码错误')
    else:
        print('用户不存在')

#数据库连接操作
def get_conn():
    import pymysql
    conn = pymysql.connect(
        host='192.168.15.111',
        port=3306,
        user='root',
        password='Test123!',
        db='user',
        charset='utf8',
        autocommit=True  # 自动二次确认
    )
    #生成一个游标对象
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #让数据自动组成字典
    return cursor


func_dic = {
    '1': register,
    '2': login
}
while True:
    print("""
    1.注册功能
    2.登录功能
    """)
    cursor = get_conn()
    choice = input('请输入功能编号>>>:').strip()
    if choice in func_dic:
        func_name = func_dic.get(choice)
        func_name(cursor)
    else:
        print('当前输入的编号不存在')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值