Pymssql library Installation & calling method, pymssql Library
1. Install pymssql (Linux)
Download pymsql: https://pypi.python.org/pypi/pymssql
Note: First switch the root permission:
Sudo su-
Sudo apt-get install freetds-dev
Cd/home/pi/pymssql
Tar zxvf pymssql-2.1.3.tar.gz
Cd/home/pi/pymssql/pymssql-2.1.3
Python3 setup. py install
Note: If you install pymssql in the Python2.x environment, you must first install it.
Sudo apt-get install python-dev
Then run python setup. py install
2. Install pymssql (windows)
First download pymssql module: http://www.lfd.uci.edu /~ Gohlke/pythonlibs/# pymssql % 20
Open the cmd command line to run
Pip install C: pymssql-2.1.3-cp35-cp35m-win_amd64.whl
3. pymssql call method class
1 # coding = UTF-8 #! /Usr/bin/env python 2 3 import pymssql 4 5 class MSSQL: 6 def _ init _ (self): 7 self. host = ''8 self. user = ''9 self. pwd = ''10 self. db = ''11 12 def _ GetConnect (self): 13 "14. Obtain the connection information. 15. Return: conn. cursor () 16 "17 if not self. db: 18 raise (NameError, "no database information set") 19 self. conn = pymssql. connect (host = self. host, user = self. user, password = self. pwd, database = self. db, timeout = 5, login_timeout = 2, charset = "utf8") 20 cur = self. conn. cursor () 21 if not cur: 22 raise (NameError, "database connection failed") 23 else: 24 return cur25 26 # verify database connection 27 def VerifyConnection (self ): 28 try: 29 if self. host = '': 30 return False31 conn = pymssql. connect (host = self. host, user = self. user, password = self. pwd, database = self. db, timeout = 1, login_timeout = 1, charset = "utf8") 32 return True33 handle T: 34 return False35 36 def ExecQuery (self, SQL ): 37 "38. Execute query statement 39 and return a list containing tuple. The list element is the record row, and the tuple element is the record field 40 in each row. Call example: 42 ms = MSSQL (host = "localhost", user = "sa", pwd = "123456", db = "PythonWeiboStatistics") 43 resList = ms. execQuery ("SELECT id, NickName FROM WeiBoUser") 44 for (id, NickName) in resList: 45 print str (id), NickName46 "47 try: 48 cur = self. _ GetConnect () 49 cur.exe cute (SQL) 50 resList = cur. fetchall () 51 # connection 52 self must be closed after query. conn. close () 53 return resList54 failed T: 55 return 'error' 56 57 def ExecNonQuery (self, SQL): 58 "" 59 run a non-query statement 60 call example: 61 cur = self. _ GetConnect () 62 cur.exe cute (SQL) 63 self. conn. commit () 64 self. conn. close () 65 "" 66 cur = self. _ GetConnect () 67 cur.exe cute (SQL) 68 self. conn. commit () 69 self. conn. close () 70 71 def ExecStoreProduce (self, SQL): 72 "73 run the query statement 74. a list containing tuple is returned, and the list element is the record row, the tuple element is the field 75 76 for each record call example: 77 MS = MSSQL (host = "localhost", user = "sa", pwd = "123456 ", db = "PythonWeiboStatistics") 78 resList = ms. execQuery ("SELECT id, NickName FROM WeiBoUser") 79 for (id, NickName) in resList: 80 print str (id), NickName81 "82 cur = self. _ GetConnect () 83 cur.exe cute (SQL) 84 resList = cur. fetchall () 85 self. conn. commit () 86 # The Connection 87 self must be closed after the query is complete. conn. close () 88 return resList89 90 def insert_Pic (self, SQL, arg): 91 92 # Save the Photo 93 cur = self. _ GetConnect () 94 cur.exe cute (SQL, arg) 95 self. conn. commit () 96 self. conn. close ()