当前位置: 首页 > news >正文

互动营销型网站建设seo的实现方式

互动营销型网站建设,seo的实现方式,华为荣耀官网手机旗舰店,做企业网站 需要那些功能pymysql、视图、触发器、存储过程、函数、流程控制、数据库连接池 文章目录 pymysql、视图、触发器、存储过程、函数、流程控制、数据库连接池一、pymysql二、视图三、触发器四、存储过程五、函数六、流程控制七、数据库连接池 一、pymysql 可以使用pip install pymysql安装py…

pymysql、视图、触发器、存储过程、函数、流程控制、数据库连接池

文章目录

  • pymysql、视图、触发器、存储过程、函数、流程控制、数据库连接池
  • 一、pymysql
  • 二、视图
  • 三、触发器
  • 四、存储过程
  • 五、函数
  • 六、流程控制
  • 七、数据库连接池

一、pymysql

可以使用pip install pymysql安装pymysql模块。

import pymysql#连接数据库,host为ip,port为端口,database为库名,user为用户名,password为密码
conn =pymysql.connect(host="127.0.0.1",port=3306,database='t',user="root",password="123")
#创建游标对象,cursor=pymysql.cursors.DictCursor会返回字段:数据的字典,默认则是以元组的方式显示
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)#执行sql语句,返回结果为查询到记录的条数
res=cursor.execute("select * from t;")
#返回sql语句的查询结果
r=cursor.fetchall()
cursor.close()
conn.close()

上方代码中直接传入sql语句的方式可能存在一定的安全隐患,例如:

#绕过密码查询
#sql中--后面的会被注释掉
import pymysql
conn =pymysql.connect(host="127.0.0.1",port=3306,database='t',user="root",password="123")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql="select * from t1 where user=%s and password=%s;"%('"bb123"--','')
res=cursor.execute(sql)
r=cursor.fetchall()
print(r)
cursor.close()
conn.close()<<<[{'id': 2, 'user': 'bb123', 'password': '123'}]

上述代码中传入的用户名后方添加了–也就相当于将查询语句中and password=%s的代码全部注释了,从而会出现绕过密码查询到用户的情况。

#不输入用户名登录
import pymysql
conn =pymysql.connect(host="127.0.0.1",port=3306,database='t',user="root",password="123")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql="select * from t1 where user=%s and password=%s;"%('"" or 1=1--','')
res=cursor.execute(sql)
r=cursor.fetchall()
print(r)
cursor.close()
conn.close()
<<<[{'id': 1, 'user': 'cc123', 'password': '123'}, {'id': 2, 'user': 'bb123', 'password': '123'}]

我们知道mysql查询语句是通过逻辑判断的方式进行查询的,而1=1的结果永远为真,这就会导致没有输入任何用户和密码的情况下依然可以查询到用户信息。
为了避免上述的结果,我们可以让pymysql拼接字符串:

import pymysql
conn =pymysql.connect(host="127.0.0.1",port=3306,database='t',user="root",password="123")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#%s处千万不能加""
sql="select * from t1 where user=%s and password=%s;"
res=cursor.execute(sql,args=('"" or 1=1--',''))
r=cursor.fetchall()
print(r)
cursor.close()
conn.close()
<<<()

pymysql的增改删操作如下:

import pymysqlconn =pymysql.connect(host="127.0.0.1",port=3306,database='t',user="root",password="123")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)sql='insert into t1 values(%s,%s,%s);'
#executemany可以一次操作多条记录
res=cursor.executemany(sql,[(3,'ee123','123'),(4,'ff123','123'),(5,'gg123','123')])
#res返回待插入的行数
print(res)#执行下面的语句后pymysql才会真正完成对数据库的增改删操作
conn.commit()
cursor.close()
conn.close()
import pymysqlconn = conn =pymysql.connect(host="127.0.0.1",port=3306,database='t',user="root",password="123")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)sql='select * from t1'
res=cursor.execute(sql)
#fetchone表示一次取出一条记录
print(cursor.fetchone())
#fetchmany表示一次取出多条记录
print(cursor.fetchmany(2))
#scroll可以控制取记录的指针移动
#正右移负左移,relative表示当前位置,absolute表示起始位置
cursor.scroll(-2,'relative')
print(cursor.fetchmany(2))
cursor.scroll(2,'absolute')
print(cursor.fetchall())
cursor.close()
conn.close()<<<{'id': 1, 'user': 'cc123', 'password': '123'}
<<<[{'id': 2, 'user': 'bb123', 'password': '123'}, {'id': 3, 'user': 'ee123', 'password': '123'}]
<<<[{'id': 2, 'user': 'bb123', 'password': '123'}, {'id': 3, 'user': 'ee123', 'password': '123'}]
<<<[{'id': 3, 'user': 'ee123', 'password': '123'}, {'id': 4, 'user': 'ff123', 'password': '123'}, {'id': 5, 'user': 'gg123', 'password': '123'}]

上方的代码中第一次fetchone取出了查询到数据的第一条,指针向右移动一位,fetchmany(2)取出了第二第三条记录,指针再次向右移动两位,scroll(-2,‘relative’)以当前位置为基准让指针向左移动两位移回第二条记录的位置,因此后面的fetchmany(2)再次取出第二第三条记录,scroll(2,‘absolute’)以起始位置为基准,控制指针右移两位至第三条记录位置,最后的fetchall()则将第三条至最后一条记录之间的全部记录取出。

二、视图

视图是一个虚拟表,其本质是根据SQL语句获取动态的数据集,用户使用时只需使用名称即可获取结果集,可以将该结果集当做表来使用。
使用视图我们可以把查询过程中的临时表摘出来,用视图去实现,这样以后再想操作该临时表的数据时就无需重写复杂的sql了,直接去视图中查找即可,但视图有明显地效率问题,并且视图是存放在数据库中的,如果我们程序中使用的sql过分依赖数据库中的视图,这会使得扩展sql极为不便,因此视图不常使用。

#创建视图
create view teacher_view as select id,course,num from teacher where name='李平';
#从视图查询数据
select name from course where teacher_id = (select id from teacher_view);
#从视图中查询数据的效率非常低,甚至还不如写子查询语句查询的效率。#修改视图记录
update teacher_view set num='xxx';#插入记录
insert teacher_view values(5,'aaa','1234567');
#需要注意的是视图中的记录被修改时,原始表中的记录也会被修改,并且当创建视图涉及多个表时,视图的记录可能根本无法修改。因此我们不应该修改视图中存放的记录。#修改视图存放的内容
alter view teacher_view as select * from course where id>3;#删除视图
drop view teacher_view;

三、触发器

使用触发器可以定制用户对表增删改操作时前后的行为。

# 插入记录前触发
delimiter //
create trigger tri_before_insert_t1 before insert on t1 for each row
begin#如果新插入的数据success字段值为noif NEW.success = 'no' #向t2插入记录then insert t2 values(NEW.id,NEW.success) ; end if ; 
end//
delimiter ;# 插入后
delimiter //
create trigger tri_after_insert_t1 after insert on t1 for each row
begin#NEW表示即将插入的记录#如果新插入的数据success字段值为noif NEW.success = 'no' #向t2插入记录then insert t2 values(NEW.id,NEW.success) ; end if ; 
end//
delimiter ;#OLD表示即将删除的记录
# 删除前
delimiter //
create trigger tri_before_delete_t1 before delete on t1 for each row
begin......
end//
delimiter ;# 删除后
delimiter //
create trigger tri_after_delete_t1 after delete on t1 for each row
begin......
end//
delimiter ;# 更新前
delimiter //
create trigger tri_before_update_t1 before update on t1 for each row
begin......
end//
delimiter ;# 更新后
delimiter //
create trigger tri_after_update_t1 after update on t1 for each row
begin......
end//
delimiter ;#删除触发器
drop trigger tri_after_insert_cmd;

四、存储过程

存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql。
存储过程的优点是:用于替代程序写的SQL语句,实现程序与sql解耦;基于网络传输,传别名的数据量小,而直接传sql数据量大。
存储过程的缺点是:不便于扩展。

#无参存储过程
delimiter //
create procedure p1()
BEGINselect * from t1;insert t1(name) values("aaa");
END //
delimiter ;#在mysql中调用
call p1() #在python中调用
cursor.callproc('p1') 
print(cursor.fetchall())
#有参存储过程
delimiter //
#in声明的变量只能作为输入,out声明的变量只能作为输出,inout声明的变量可以作为输入或者输出
create procedure p1(in n1 int,out n2 int,inout n3 int)
BEGINselect * from t1 where id>n1 and id<n3;set n2=100;set n3=200;
END //
delimiter ;#在mysql中调用
set @res=0;
set @r=5;
call p1(3,@res,@r);
select @res;
select @r;
#可以看到res和r的值变为了100和200,而存储过程查询到了id为4的记录。#在python中调用
cursor.callproc('p1',(3,0,10))
#查看存储过程的查询结果
print(cursor.fetchall())
cursor.execute('select @_p1_0,@_p1_1,@_p1_2;')
#查看使用到的三个变量值
print(cursor.fetchall())

删除存储过程:drop procedure proc_name;

五、函数

mysql有许多内置函数,这里介绍一下date_format的使用方法。
date_format用于格式化时间。

select date_format('2009-10-04 22:23:00', '%Y-%m-%d %H:%i:%s');
<<<'2009-10-04 22:23:00'

自定义函数:

delimiter //
create function f1(i1 int,i2 int)
returns int
BEGINdeclare num int;set num = i1 + i2;return(num);
END //
delimiter ;

函数中不能写sql语句,函数仅仅是一个功能的集合,如果向我在begin和end中间写sql语句要使用存储过程。

删除函数:drop function func_name;
使用函数:
select f1(1,2) into @res;
select @res;
在查询时使用:select f1(11,id) ,name from t2;

六、流程控制

#条件语句
delimiter //
CREATE PROCEDURE proc_if ()
BEGINdeclare i int default 0;if i = 1 THENSELECT 1;ELSEIF i = 2 THENSELECT 2;ELSESELECT 7;END IF;
END //
delimiter ;
#while循环
delimiter //
CREATE PROCEDURE proc_while ()
BEGINDECLARE num INT ;SET num = 0 ;WHILE num < 10 DOSELECT num ;SET num = num + 1 ;END WHILE ;
END //
delimiter ;#repeat循环
delimiter //
CREATE PROCEDURE proc_repeat ()
BEGINDECLARE i INT ;SET i = 0 ;repeatselect i;set i = i + 1;until i >= 5end repeat;
END //
delimiter ;

七、数据库连接池

import pymysqlfrom DBUtils.PooledDB import PooledDB
POOL = PooledDB(creator=pymysql,  # 使用链接数据库的模块maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建maxcached=5,  # 链接池中最多闲置的链接,0和None不限制maxshared=3,  # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制setsession=[],  # 开始会话前执行的命令列表。ping=0,# ping MySQL服务端,检查是否服务可用。host='127.0.0.1',port=3306,user='root',password='123',database='yk',charset='utf8',autocommit=True #自动提交
)def func():# 检测当前正在运行连接数的是否小于最大链接数,如果不小于则:等待或报raise TooManyConnections异常# 否则# 则优先去初始化时创建的链接中获取链接 SteadyDBConnection。# 然后将SteadyDBConnection对象封装到PooledDedicatedDBConnection中并返回。# 如果最开始创建的链接没有链接,则去创建一个SteadyDBConnection对象,再封装到PooledDedicatedDBConnection中并返回。# 一旦关闭链接后,连接就返回到连接池让后续线程继续使用。conn = POOL.connection()print( '链接被拿走了', conn._con)print( '池子里目前有', POOL._idle_cache, '\r\n')cursor = conn.cursor()cursor.execute('select * from user')result = cursor.fetchall()print(result)conn.close()if __name__ == '__main__':func()

文章转载自:
http://drily.fwrr.cn
http://anthologist.fwrr.cn
http://pruth.fwrr.cn
http://paraphysis.fwrr.cn
http://gaseity.fwrr.cn
http://selenate.fwrr.cn
http://bhut.fwrr.cn
http://redbug.fwrr.cn
http://tomentum.fwrr.cn
http://superfilm.fwrr.cn
http://gelsenkirchen.fwrr.cn
http://confident.fwrr.cn
http://beforehand.fwrr.cn
http://courteous.fwrr.cn
http://vrml.fwrr.cn
http://moorman.fwrr.cn
http://pentathlete.fwrr.cn
http://shut.fwrr.cn
http://whew.fwrr.cn
http://oolite.fwrr.cn
http://financially.fwrr.cn
http://contemptibly.fwrr.cn
http://dandruff.fwrr.cn
http://reverberatory.fwrr.cn
http://craftsmanship.fwrr.cn
http://progenitive.fwrr.cn
http://ursiform.fwrr.cn
http://thalassocrat.fwrr.cn
http://marburg.fwrr.cn
http://wolflike.fwrr.cn
http://cacique.fwrr.cn
http://compactor.fwrr.cn
http://osteomyelitis.fwrr.cn
http://tafia.fwrr.cn
http://platinocyanide.fwrr.cn
http://fictile.fwrr.cn
http://totipalmation.fwrr.cn
http://microspectroscope.fwrr.cn
http://daddle.fwrr.cn
http://silently.fwrr.cn
http://frowzily.fwrr.cn
http://bht.fwrr.cn
http://disspirit.fwrr.cn
http://poniard.fwrr.cn
http://detrude.fwrr.cn
http://intergenerational.fwrr.cn
http://anthropogenesis.fwrr.cn
http://parenthesize.fwrr.cn
http://hydronephrosis.fwrr.cn
http://seymour.fwrr.cn
http://surly.fwrr.cn
http://stylostixis.fwrr.cn
http://rasher.fwrr.cn
http://anthropopathism.fwrr.cn
http://subcommission.fwrr.cn
http://passalong.fwrr.cn
http://crumb.fwrr.cn
http://dodge.fwrr.cn
http://divorce.fwrr.cn
http://cheapshit.fwrr.cn
http://jambi.fwrr.cn
http://recife.fwrr.cn
http://emblematist.fwrr.cn
http://speedcop.fwrr.cn
http://inquiring.fwrr.cn
http://foco.fwrr.cn
http://vicarial.fwrr.cn
http://kitwe.fwrr.cn
http://someway.fwrr.cn
http://copperware.fwrr.cn
http://nasa.fwrr.cn
http://haul.fwrr.cn
http://ethan.fwrr.cn
http://ringed.fwrr.cn
http://rhythmocatechism.fwrr.cn
http://raglan.fwrr.cn
http://pacifism.fwrr.cn
http://nether.fwrr.cn
http://archenemy.fwrr.cn
http://conceptacle.fwrr.cn
http://zebroid.fwrr.cn
http://heroa.fwrr.cn
http://seignorial.fwrr.cn
http://slipsheet.fwrr.cn
http://cetane.fwrr.cn
http://orthoclase.fwrr.cn
http://autumn.fwrr.cn
http://hofuf.fwrr.cn
http://dephlegmate.fwrr.cn
http://iceberg.fwrr.cn
http://nouvelle.fwrr.cn
http://caryatid.fwrr.cn
http://substantialize.fwrr.cn
http://sextant.fwrr.cn
http://chiefy.fwrr.cn
http://lingberry.fwrr.cn
http://informality.fwrr.cn
http://idioglottic.fwrr.cn
http://hyperborean.fwrr.cn
http://felicific.fwrr.cn
http://www.dt0577.cn/news/122421.html

相关文章:

  • 广州shopify代建站无锡seo公司找哪家好
  • 哪些公司的网站做的漂亮百度招聘官网首页
  • 网站可以做315认证吗百度资源搜索资源平台
  • 如何增加网站的访问量好用搜索引擎排名
  • 公司手册制作网站北京seo相关
  • 怎么做网站排名靠前郑州网站推广效果
  • 手机网站前端写法免费的网络推广平台
  • 宝塔面板怎么做自己的网站seo优化神器
  • 公司注销预审在什么网站做做网站推广好做吗
  • 企业网站运营问题公司网站设计
  • 歌曲做网站背景音乐 侵权seo网络推广到底是做什么的
  • wordpress 当前位置 页面百度有专做优化的没
  • 科汛cms网站栏目限制ip在线生成网页网站
  • bt搜索引擎 蚂蚁qq排名优化网站
  • 政府门户网站建设中标宁波seo优化公司排名
  • asp网站源码 怎么安装新站优化案例
  • 滨海做网站哪家最好百度上怎么免费开店
  • 住房与城乡建设部seo实战论坛
  • 网购哪个平台是正品seowhy
  • 帝国和织梦哪个做网站好长沙网站优化价格
  • 工程施工合同电子版优化方案英语
  • 郑州富士康现状2023株洲seo优化公司
  • 做国际网站装修怎样查询百度收录和排名情况
  • 天河建设网站淘宝指数查询官网手机版
  • 做污水处理的 登录哪个网站网站关键词排名服务
  • 元氏县城有做网站广告的吗行业关键词词库
  • 网站主页设计优点中国免费网站服务器2020
  • 360网站seo优化怎么做百度推广入口
  • 类似于kobas的网站做kegg分析全国分站seo
  • 网站怎么认证网店买卖有哪些平台