首页 > 数据库 >python 增删改查sqlserver

python 增删改查sqlserver

时间:2022-11-18 11:22:44浏览次数:47  
标签:demo execute python 改查 sqlserver cursor sql print db

import pymssql

# 打开数据库连接
db = pymssql.connect(server='localhost', user='sa', password='888888',database='customerdb')

# 创建游标对象,并设置返回数据的类型为字典
cursor = db.cursor(as_dict=True)

# 设置立即操作
db.autocommit(True)

# 插入
sql = "insert into demo (name,dec) values (%s,%s);"
#单条记录
cursor.execute(sql,('cx','jamesdec'))
for m in range(2,10):
cursor.execute(sql,('cx'+str(m),'jamesdec'+str(m)))
print(sql)

cursor.executemany(sql,[('cx','jamesdec'),('cx','jamesdec')])
#删除
sql="delete demo where id=%s"
cursor.execute(sql,(442742))
#sql like查询
sql="update demo set name='kkkkkkkkk' where name LIKE '%s'" % ('%%%s%%' % 'kx')
cursor.execute(sql)
print('******************************************************')
print(sql)
print('******************************************************')
db.commit()


#查询
"""
查询一种写法
sql: str="select * from demo where id=%s"%'442632'
cursor.execute(sql)
data=cursor.fetchall()
print(sql)
print(data)
"""
# 查询另外一种写法
sql: str="select * from demo where name=%s"
cursor.execute(sql,('kxkx'))
data=cursor.fetchall()
print(sql)
print(data)
# 关闭游标与数据库连接
cursor.close()
db.close()

标签:demo,execute,python,改查,sqlserver,cursor,sql,print,db
From: https://www.cnblogs.com/dawei163/p/16902622.html

相关文章

  • python模块 - copy模块
    copy模块用于对象的拷贝操作。该模块只提供了两个主要的方法:copy.copy与copy.deepcopy,分别表示浅复制与深复制。b=a.copy():浅拷贝,a和b是一个独立的对象,但他......
  • python-日志详解
    一.logging模块_打印以及保存日志1、Logging:用来做简单的日志。等级分为debug()、info()、warning()、error()和critical()等级 使用场景DEBUG 调试......
  • Python print() 实时打印
    在程序中发现print内容输出和实际输出时间不一致,所以怀疑输出时间有问题,实践发现print没有进行实时刷新。#正常使用print("something。。。")#实时刷新print("someth......
  • Python | Python中 from __future__ import * 的作用
    我们在读代码的时候,总是会看到代码开头会加上from__future__import*这样的语句。这样的做法的作用就是将新版本的特性引进当前版本中,也就是说我们可以在当前版本使用新......
  • python单线程爬虫安装与调试
    信息时代的到来,带给我们海量信息的同时也给我们带来很多有用的价值。如何在这些海量信息池里面找到自己需要的有价值的信息就离不开爬虫技术了,那么在python下如果去部署安装......
  • Python基础之运算符
    一、算数运算符混合运算优先级顺序:()⾼于**⾼于*///%⾼于+-二、赋值运算符单个变量赋值num=1print(num)多个变量赋值num1,float1,str1=10,0.5,'hellowo......
  • No Python interpreter configured for the project
    ......
  • 力扣704(java&python)-二分查找(简单)
    题目:给定一个 n 个元素有序的(升序)整型数组 nums和一个目标值 target ,写一个函数搜索 nums 中的target,如果目标值存在返回下标,否则返回-1。示例1:输入:nums......
  • python笔记75-compile() 函数将字符串转字节代码
    前言compile()函数将一个字符串编译为字节代码。compile()使用以下是compile()方法的语法:compile(source,filename,mode[,flags[,dont_inherit]])参数so......
  • 【Python错误】TypeError: sequence item 0: expected str instance, int found【列表
    【错误类型】TypeError:sequenceitem0:expectedstrinstance,intfound前景提要:获得用户输入的以逗号分隔的三个数字,记为a、b、c,以a为起始值,b为前后相邻数的比值,c为......