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')