欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pymssql數(shù)據(jù)庫(kù)操作MSSQL2005實(shí)例分析

 更新時(shí)間:2015年05月25日 14:49:36   作者:惟愿蓮心不染塵  
這篇文章主要介紹了pymssql數(shù)據(jù)庫(kù)操作MSSQL2005的方法,可實(shí)現(xiàn)基本的連接、查詢、插入、更新及調(diào)用存儲(chǔ)過(guò)程等功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了pymssql數(shù)據(jù)庫(kù)操作MSSQL2005的方法。分享給大家供大家參考。具體如下:

使用的MSSQL2005,通過(guò)pymssql來(lái)連接的。把可能用到的數(shù)據(jù)庫(kù)操作方式都總結(jié)如下,如果要用的時(shí)候就備查啦。

#!/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 長(zhǎng)連接查詢操作(逐條方式獲取數(shù)據(jù))
  sql = "select * from pcontent"
  cur.execute(sql)
  for i in range(cur.rowcount):
    print cur.fetchone()
  #SELECT 短鏈接查詢操作(一次查詢將所有數(shù)據(jù)取出)
  sql = "select * from pcontent"
  cur.execute(sql)
  print cur.fetchall()
  #INSERT 
  sql = "INSERT INTO pcontent(title)VAlUES(%s)"
  uuidstr = str(uuid.uuid1())
  cur.execute(sql,(uuidstr,))
  conn.commit()
  print cur._result
  #INSERT 獲取IDENTITY(在插入一個(gè)值,希望獲得主鍵的時(shí)候經(jīng)常用到,很不優(yōu)雅的方式)
  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]#不知道具體的做法,目前暫時(shí)這樣使用
  conn.commit()
  #Update
  vl = '中國(guó)'
  sql = 'update pcontent set title = %s where id=1'
  cur.execute(sql,(vl,))
  conn.commit()
  #參數(shù)化查詢這個(gè)是為了避免SQL攻擊的
  sql = "select * from pcontent where id=%d"
  cur.execute(sql,(1,))
  print cur.fetchall()
  # 調(diào)用存儲(chǔ)過(guò)程SP_GetALLContent 無(wú)參數(shù)
  sql = "Exec SP_GetALLContent"
  cur.execute(sql)
  print cur.fetchall()
  # 調(diào)用存儲(chǔ)過(guò)程SP_GetContentByID 有參數(shù)的
  sql = "Exec SP_GetContentByID %d"
  cur.execute(sql,(3,))
  print cur.fetchall()
  #調(diào)用存儲(chǔ)過(guò)程SP_AddContent 有output參數(shù)的(很不優(yōu)雅的方式)
  sql = "DECLARE @ID INT;EXEC SP_AddContent 'ddddd',@ID OUTPUT;SELECT @ID"
  cur.execute(sql)
  print cur._result

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論