网站防御怎么做如何建设大型电子商务网站
网站防御怎么做,如何建设大型电子商务网站,打开百度网址,网站建设-应酷一、安装第三方库PyMySQL
1、在PyCharm中通过 【File】-【setting】-【Python Interpreter】搜索 PyMySQL进行安装 2、通过PyCharm中的 Terminal 命令行 输入: pip install PyMySQL
注#xff1a;通过pip安装#xff0c;可能会提示需要更新pip#xff0c;这时可执行#…一、安装第三方库PyMySQL
1、在PyCharm中通过 【File】-【setting】-【Python Interpreter】搜索 PyMySQL进行安装 2、通过PyCharm中的 Terminal 命令行 输入: pip install PyMySQL
注通过pip安装可能会提示需要更新pip这时可执行pip install --upgrade pip 进行更新pip
二、mysql数据库查询SELECT)
1、pymysql.connect连接数据库
import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf82、使用 cursor( ) 创建游标
import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf8
with connect.cursor() as cursor: # 创建游标3、使用 execute( ) 执行sql语句 import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf8
with connect.cursor() as cursor: # 创建游标 # 创建游标sql SELECT * FROM movie2 cursor.execute(sql) # 执行sql语句4、执行sql语句后需要调用 fetchone() 或 fetchall() 方法来获得查询的返回结果
fetchone(): 该方法获取第一个查询结果集。结果集是一个对象连续多次执行将依次取得下一条结果直到为空fetchall(): 接收全部的返回结果行
import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf8# cursorclasspymysql.cursors.DictCursor 设置游标返回结果为字典
with connect.cursor() as cursor: # 创建游标sql SELECT * FROM movie2 cursor.execute(sql) # 执行sql语句data cursor.fetchall() # 接收全部返回的结果返回一个元祖类型print(f数据库查询数据{data})print(type(data))
connect.close() # 关闭数据库连接数据库查询数据((1, My Neighbor Totoro, 1988), (2, Dead Poets Society, 1989), (3, A Perfect World, 1993), (4, Leon, 1994), (5, Mahjong, 1996), (6, Swallowtail Butterfly, 1996), (7, King of Comedy, 1999), (8, Devils on the Doorstep, 1999), (9, WALL-E, 2008), (10, The Pork of Music, 2012), (12, huawei, 2020))
class tuple
二、mysql数据库更新数据UPDATE
import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf8
with connect.cursor() as cursor: # 创建游标sql UPDATE movie2 SET year 1998 WHERE id 1 cursor.execute(sql) # 执行sql语句connect.commit() # 提交数据到数据库
connect.close() # 关闭数据库连接
在cursor( ) 创建游标后通过execute( ) 执行sql需要通过connect实例调用commit( ) 进行数据提交
三、mysql数据库插入数据INSERT)
import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf8
with connect.cursor() as cursor: # 创建游标sql INSERT INTO movie2(title, year) VALUES (firstday, 2021);cursor.execute(sql) # 执行sql语句connect.commit() # 提交数据到数据库
connect.close() # 关闭数据库连接 四、mysql数据库删除数据DELETE
import pymysql
connect pymysql.connect(host101.37.246.001, # 数据库地址port 3306, # 数据库端口mysql一般默认为3306userxxxx, # 用户名passwordxxxx, # 登录密码databasemovie_cat, # 要连接的数据库名charsetutf8) # 数据库编码一般默认utf8
with connect.cursor() as cursor: # 创建游标sql DELETE FROM movie2 WHERE id 13;cursor.execute(sql) # 执行sql语句connect.commit() # 提交数据到数据库
connect.close() # 关闭数据库连接
注insert/delete/update后都需要调用commit( )提交数据到数据库完成事务提交
封装一个数据库操作类
class ConnectDB:config {host:47.103.126.208,user:siyuan,password:123456,database:mall,charset:utf8,#cursorclass:pymysql.cursors.DictCursor}def __init__(self):self.connect pymysql.connect(**self.config)def select_datas(self, sql):with self.connect.cursor(pymysql.cursors.DictCursor) as cur:cur.execute(sql)data cur.fetchall()print(data)print(type(data))return datadef charge_datas(self, sql):passdef connect_close(self):self.connect.close()def __call__(self, actNone, sqlNone, connectTrue):if connect:if act select:datas self.select_datas(sql)return dataselif act in [update, insert, delete]:self.charge_datas(sql)return selfelse:self.connect_close()if __name__ __main__:connect_db ConnectDB()sql SELECT * FROM ls_user WHERE nickname LIKE %思源%;data1 connect_db(select, sql)data2 connect_db(select, sql)connect_db(connectFalse)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/90206.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!