python pynssql创建表,删除表,插入数据,查询
Posted 筱筱的春天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pynssql创建表,删除表,插入数据,查询相关的知识,希望对你有一定的参考价值。
import pymssql server=‘10.194.**.***:*****‘ user=‘sa‘ password=‘******‘ database=‘******‘
#连接 conn=pymssql.connect(server,user,password,database) print(server) cursor=conn.cursor() #get cursor print(‘connect to db success‘) #创建表,删除表 cursor.execute(""" IF OBJECT_ID(‘test1‘,‘U‘) IS NOT NULL DROP TABLE test1 CREATE TABLE test1( tid INT NOT NULL, age int, school VARCHAR(150), PRIMARY KEY(tid)) """) conn.commit() #插入数据 cursor.executemany( "insert into test1 values(%d,%d,%s)", [ (10001,5,‘qiuhua school‘), (10002,4,‘chunhua school‘), (10003,6,‘qiu school‘) ] ) conn.commit() #查询 ‘‘‘cursor.execute(‘select * from test1 where school=%s‘,‘qiu school‘)‘‘‘ cursor.execute(‘select * from test1‘) row=cursor.fetchone() for row in cursor: print(row) conn.close() print(‘create table success‘)
以上是关于python pynssql创建表,删除表,插入数据,查询的主要内容,如果未能解决你的问题,请参考以下文章