首页 > 数据库 >python sqlserver

python sqlserver

时间:2022-11-24 16:48:41浏览次数:33  
标签:语句 python sqlserver cursor result sql pymssql conn

python连接并简单操作SQLserver数据库
实验环境:
python版本3.9

Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
1
2
3
SQLserver版本2019

exec xp_msver
1


基本步骤:
安装pymssql模块
#pip 安装pymssql (python3.6以上直接安装不上)
pip install pymssql
1
2
#在官网下载pymssql对应版本,在当前文件夹下
https://pypi.org/project/pymssql/
pip install pymssql-2.2.4-cp39-cp39-win_amd64.whl
1
2
3
编写代码
sqlserver.py

#coding=utf-8 #settng设置全局为utf—8格式
#导包
import pymssql
#创建连接字符串 (sqlserver默认端口为1433)
conn =pymssql.connect(host='localhost', #这里的host='_'可以用本机ip或ip+端口号
server="******",#本地服务器
port="1433",#TCP端口
user="sa",password="******",
database="******",
charset="GBK"
#这里设置全局的GBK,如果设置的是UTF—8需要将数据库默认的GBK转化成UTF-8
)
if conn:
print('连接数据库成功!')#测试是否连接上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
查询
#查询语句
cursor =conn.cursor() #使用cursor()方法获取操作游标
sql_select="SELECT * FROM STUDENT" #数据库查询语句
cursor.execute(sql_select) #执行语句
results =cursor.fetchall() #获取所有记录列表
#print('原始:',end="")
#print(result) #后边有空格

for result in results:
result= list(result) #元组转化为列表
for res in range (len(result)):
if isinstance(result[res],str):
result[res]=result[res].replace(' ','')解决空格问题
result=tuple(result) #列表再转换为元组
print("处理后:",end="")
print(result)
#
conn.commit()
#关闭数据库
conn.close()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
原始:

 

处理后:

 

插入
#插入语句
cursor =conn.cursor()
sql_insert="insert into student values(990031,'wang',20,'男','bb','aa')"
cursor.execute(sql_insert)
conn.commit()
conn.close()
1
2
3
4
5
6
更新
#更新语句
cursor =conn.cursor()
sql_update="update student set AGE=21 where SNO='990031' "
cursor.execute(sql_update)
conn.commit()
conn.close()
1
2
3
4
5
6
删除语句
#删除语句
cursor =conn.cursor()
sql_delete="delete from student where sno='990031'"
cursor.execute(sql_delete)
conn.commit()
conn.close()
1
2
3
4
5
6
增加表
#新建表
cursor =conn.cursor()
sql_create="""create table table_student(SNO int primary key not null,SNAME varchar(100),SDEPT varchar(100),BPLACE varchar(100))"""
cursor.execute(sql_create)
conn.commit()
conn.close()
1
2
3
4
5
6
错误1:
报错


原因
没有把TCP/IP协议打开

查看TCP端口位置:

 

保证TCP/IP开启:

开启之后,服务,重新启动服务

 

 

错误2:
出现查询结果乱码:
解决办法:
查看sqlserver的编码:

select COLLATIONPROPERTY('chinese_PRC_Stroke_CI_AI_KS_WS','CodePage')
1


查询结果:

936 简体中文gbk

950 繁体中文BIG5

437 加拿大/美国 英语

932 日文

949 韩文

866 俄文

标签:语句,python,sqlserver,cursor,result,sql,pymssql,conn
From: https://www.cnblogs.com/ruiy/p/16922324.html

相关文章

  • python8
    一、创建计算BMI指数的模块deffun_bmi(person,height,weight):'''功能:根据身高体重计算BMI指数person:姓名height:身高,单位:米weight:体重,单......
  • python arp欺骗
     ls(ARP())hwtype    :XShortField                        =1              ('1')ptype     :XShortEnumField......
  • centos下python2.7.5升级到python3.5版本
    1.我们先下载python3.5的版本在我们的服务器任意一个文件夹,博主是放在home目录下,我们先进入到该目录:百度网盘python3.5.2下载链接:链接:https://pan.baidu.com/s/1Wp04mcKo......
  • python练习题-函数-练习(一)
    1.编写一个函数cacluate,可以接收任意个数,返回的是一个元组,其中元组的第一个值为所有参数的平均值,第二个值是大于平均值的所有数。defcacluate(*args):printargs,......
  • Chapter 2: Python Language Basics, IPython, and Jupyter Notebooks 个人理解与问题
    2.2IPython基础2.2.2运行Jupyternotebook  在终端输入\(Jupyter\quadnotebook\),会在默认浏览器打开\(Jupyter\),但是注意路径问题,如果我们在D:\Python_Code\IPy......
  • python装饰器 - 修改函数返回值
    deff(n):n+=1print("hello:{}".format(n))returnn+1ret=f(9)print("Functionreturnvalue:",ret)在没有装饰器的情况下,运行结果如下hello:1......
  • 【python自动化】02. pywin32库自动操作键鼠(保姆级代码注释)
     目录源码和工具下载大漠综合工具->坐标和窗口信息抓取在你的桌面上新建一个记事本用于后面的代码测试完整项目源码实现思路介绍win32的基础思路基本步......
  • Python第十周
    一.   实验目的和要求二.  实验环境 python3.1064-bit三.  实验过程实例1代码如下:1deffun_bmi(person,height,weight):2'''功能:根据身高和体......
  • 【Python字符串方法】字符串类型判断、大小写转化、拆分和组合、填充和对齐
    去空白字符str.strip():去掉字符串左右两边的空格str.lstrip():去掉字符串左边的空格str.rstrip():去掉字符串右边的空格以上函数返回从处理后的字符串,并不在原对象上......
  • python - metaclass
     metaclass的作用-在python的世界,一起都是object.类也是object。上面这张图很好的描述了class类的创建过程。通过下面的例子来了解类的创建过程,实例对象的创......