How to call a stored procedure
The Return Type
Return to Table:
--示例表
create table Salary(
Sno varchar(20) NOT NULL REFERENCES Staff(Sno)ON DELETE CASCADE, --职工号
Base decimal(20,2) not null , --基础薪资
Bonus decimal(20,2) not null , --津贴奖金
Stock decimal(20,2) not null , --股份分红
constraint cons_Base check(Base >=2000 and Base <=50000),
constraint cons_Bonus check(Bonus<=20000 and Bonus>=0),
constraint cons_Stock check(Stock<=100000 and Stock>=0)
);
--返回结果集table的proc
create proc test_proc_select
as
select * from Salary
import pymssql
conn = pymssql.connect(host='127.0.0.1',user='sa',
password='xxxxxxxxxx',database='Bank',charset='GBK')
cursor = conn.cursor()
cursor.execute('exec test_proc_select')
result = cursor.fetchall() #得到结果集
for item in result:
print(item) #遍历打印查询结果集的数据
( '642825', Decimal ( '15000.00'), Decimal ( '5000.00'), Decimal ( '5000.00'))
( '666666', Decimal ( '7000.00'), Decimal ( '2000.00'), Decimal ( '1500.00' ))
Back return 'int / string':
I did not know how to solve! ! ! Hope fellow answer!
How to solve the garbage problem reading Chinese
charset set to 'GBK'; not 'utf8', because the SQL server default format is GBK
import pymssql
conn = pymssql.connect(host='127.0.0.1',user='sa',
password='xxxxxxxxxxx',database='Bank',charset='GBK')
Connection Objects
Connection objects should respond to the following methods.
Connection methods
Close connection objects
.commit()
Commit any pending transaction to the database.
Perform any transaction is delayed
Connection objects waiting to be executed under the sql statement
sql='''insert into test1
values(1)'''
cursor.execute(sql)
# 提交到数据库执行;没有此句,插入语句将不会执行
conn.commit()
sql statement is not executed after execute, before going in connection.commit (after)
Rollback previously performed, undo changes to the database caused
Closing a connection without committing the changes first will cause an implicit rollback to be performed.
.cursor()
Cursor variable return
from pymssql import connect
server='127.0.0.1'
user='sa'
password='xx94524253xx'
database='users'
conn=connect(host='%s'%(server),user='%s'%(user),password='%s'%(password),database='%s'%(database),charset='GBK')
cursor = conn.cursor()
sql='''insert into test1
values(1)'''
cursor.execute(sql)
try:
cursor.execute(sql)
# 提交到数据库执行;没有此句,插入语句将不会执行
conn.commit()
cursor.close()
except:
conn.rollback()
conn.close()
Cursor Objects
Cursor object. That is a connection object creates multiple cursor, interconnected with each other, are caused by changes in a cursor will be reflected in the other cursors,
Cursor attributes
Contains seven
- The name attribute name
- type_code type coding
- display_size
- internal_size
- precision
- scale
- null_ok
name and type_code is mandatory, regardless of whether the value to be taken; others are optional, if empty, the corresponding value is empty;
When the cursor operation does not return a result set return result set, .description return None
cursor.execute('exec test_proc_select @int=1')
result = cursor.fetchall() #得到结果集
print('description:')
print(cursor.description)
print('fetch_result:'+str(result))
description:
(('Sno', 1, None, None, None, None, None), ('Base', 5, None, None, None, None, None), ('Bonus', 5, None, None, None, None, None), ('Stock', 5, None, None, None, None, None))
fetch_result:[('642825', Decimal('15000.00'), Decimal('5000.00'), Decimal('5000.00')), ('666666', Decimal('7000.00'), Decimal('2000.00'), Decimal('1500.00'))]
The reaction last rows execute () the influence of the initial value 0
sql='insert into test1 values(%d)'
cursor.executemany(sql,(11,12,13))
conn.commit()
print(str(cursor.rowcount)) # 3
Cursor methods
.callproc( procname [, parameters ] )
Two approaches are not feasible! ! !
cursor.callproc('test_proc_select',(1))
cursor.callproc('test_proc_select',1)
.close()
Close the cursor
.execute(operation [, parameters])
Perform the appropriate SQL statements
.executemany ( SQL statement, element sequence parameter set )
With different parameters while performing the same format statement
sql='insert into test1 values(%d)'
cursor.executemany(sql,(11,12,13))
conn.commit()
import pymssql
conn = pymssql.connect(host='127.0.0.1',user='sa',
password=pwd,database='Bank',charset='GBK')
cursor = conn.cursor()
cursor.execute('exec test_proc_select')
result = cursor.fetchone() #得到结果集
print('fetch_result:'+str(result))
count = 0
for item in result:
print('item_'+str(count)+':'+str(item)) #遍历打印查询结果集的数据
count = count+1
ps: Here a small additional rules on the print py, if it is a separate non-print print str variable can be compiled; if a print which contains variables and non-str str variable is being given to. For example as follows:
test1=1
test2=2
print(test1+test2)#可以通过
test1=1
test2='2'
print(test1+test2)#不可以通过
.fetchone () can take an element of the result set back each execution, if a result set only two elements, and called three times .fetchone (), the result of the last to None
print(str(cursor.fetchone()))
print(str(cursor.fetchone()))
print(str(cursor.fetchone()))
.fetchmany([size=cursor.arraysize])
#当list总共只有两个元素时只要size>=2,无论为多少
rs = cursor.fetchmany(size=2)
print(rs)
rs = cursor.fetchmany(size=3)
print(rs)
#输出结果一样
Extracting all elements of the result set, and go to the end of collection
rs = cursor.fetchall() # 提取所有结果集赋值给rs变量,并使cursor直接调至集尾
Corresponds .fetchmany (szie = max_size)
If the first .fetchall (), after .fetchone () the value of None
print(str(cursor.fetchall()))
print(str(cursor.fetchone()))
.nextset()
This method will make the cursor skip to the next available set, discarding any remaining rows from the current set.
The method skips the current result set units (either list, tuple ... corresponding to skip all the remaining elements of the current)
When the result set only under the second element, the first either fetchone () preferably under the first element, the second either fetchone () or None
print(str(cursor.fetchone()))
cursor.nextset()
print(str(cursor.fetchone()))
This read/write attribute specifies the number of rows to fetch at a time with .fetchmany(). It defaults to 1 meaning to fetch a single row at a time.
Implementations must observe this value with respect to the .fetchmany() method, but are free to interact with the database a single row at a time. It may also be used in the implementation of .executemany().
You can not pass!
result = cursor.fetchmany(size=cursor.arraysize)