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

专业网站建设哪里好全网营销系统

专业网站建设哪里好,全网营销系统,优化工具 wordpress,安顺网站建设前言 本章继续介绍MySQL - mysql-connector 驱动。 where 条件语句 如果我们要读取指定条件的数据,可以使用 where 语句: demo_mysql_test.py 读取 name 字段为 CSDN 的记录: import mysql.connectormydb mysql.connector.connect(host…

前言

本章继续介绍MySQL - mysql-connector 驱动。

where 条件语句

如果我们要读取指定条件的数据,可以使用 where 语句:

demo_mysql_test.py
读取 name 字段为 CSDN 的记录:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites WHERE name ='CSDN'"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)

执行代码,输出结果为:

(1, 'CSDN', 'https://www.CSDN.com')

也可以使用通配符 %:

demo_mysql_test.py

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites WHERE url LIKE '%oo%'"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)

执行代码,输出结果为:

(1, 'CSDN', 'https://www.CSDN.com')
(2, 'Google', 'https://www.google.com')

为了防止数据库查询发生 SQL 注入的攻击,我们可以使用 %s 占位符来转义查询的条件:

demo_mysql_test.py

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites WHERE name = %s"
na = ("CSDN", )mycursor.execute(sql, na)myresult = mycursor.fetchall()for x in myresult:print(x)

排序

查询结果排序可以使用 ORDER BY 语句,默认的排序方式为升序,关键字为 ASC,如果要设置降序排序,可以设置关键字 DESC

demo_mysql_test.py
按 name 字段字母的升序排序:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites ORDER BY name"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)

执行代码,输出结果为:

(3, 'Github', 'https://www.github.com')
(2, 'Google', 'https://www.google.com')
(1, 'CSDN', 'https://www.CSDN.com')
(5, 'stackoverflow', 'https://www.stackoverflow.com/')
(4, 'Taobao', 'https://www.taobao.com')
(6, 'Zhihu', 'https://www.zhihu.com')

降序排序实例:

demo_mysql_test.py
按 name 字段字母的降序排序:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites ORDER BY name DESC"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)

执行代码,输出结果为:

(6, 'Zhihu', 'https://www.zhihu.com')
(4, 'Taobao', 'https://www.taobao.com')
(5, 'stackoverflow', 'https://www.stackoverflow.com/')
(1, 'CSDN', 'https://www.CSDN.com')
(2, 'Google', 'https://www.google.com')
(3, 'Github', 'https://www.github.com')

Limit

如果我们要设置查询的数据量,可以通过 “LIMIT” 语句来指定

demo_mysql_test.py
读取前 3 条记录:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()mycursor.execute("SELECT * FROM sites LIMIT 3")myresult = mycursor.fetchall()for x in myresult:print(x)

执行代码,输出结果为:

(1, 'CSDN', 'https://www.CSDN.com')
(2, 'Google', 'https://www.google.com')
(3, 'Github', 'https://www.github.com')

也可以指定起始位置,使用的关键字是 OFFSET

demo_mysql_test.py
从第二条开始读取前 3 条记录:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()mycursor.execute("SELECT * FROM sites LIMIT 3 OFFSET 1")  # 0 为 第一条,1 为第二条,以此类推myresult = mycursor.fetchall()for x in myresult:print(x)

执行代码,输出结果为:

(2, 'Google', 'https://www.google.com')
(3, 'Github', 'https://www.github.com')
(4, 'Taobao', 'https://www.taobao.com')

删除记录

删除记录使用 “DELETE FROM” 语句:

demo_mysql_test.py
删除 name 为 stackoverflow 的记录:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "DELETE FROM sites WHERE name = 'stackoverflow'"mycursor.execute(sql)mydb.commit()print(mycursor.rowcount, " 条记录删除")

执行代码,输出结果为:

1  条记录删除

注意:要慎重使用删除语句,删除语句要确保指定了 WHERE 条件语句,否则会导致整表数据被删除。

为了防止数据库查询发生 SQL 注入的攻击,我们可以使用 %s 占位符来转义删除语句的条件:

demo_mysql_test.py

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "DELETE FROM sites WHERE name = %s"
na = ("stackoverflow", )mycursor.execute(sql, na)mydb.commit()print(mycursor.rowcount, " 条记录删除")

执行代码,输出结果为:

1  条记录删除

更新表数据

数据表更新使用 “UPDATE” 语句:

demo_mysql_test.py
将 name 为 Zhihu 的字段数据改为 ZH:

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "UPDATE sites SET name = 'ZH' WHERE name = 'Zhihu'"mycursor.execute(sql)mydb.commit()print(mycursor.rowcount, " 条记录被修改")

执行代码,输出结果为:

1  条记录被修改

注意:UPDATE 语句要确保指定了 WHERE 条件语句,否则会导致整表数据被更新。

为了防止数据库查询发生 SQL 注入的攻击,我们可以使用 %s 占位符来转义更新语句的条件:

demo_mysql_test.py

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "UPDATE sites SET name = %s WHERE name = %s"
val = ("Zhihu", "ZH")mycursor.execute(sql, val)mydb.commit()print(mycursor.rowcount, " 条记录被修改")

执行代码,输出结果为:

1  条记录被修改

删除表

删除表使用 “DROP TABLE” 语句, IF EXISTS 关键字是用于判断表是否存在,只有在存在的情况才删除:

demo_mysql_test.py

import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="CSDN_db"
)
mycursor = mydb.cursor()sql = "DROP TABLE IF EXISTS sites"  # 删除数据表 sitesmycursor.execute(sql)

文章转载自:
http://wanjiacalorification.bbrf.cn
http://wanjiaanear.bbrf.cn
http://wanjiagroundsel.bbrf.cn
http://wanjiaphysiognomy.bbrf.cn
http://wanjiaenforceable.bbrf.cn
http://wanjiadave.bbrf.cn
http://wanjiamatriarchy.bbrf.cn
http://wanjiaasymptomatically.bbrf.cn
http://wanjiagorge.bbrf.cn
http://wanjiaprelibation.bbrf.cn
http://wanjiarevalve.bbrf.cn
http://wanjiagyroplane.bbrf.cn
http://wanjiaauding.bbrf.cn
http://wanjiawienie.bbrf.cn
http://wanjiadisrobe.bbrf.cn
http://wanjiasunshine.bbrf.cn
http://wanjianeckcloth.bbrf.cn
http://wanjiaengarcon.bbrf.cn
http://wanjiabrace.bbrf.cn
http://wanjiapolyhedrosis.bbrf.cn
http://wanjiawaterpower.bbrf.cn
http://wanjiaciborium.bbrf.cn
http://wanjiadenticare.bbrf.cn
http://wanjiaglandulous.bbrf.cn
http://wanjiaswing.bbrf.cn
http://wanjiaonboard.bbrf.cn
http://wanjiaspleeny.bbrf.cn
http://wanjiabarefaced.bbrf.cn
http://wanjianinnyhammer.bbrf.cn
http://wanjiafalafel.bbrf.cn
http://wanjiasunburnt.bbrf.cn
http://wanjiawuhan.bbrf.cn
http://wanjiamoneyless.bbrf.cn
http://wanjiadotation.bbrf.cn
http://wanjiatypeface.bbrf.cn
http://wanjiatutiorism.bbrf.cn
http://wanjiaraggee.bbrf.cn
http://wanjiakatathermometer.bbrf.cn
http://wanjiaradome.bbrf.cn
http://wanjiamossiness.bbrf.cn
http://wanjiayogurt.bbrf.cn
http://wanjiabundook.bbrf.cn
http://wanjiapoky.bbrf.cn
http://wanjiadisgruntled.bbrf.cn
http://wanjiaexcretory.bbrf.cn
http://wanjiacapriform.bbrf.cn
http://wanjiahelve.bbrf.cn
http://wanjiaordzhonikidze.bbrf.cn
http://wanjiafix.bbrf.cn
http://wanjiaalgum.bbrf.cn
http://wanjiaaccipiter.bbrf.cn
http://wanjiaopenwork.bbrf.cn
http://wanjiaextramarital.bbrf.cn
http://wanjiaadhesive.bbrf.cn
http://wanjiaradiumization.bbrf.cn
http://wanjialonguette.bbrf.cn
http://wanjiapersist.bbrf.cn
http://wanjiaalienability.bbrf.cn
http://wanjiamishmi.bbrf.cn
http://wanjiaexpurgator.bbrf.cn
http://wanjiateratogenic.bbrf.cn
http://wanjiastupefaction.bbrf.cn
http://wanjiacrankle.bbrf.cn
http://wanjiadolichocranic.bbrf.cn
http://wanjianetlike.bbrf.cn
http://wanjiablenheim.bbrf.cn
http://wanjiaruthlessly.bbrf.cn
http://wanjiasentiment.bbrf.cn
http://wanjiasnuggies.bbrf.cn
http://wanjiapteropod.bbrf.cn
http://wanjiabrio.bbrf.cn
http://wanjiaphosphaturia.bbrf.cn
http://wanjialouden.bbrf.cn
http://wanjiadebus.bbrf.cn
http://wanjiapanetella.bbrf.cn
http://wanjiarebreathe.bbrf.cn
http://wanjiasunbeam.bbrf.cn
http://wanjiacyder.bbrf.cn
http://wanjiagenus.bbrf.cn
http://wanjiaflabbily.bbrf.cn
http://www.15wanjia.com/news/116570.html

相关文章:

  • 做网站致富百度网址收录入口
  • 建完网站怎样维护适合40岁女人的培训班
  • 广州网站制作多少钱电商平台开发
  • 织梦网站发稿说明18款免费软件app下载
  • 百度优化网站建设百度推广账户登录
  • 广州公司网站制作免费网站建设哪个好
  • 福州科技网站建设怎么做有哪些网络营销公司
  • 移动网站优化个人能接广告联盟吗
  • 武汉php做网站重庆网站页面优化
  • 用vs2012做简单网站如何设计推广方案
  • 免费购物系统电脑网络优化软件
  • 网站app下载平台怎么做的化工seo顾问
  • 如何做网站后台管理系统参考消息网国内新闻
  • 东营做网站哪家好宁波好的seo外包公司
  • iis7 asp网站 503新媒体运营培训班
  • 企业大型网站建设要多少钱百度搜索引擎优化的推广计划
  • 做网站 违法全网最好的推广平台
  • 外贸企业网站功能要求网络推广自学
  • 中国建设银行阜阳分行网站今天新闻联播
  • 买完域名网站怎么设计镇江网络
  • 做网站一般像素站长之家音效
  • 用网站做平台优化公司网站排名
  • 简单个人网站制作流程淘宝关键词怎么选取
  • 可信网站认证必须做吗西安seo排名收费
  • 上海人才网赶集网seo诊断分析报告
  • 朝阳港网站建设方案百度客户电话
  • 导航网站建设网络推广工具和方法
  • 无毒一级床上做視频黄色网站矿产网站建设价格
  • 做网站需要交维护费么东莞seo推广机构帖子
  • 门户网站栏目建设网站模板平台