Python database query data on demand, fetchone, fetchall

Python database on-demand query operation

Python queries the SQL server using the fetchone () method to obtain a single piece of data, and the fetchall () method to obtain multiple pieces of data.


fetchone (): This method gets the next query result set. The result set is an object

fetchall (): Receive all returned result rows

rowcount: This is a read-only attribute and returns the number of rows affected by the execute () method


fetchone():

Return a single tuple, that is, a row (row), if there is no result, return none


fetchall():

Return multiple tuples, that is, return multiple records (rows), if there is no result, return []



Need to indicate: null in sql server and none in Python



The usage is as follows:


pymssql
#Load sql server module

connect = pymssql.connect()
cursor = connect.cursor()
id = ()
sql = %(id)
cursor.execute(sql)
f1 = cursor.fetchone ()

sql1 = cursor.execute(sql1)
f2 = cursor.fetchall()

connect.commit()
cursor.close()
connect.close()

f1:
    (F1 [])
:
    ()
(f2)





Guess you like

Origin blog.51cto.com/11728495/2488699