pymssql Database Operation MSSQL2005 Example analysis

Source: Internet
Author: User
Tags commit uuid

This article illustrates the method of pymssql database operation MSSQL2005. Share to everyone for your reference. Specifically as follows:

Use of MSSQL2005, connected by pymssql. The possible use of the database operations are summarized below, if you want to use the time for reference.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the #!/usr/bin/env python #coding =utf-8 from __future__ import with_statement from contextlib import closing import inspect Import pymssql import UUID import datetime #查询操作 with closing (pymssql.connect (host= ' localhost ', user= ' sa ', password= ') Pppp ', database= ' blogs ') as Conn:cur = Conn.cursor () #SELECT Long Connection query operation (fetch data by method) sql = "SELECT * from Pcontent" Cur.execut E (SQL) for I in Range (Cur.rowcount): Print Cur.fetchone () #SELECT A short link query operation (once a query takes all the data out) sql = "SELECT * from Pcontent" cur. Execute (SQL) print cur.fetchall () #INSERT sql = INSERT into pcontent (title) VAlUES (%s) uuidstr = str (UUID.UUID1 ()) Cur.ex Ecute (SQL, (Uuidstr,)) Conn.commit () print Cur._result #INSERT get identity (often used when inserting a value in the hope of obtaining a primary key) sql = "Insert Into Pcontent (title) VAlUES (%s); SELECT @ @IDENTITY "uuidstr = str (UUID.UUID1 ()) Cur.execute (SQL, (Uuidstr,)) print" Arraysite: ", cur.arraysize print Cur._ Result[1][2][0][0] #不知道具体的做法, this is the temporary use of conn.commit () #Update VL = ' China ' sql = ' Update pcontent set title =%s where id=1 ' cur . Execute (SQL, (VL,)) conn.commit () #参数化查询这个是为了避免SQL攻击的 sql = "SELECT * from Pcontent where id=%d" Cur.execute (SQL, (1,)) PRI NT Cur.fetchall () # Calling stored procedures sp_getallcontent parameterless sql = "Exec sp_getallcontent" Cur.execute (SQL) print Cur.fetchall () # calls stored Process Sp_getcontentbyid sql = "Exec Sp_getcontentbyid%d" Cur.execute (SQL, (3,)) print Cur.fetchall () #调用存储过程SP_ Addcontent (very elegant way) with output parameters sql = "DECLARE @ID INT; EXEC sp_addcontent ' ddddd ', @ID OUTPUT; Select @ID cur.execute (SQL) print Cur._result

I hope this article will help you with your Python programming.

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.