当前位置: 首页 > 工具软件 > PySQL > 使用案例 >

pysql

乐正德华
2023-12-01

1.python语言操作mysql的包:

import pymysql
# 连接mysql服务器

conn = pymysql.connect(host='localhost', user='root', password='123',database='db2', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "select * from student where id > %s " % (12,)
# sql =
cursor.execute(sql)

# res = cursor.fetchone()
res = cursor.fetchmany(10)
# res = cursor.fetchall()  ### 列表里面套字典
print(res)

cursor.close()
conn.close()

import pymysql
# 连接mysql服务器

conn = pymysql.connect(host='localhost', user='root', password='123',database='db1', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "delete from t7 where id=3"
cursor.execute(sql)

### 删除和更新的时候, 需要事物提交
conn.commit()


# res = cursor.fetchone()
# res = cursor.fetchmany(10)
# res = cursor.fetchall() ### 列表里面套字典
# print(res)

cursor.close()
conn.close()
import pymysql
# 连接mysql服务器

conn = pymysql.connect(host='localhost', user='root', password='123',database='db1', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "delete from t7 where id=3"
cursor.execute(sql)

### 删除和更新的时候, 需要事物提交
conn.commit()


# res = cursor.fetchone()
# res = cursor.fetchmany(10)
# res = cursor.fetchall() ### 列表里面套字典
# print(res)

cursor.close()
conn.close()




  注:a.文件名不能写自己本身

    b.connect----->conn----->cursor

    c.执行sql语句  ----->excute(sql)
    d.取数据: fetchone()   取一条 ;    fetchmany()  取多条;   fetchall()取全部

    e:增加删除:     conn.commit()        *****需要事物提交

    插入一条数据:  cursor.execute('sql,('lxxx,'34234'))

    查如多条数据:      data = [('aaaaa', 'aaa'),('bbbb', 'bbb'),('lxxx,'34234')]

                cursor.execute('sql,data)

2.sol注入

  写sql语句,%传值时,要加引号:  sql="select * from t4 where name='%s' and pwd ='%s' "%(username,pwd)

上面那么写sql语句有风险,eg :
例一: username = zekai' # select * from t4 where name = 'zekai' #' and pwd = '' 例二: username = dbsahvbdsha' or 1=1 # select * from t4 where name = 'dbsahvbdsha' or 1=1

  我们把这样的问题称之为sql 注入,之所以出现这样 根源是 太过于相信用户的输入,导致我们接受用户传入的参数时,没有对其进行转义;

  解决方案:

    a.自己动手对用户输入的值进行转义;

    b.使用execute()自动进行过滤eg:

        sql = "select * from t4 where name = %s and pwd = %s" 

     cursor.execute(sql,(username, pwd))
3.事物
  定义:一组操作,要me成功,要么失败
  特性:a.原子性:一组操作要me成功,要么失败
     b.一致性:事物发生前与发生后 ,数据的 总额 依然 不变 ,能与之匹配
     c.隔离性:简单说 就是一个事物的操作对其它事物是不可见得
     d.持久性: 当事务完成后, 其影响已形成,不能撤销,只能通过另一个事物来抵消失误

场景:
            思考:
                我去银行给朋友汇款,
                我卡上有1000元,
                朋友卡上500元,
                我给朋友转账100元(无手续费),
                如果,网线断了, 我的钱刚扣,而朋友的钱又没加时, 怎么办?
            
            create table t11 (
                id int auto_increment primary key,
                name varchar(32) not null default '',
                money int not null default 0
            )engine=Innodb  charset=utf8;
            
            insert into t11 (name,money) values ('zekai', 1000), ('eagon', 500);
            
        
        解决方法:
        
            开启事务 (start transaction)
            
            (执行sql操作)
            
            commit : 提交上面的SQL, 让其生效
            
            rollback: 回滚
            
            show full tables;  显示全部类型
视图:
        
        产生的原因:
            如果有一个SQL语句频繁的会被使用到,比如说:
                select * from t4 where id>12 and id <24;
        
        搞一个映射,或者取一个别名
                select * from t4 where id>12 and id <24   === > v1
        视图:
        
            select * from v1;
            
        创建视图:
            
            create view v1 as select * from t4 where id>12 and id <24;
        
        修改视图:
            alter view v1 as sql语句;
            
        删除视图:
            drop view v1;
            
        
        问题:
            如果原生的表数据发生了变化, 那视图会不会发生变化? 也会变化
            
            视图中的数据会不会发生修改? 不会发生修改
            
        应用场景:
            
            MySQL: (DBA)
                生成视图View
            
            程序:
                调用  select  * from v1;
    
    
    
函数:
        
        不要轻易使用
        
        在程序中, 用代码计算, 计算好了, 再传给SQL语句执行
    
    
存储过程:
        
        将一大堆 SQL 语句进行封装, 类似于函数, 结果就是存储过程
        
        MySQL服务端:
            DBA (写)
        
        
        a. 简单的存储过程:
            delimiter //
            create procedure p1()
            BEGIN
                select * from t11;
            END //
            delimiter ;
            
            程序:
                call p1();
        
        b. 传参数: (in)
            delimiter //
            create procedure p2(
                in n1 int,
                in n2 int
            )
            BEGIN
                select * from t11 where id > n1;
            END //
            delimiter ;
            
            程序:
                call p2(12, 2)
        
        c. 传入参数: (out)
            
            delimiter //
            create procedure p3(
                in n1 int,
                out n2 int
            )
            BEGIN
                select * from t11 where id > n1;
                set n2 =  1;
            END //
            delimiter ;
            
            
            set @v2=123212;
            call p3(12, @v2);
            
            select @v2;
            
            
触发器:
        
        向用户表中添加一条数据的同时, 在日志表中也添加一条记录
        
        delimiter //
        CREATE TRIGGER t1 BEFORE INSERT ON t7 FOR EACH ROW
        BEGIN
            insert into t11 (name, money) values ('xxx', 1234);
        END //
        delimiter ;
        

 

 

  

转载于:https://www.cnblogs.com/wyf20190411-/p/11028751.html

 类似资料:

相关阅读

相关文章

相关问答