python3远程连接SQL-server

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011798443/article/details/80974166

首先需要python安装一个模块: pymssql 模块。

直接pip安装就好了!我已经安装过了。


接下来就是主要代码了:

import pymssql
class linkDB():
	def linkdb():
		#数据库远程连接
		conn = pymssql.connect(host="数据库IP地址:端口号",user="用户名",password="密码",database="数据库名",charset="utf8")
		# 使用cursor()方法获取操作游标
		cursor = conn.cursor()
		#查询语句
		sql = "这里面是sql语句"
		try:
			cursor.execute(sql)  #游标
			result = cursor.fetchall() #查询
			print(result)
		except:
			print("连接数据库报错了!")
		#关闭数据库连接
		conn.close()   
			
if __name__ == '__main__':
	linkDB.linkdb()
以上就是使用python去远程连接sql-server数据库了。


猜你喜欢

转载自blog.csdn.net/u011798443/article/details/80974166