zoukankan      html  css  js  c++  java
  • pymysql

    pymysql
    import pymysql		# pip3 install pymysql
    
    # 连接
    conn = pymysql.connect(
    		host = '127.0.0.1',
    		port = 3306,
    		user = 'root',
    		passwort = '123',
    		database = 'db44',
    		charset = 'utf8')
    
    # 拿到游标		# 相当于在cmd中的   mysql>
    cursor = conn.cursor()
    
    # 执行sql命令
    res = cursor.exeture('show tables;')
    print(res)
    # 
    data = cursor.fatchall()/fatchon()/fatchmany(条数)
    print(data)			# 以元组的形式显示
    
    conn.commit()		# 提交	只有提交完成以后才能真正的修改服务器的数据
    cursor.close()
    
    # 如果要以字典的形式显示
    cursor = conn.cursor(pymysql.cursor.DictCursor)
    
    控制指针的移动
    # 绝对移动
    cursor.scroll(3,'absolute')
    print(cursor.fatchone())
    # 3:移动的条数
    # absolute:绝对的,每次从最开始的位置开始移动
    
    # 相对移动
    cursor.scroll(1,'relative')
    pirnt(cursor.fatchone())
    # 1:移动的条数
    # relative:相对的,每次从当前位置开始移动
    
    数据库存储数据是登录验证
    import pymysql
    # 创建连接
    conn = pymysql.connect(
    	host='127.0.0.1',
    	port = 3306,
    	user = 'root',
    	password = '123',
    	database = 'db44',
    	charset = 'utf8')
    # 控制光标
    cursor = conn.cursor(pymysql.cursor.DictCursor)
    username = input('name: ').strip()
    password = input('password: ').strip()
    sql = "select * from user where username = '%s' and password = '%s'" %(username,password)
    
    # if username == 'egon' and password == '123':
    
    rows = cursor.execute(sql)
    print(rows)
    if rows:
        print('login success')
    else:
        print('login failure')
    
    sql注入
    用户名>>: xxx or 1=1 --adadfadf
    密码>>:
    
    会登录成功
    
    防止sql注入
    # 不要服务端自己去拼接字符串,让pymysql模块去处理
    
    # 在输入sql语句时
    sql = 'select * from user where username = %s and password = %s'
    # 在想服务端发送数据时
    rows = cursor.execute(sql,(username,password))
    
    一次性插入多行记录
    rows = cursor.executemany(sql,[('aaa','123'),('bbb','123')])
    
  • 相关阅读:
    软件项目技术点(7)——在canvas上绘制自定义图形
    软件项目技术点(4)——实现点击选中画布上元素
    软件项目技术点(5)——在canvas上绘制动态网格线
    软件项目技术点(3)——多画布职责分离
    软件项目技术点(1)——游戏主循环机制
    Node.js Express 框架
    nodejs进阶(7)—async异步流程控制
    利用shell脚本快速定位日志
    MySQL数据库查询所有表名
    MySQL数据库中查询表的所有列名
  • 原文地址:https://www.cnblogs.com/hello-yuanjing/p/10022987.html
Copyright © 2011-2022 走看看