import pymssql
conn = pymssql.connect(host='127.0.0.1',
user='sa',
password='123',
database='SQLTest',
charset='utf8')
#查看连接是否成功
cursor = conn.cursor()
sql = 'select * from student'
cursor.execute(sql)
#用一个rs变量获取数据
rs = cursor.fetchall()
print(rs)
try:
conn = pymssql.connect(host='127.0.0.1',
user='sa',
password='123',
database='SQLTest',
charset='utf8')
cursor = conn.cursor()
sql = 'insert into student values('0001', '张三', 18, '男', '文学院')'
cursor.execute(sql)
conn.commit()
except Exception as ex:
conn.rollback()
raise ex
finally:
conn.close()
'''
TestDB类
功能:测试数据库的类写法
作者:PyLearn
博客: http://www.cnblogs.com/PyLearn/
最后修改日期: 2017/10/17
'''
import pymssql
class TestDB():
def __init__(self):
try:
self.conn = pymssql.connect(host='127.0.0.1',
user='sa',
password='123',
database='SQLTest',
charset='utf8')
self.cursor = self.conn.cursor()
self.sql = "insert into student values('0001', '张三', 18, '男', '文学院')"
self.cursor.execute(self.sql)
self.conn.commit()
except Exception as ex:
self.conn.rollback()
raise ex
finally:
self.conn.close()
if __name__ == '__main__':
test_DB = TestDB()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有