13 pymysql模块的 基本使用/ sql 注入的问题/增删改查

import pymysql

user=input(‘用户名: ‘).strip()
pwd=input(‘密码: ‘).strip()
#连接MySQL
conn=pymysql.connect(
    host=‘localhost‘,
    user =‘root‘,
    password=‘888888‘,
    database=‘db10‘,
    charset=‘utf8‘
)

#游标
cursor=conn.cursor()

#执行sql 语句
sql=‘select * from userinfo where user= "%s" and pwd="%s"‘%(user,pwd)print(sql)
raws=cursor.execute(sql)

cursor.close()
conn.close()

#判断

if raws:
    print(‘登录成功‘)
else:
    print(‘登录失败‘)

sql 注入的问题:

#执行sql 语句
sql=‘select * from userinfo where user= %s and pwd=%s‘
raws=cursor.execute(sql,(user,pwd))

增删改查:

import pymysql

conn=pymysql.connect(
    host=‘localhost‘,
    user =‘root‘,
    password=‘888888‘,
    database=‘db10‘,
    charset=‘utf8‘
)

#游标
cursor=conn.cursor()

sql =‘insert into userinfo(user,pwd) values(%s,%s);‘
# rows=cursor.execute(sql,(‘iris‘,‘123‘))
# print(rows)
rows=cursor.executemany(sql,[(‘kevin1‘,‘1233‘),(‘kevin2‘,‘111‘),(‘kevin3‘,‘222‘)]) #增加多条
print(cursor.lastrowid)
conn.commit()  #这一条才能让插入语句插入成功
#关闭
cursor.close()
conn.close()

#查询

import pymysql

conn=pymysql.connect(
    host=‘localhost‘,
    user =‘root‘,
    password=‘888888‘,
    database=‘db10‘,
    charset=‘utf8‘
)

#游标
cursor=conn.cursor(pymysql.cursors.DictCursor)

rows=cursor.execute(‘select * from userinfo;‘)

# print(cursor.fetchmany(6))
# print(cursor.fetchall())
# res=cursor.fetchone()
# res2=cursor.fetchone()
# res3=cursor.fetchone()
# print(res)
# print(res2)
# print(res3)
print(cursor.fetchone())
# cursor.scroll(3,mode=‘absolute‘) #绝对移动
cursor.scroll(2,mode=‘relative‘)  #相对移动
print(cursor.fetchone())

#关闭
cursor.close()
conn.close()

原文地址:https://www.cnblogs.com/zhujing666/p/12318238.html

时间: 2024-08-28 16:14:53

13 pymysql模块的 基本使用/ sql 注入的问题/增删改查的相关文章

Python 模拟SQL对文件进行增删改查

1 #!/usr/bin/env python 2 # _*_ coding:UTF-8 _*_ 3 # __auth__: Dalhhin 4 # Python 3.5.2,Pycharm 2016.3.2 5 # 2017/05/15 6 7 import sys,os 8 9 def where(dbfile,where_list): #条件是一个list 10 11 def and_or_where(sub_where_list): 12 '''获取and或同时含有and.or关键字的条

ADO.NET实现对SQL Server数据库的增删改查

了解了上一篇的ADO.NET简介,我们就可以来对数据库进行增删改查等基本操作了!下面是每种操作的具体实现. 先在自定义类的头部定义好数据库连接对象和连接字符串: 1 string connectionString = "Data Source=SC-201607131829;Initial Catalog=Animal;Integrated Security=True"; 2 3 SqlConnection conn; 1.数据库的查询操作,返回一个DataSet 1 public D

JavaWeb SQL常用语言(增删改查)

1.SQL常用插入语INSERT(增) 1.1插入字符类型数据  需要使用单引号'将字符串引起来,如果要插入的值中有单引号',则需要使用两个单引号''进行转义,如下图给student表中插入name值: 1.2插入日期型的数据  需要将日期按照年月日的形式组织好,然后使用单引号引起来.1.3给表中字段赋值  创建一个student表,并给id字段和name字段赋值:1.4一次插入多个值  给student表中一次增加两个值: 2.SQL常用查询语SELECT(查) 2.1查询项目表中的所有字段(

SqL语句基础之增删改查

增查删改的SQL语句,如此的实用,下面我就来简单介绍一下它简单的用法. 1.什么是SQL? SQL是用于访问和处理数据库的标准的一种计算机语言. 2.SQL可以做什么?  (1)可以向数据库进行查询  (2)可以向数据库插入数据  (3) 可以更新数据库中的数据  (4)可以删除数据库中的数据  (5)可以向数据库新增数据 ps:总的可以大体的分为增,删,改,查 3.怎么编写SQL语言? 现在主要编写SQL语言的工具是SQL server,由微软公司发布出来的,由于数据库更新比较繁琐,所以当前使

mybatis反向生成sql,基本的增删改查

用到的几个文件 MyBatisGeneratorProxy.java package com.timestech.wsgk.test.tools; import static org.mybatis.generator.internal.util.ClassloaderUtility.getCustomClassloader; import static org.mybatis.generator.internal.util.messages.Messages.getString; import

偏于SQL语句的 sqlAlchemy 增删改查操作

ORM 江湖 曾几何时,程序员因为惧怕SQL而在开发的时候小心翼翼的写着sql,心中总是少不了恐慌,万一不小心sql语句出错,搞坏了数据库怎么办?又或者为了获取一些数据,什么内外左右连接,函数存储过程等等.毫无疑问,不搞懂这些,怎么都觉得变扭,说不定某天就跳进了坑里,叫天天不应,喊地地不答. ORM 的出现,让畏惧SQL的开发者,在坑里看见了爬出去的绳索,仿佛天空并不是那么黑暗,至少再暗,我们也有了眼睛.顾名思义,ORM 对象关系映射,简而言之,就是把数据库的一个个table(表),映射为编程语

【sql server常用操作{增删改查}】

use DB_x   go   drop database DB_y   create database DB_y --创建数据库   on primary --指定主数据文件   (   name=db, --逻辑名   filename='d:\db.mdf', --文件位置   size=3MB, --初始大小   maxsize=10MB, --最大增长   filegrowth=1MB --增长方式   )   log on --指定日志文件   (   name=ldb,   fil

sql常用命令之增删改查

1. 显示数据库 show databases; show databases; 2. 显示当前数据库 select current_database(); 3. 创建数据库 create database db_name; 4. 选择某个数据库 use db_name; 5. 显示当前数据库下的表 show tables; 6. 创建表 create table create table if not exists my_test( id INT UNSIGNED AUTO_INCREMENT

SQL(2)—增删改查2

简单查询:一.投影select * from 表名select 列1,列2... from 表名select distinct 列名 from 表名二.筛选select top 数字 列|* from 表名(一)等值与不等值select * from 表名 where 列名=值select * from 表名 where 列名!=值select * from 表名 where 列名>值select * from 表名 where 列名<值select * from 表名 where 列名>