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.