Python Operations SQL Server database

Source: Internet
Author: User
Tags mssql


First install the Pymssql module

Pip install pymssqlcollecting pymssql downloading PYMSSQL-2.1.3-CP35-CP35M-WIN_AMD64.WHL (367kB) 100% |███████████████ █████████████████| 368kB 39kb/sinstalling collected packages:pymssql Found existing installation:pymssql 2.1.2 Uninstalling pymssql-2.1 .2:successfully uninstalled pymssql-2.1.2successfully installed pymssql-2.1.3


Case code:

#!/usr/bin/env python# encoding: utf-8 "" "@version:  ?? @author: phpergao@license: apache licence  @file: mssql.py@time: 2016/7/18  10:18 "" "import pymssqlclass mssql:    " ""      Simple Package for pymssql     pymssql Library, the library is here to download: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql       When using this library, you need to open the TCP/IP protocol in Sql server configuration manager      usage:      "" "    def __init__ (SELF,HOST,USER,PWD,DB):         self.host = host        self.user  = user        self.pwd = pwd         self.db = db    def __getconnect (self):          "" "  &Nbsp;      Get connection Info          return:  conn.cursor ()          "" "        if not  self.db:            raise (NameError, " Database information not set ")         self.conn = pymssql.connect (host= self.host,user=self.user,password=self.pwd,database=self.db,charset= "UTF8")          cur = self.conn.cursor ()         if  Not cur:            raise (NameError, "Connection to database failed" )         else:             return cur    def execquery (Self,sql):          "" "         Execute Query Statement          The element that returns a list,list containing a tuple is a record row, the element of a tuple is the field of the record for each row          The example is called:                 ms = mssql (host= "localhost", user= "sa", pwd= "123456", db= "Pythonweibostatistics")                  reslist = ms. ExecQuery ("Select id,nickname from weibouser")                  for  (Id,nickname)  in resList:                     print  str (ID),nickname         "" "         cur = self.__getconnect ()    &NBsp;    cur.execute (SQL)         reslist =  cur.fetchall ()          #查询完毕后必须关闭连接          self.conn.close ()         return resList     def execnonquery (self,sql):         "" "          Execute non-query statements          invoke example:             cur = self.__getconnect ()              cur.execute (SQL)              self.conn.commit ()              self.conn.close ()          "" "         cur = self.__getconnect ()         cur.execute (SQL)         self.conn.commit ()          self.conn.close () Def main (): ## ms = mssql (host= "localhost", user= "sa", pwd= "123456", db = "Pythonweibostatistics") ##  #返回的是一个包含tuple的list, the element of the list is the record row, and the element of the tuple is the field ## ms for each row of records. Execnonquery ("Insert into weibouser values (' 2 ', ' 3 ')")     ms =  MSSQL (host= "192.168.1.1", user= "sa", pwd= "123456789", db= "Stddata")     reslist = ms . ExecQuery ("Select * from std_store_data")     for i in  reslist:        print  (i) if __name__ ==  ' __main__ ' :     main ()

Results after execution:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/84/4B/wKiom1eMQKTCkv5TAAEPDF-K4VU743.png-wh_500x0-wm_3 -wmp_4-s_253326604.png "title=" qq picture 20160718103546.png "alt=" Wkiom1emqktckv5taaepdf-k4vu743.png-wh_50 "/>

Python Operations SQL Server database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.