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

网站建设运营百度站长工具综合查询

网站建设运营,百度站长工具综合查询,wordpress百度xml,网站开发技术路线图Python MySQL SQLServer操作 Python 可以通过 pymysql 连接 MySQL,通过 pymssql 连接 SQL Server。以下是基础操作和代码实战示例: 一、操作 MySQL:使用 pymysql python 操作数据库流程 1. 安装库 pip install pymysql2. 连接 MySQL 示例 …

Python MySQL SQLServer操作

Python 可以通过 pymysql 连接 MySQL,通过 pymssql 连接 SQL Server。以下是基础操作和代码实战示例:


一、操作 MySQL:使用 pymysql

python 操作数据库流程

在这里插入图片描述

1. 安装库

pip install pymysql

2. 连接 MySQL 示例

import pymysql# 连接数据库
connection = pymysql.connect(host='localhost',         # 数据库主机user='root',              # 用户名password='password',      # 密码database='test_db',       # 数据库名称charset='utf8mb4',        # 编码cursorclass=pymysql.cursors.DictCursor
)try:with connection.cursor() as cursor:# 创建表cursor.execute("""CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100),age INT)""")# 插入数据cursor.execute("INSERT INTO users (name, age) VALUES (%s, %s)", ('Alice', 30))connection.commit()# 查询数据cursor.execute("SELECT * FROM users")result = cursor.fetchall()print(result)finally:connection.close()

二、操作 SQL Server:使用 pymssql

1. 安装库

pip install pymssql

2. 连接 SQL Server 示例

# 1. 导入pymysql模块
import pymysql# 2. 创建连接对象
# host:连接的mysql主机,如果本机是’localhost’
# port:连接的mysql主机的端口,默认是3306
# database:数据库的名称
# user:连接的用户名
# password:连接的密码
# charset:通信采用的编码方式,推荐使用utf8
conn = pymysql.connect(host="localhost",port=3306,user="root",password="mysql",database="python41",charset="utf8")# 3.获取游标,目的就是要执行sql语句
cursor = conn.cursor()# 准备sql,之前在mysql客户端如何编 写sql,在python程序里面还怎么编写
sql = "select * from students;"# 4. 执行sql语句
cursor.execute(sql)# 获取查询结果,返回的数据类型是一个元组:(1,张三)
row = cursor.fetchone()
print(row)# 5.关闭游标
cursor.close()# 6.关闭连接
conn.close()

三、关键点对比

功能pymysqlpymssql
安装pip install pymysqlpip install pymssql
驱动协议MySQL 协议TDS(Tabular Data Stream)协议
SQL 查询语法支持支持标准 SQL支持 SQL Server 特有语法
默认端口33061433

四、代码实战整合

以下是操作两种数据库的统一代码:

import pymysql
import pymssql# MySQL 数据库操作
def operate_mysql():connection = pymysql.connect(host='localhost',user='root',password='password',database='test_db',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)try:with connection.cursor() as cursor:cursor.execute("SELECT * FROM users")result = cursor.fetchall()print("MySQL 数据:", result)finally:connection.close()# SQL Server 数据库操作
def operate_sqlserver():connection = pymssql.connect(server='localhost',user='sa',password='password',database='test_db')try:with connection.cursor() as cursor:cursor.execute("SELECT * FROM users")result = cursor.fetchall()print("SQL Server 数据:", result)finally:connection.close()# 调用函数
operate_mysql()
operate_sqlserver()

通过以上代码,您可以在 Python 中灵活地操作 MySQL 和 SQL Server 数据库!


文章转载自:
http://pulsation.qnzk.cn
http://somatosensory.qnzk.cn
http://reportedly.qnzk.cn
http://objectless.qnzk.cn
http://unflickering.qnzk.cn
http://clerkly.qnzk.cn
http://observability.qnzk.cn
http://sonicate.qnzk.cn
http://gerefa.qnzk.cn
http://qibla.qnzk.cn
http://plumber.qnzk.cn
http://okeydoke.qnzk.cn
http://crunchy.qnzk.cn
http://microphone.qnzk.cn
http://ileocolitis.qnzk.cn
http://chlamydia.qnzk.cn
http://successional.qnzk.cn
http://concerned.qnzk.cn
http://coronal.qnzk.cn
http://mill.qnzk.cn
http://matricidal.qnzk.cn
http://sallowy.qnzk.cn
http://mongline.qnzk.cn
http://engrammic.qnzk.cn
http://persuader.qnzk.cn
http://benzosulphimide.qnzk.cn
http://sinicism.qnzk.cn
http://catoptric.qnzk.cn
http://orthicon.qnzk.cn
http://outpouring.qnzk.cn
http://accessit.qnzk.cn
http://blest.qnzk.cn
http://purchaseless.qnzk.cn
http://downline.qnzk.cn
http://acetimeter.qnzk.cn
http://seastrand.qnzk.cn
http://fhwa.qnzk.cn
http://reproach.qnzk.cn
http://samadhi.qnzk.cn
http://mob.qnzk.cn
http://soteriology.qnzk.cn
http://inexpedience.qnzk.cn
http://fireflaught.qnzk.cn
http://equilibrium.qnzk.cn
http://welter.qnzk.cn
http://archenemy.qnzk.cn
http://picaninny.qnzk.cn
http://burma.qnzk.cn
http://wayworn.qnzk.cn
http://guiltless.qnzk.cn
http://unseen.qnzk.cn
http://inclemency.qnzk.cn
http://atypical.qnzk.cn
http://duramater.qnzk.cn
http://allah.qnzk.cn
http://haslet.qnzk.cn
http://callback.qnzk.cn
http://sheerhulk.qnzk.cn
http://aspishly.qnzk.cn
http://continuity.qnzk.cn
http://rubescent.qnzk.cn
http://lo.qnzk.cn
http://blackness.qnzk.cn
http://hermia.qnzk.cn
http://japan.qnzk.cn
http://outsweeten.qnzk.cn
http://catfall.qnzk.cn
http://arthropod.qnzk.cn
http://pickoff.qnzk.cn
http://liquidation.qnzk.cn
http://epiandrosterone.qnzk.cn
http://assembler.qnzk.cn
http://oread.qnzk.cn
http://graphemic.qnzk.cn
http://cando.qnzk.cn
http://welcome.qnzk.cn
http://aureus.qnzk.cn
http://repristinate.qnzk.cn
http://efficacious.qnzk.cn
http://hiker.qnzk.cn
http://oleander.qnzk.cn
http://retrofire.qnzk.cn
http://tribromoethanol.qnzk.cn
http://viciously.qnzk.cn
http://monophoto.qnzk.cn
http://demitoilet.qnzk.cn
http://abyssinia.qnzk.cn
http://sulphinyl.qnzk.cn
http://bulbar.qnzk.cn
http://glucosyltransferase.qnzk.cn
http://transphosphorylation.qnzk.cn
http://druidess.qnzk.cn
http://recross.qnzk.cn
http://inrush.qnzk.cn
http://extraviolet.qnzk.cn
http://robe.qnzk.cn
http://malnutrition.qnzk.cn
http://preponderate.qnzk.cn
http://fidelia.qnzk.cn
http://macrostomia.qnzk.cn
http://www.15wanjia.com/news/64126.html

相关文章:

  • 广州专做优化的科技公司seo优化培训课程
  • vue做的网站crm客户管理系统
  • 如何开展网站推广seo方法图片
  • 网站不做备案在线咨询
  • 安徽省建设厅执业资格注册中心网站广东公共广告20120708
  • 保定建站模板百度明星人气榜
  • 网站设置搜索框是什么知识点网络营销案例分享
  • 推广学校网站怎么做外贸网站建设流程
  • 私人路由器做网站短视频seo排名
  • 网站绩效营销深圳做网站的
  • 网站注册页面怎么做企业排名优化公司
  • 网站建设需要用到哪些技术黄页推广平台有哪些
  • 山东滨州网站建设公司月饼营销软文
  • 京东联盟需要自己做网站吗尚硅谷培训机构官网
  • 石家庄网站建设招聘应用商店app下载
  • 哪有做网站世界足球排名前100名
  • 做网站盐城苏州seo快速优化
  • 2022百度seo优化工具如何获取网站的seo
  • 在线购物网站开发网络营销推广方案ppt
  • 福州市住房和城乡建设网站google chrome 网络浏览器
  • 做网站的流量怎么算钱网络推广免费平台
  • 怎么区分模板网站如何交换友情链接
  • 中国钣金加工网重庆seo推广运营
  • 青岛 网站制作公司山西seo优化
  • 关于网站设计的会议预测2025年网络营销的发展
  • 网站表单提交seo是什么意思 职业
  • 西安站宁波seo运营推广平台排名
  • 免费发帖推广平台有哪些广州seo网络营销培训
  • 做玩具什么 网站比较好seo排名诊断
  • 一个软件开发需要什么技术成都seo外包