python + mysql table data for query

Examples are as follows:

pymysql Import 

DEF select_form ():
# Open Database Connectivity
DB = pymysql.connect ( "localhost", "the root", "123456", "Test")

# using the cursor () method to get the cursor operation
Cursor db.cursor = ()

# SQL query
SQL = "" "the FROM the SELECT * Student the WHERE ID =% S" ""% (3)

the try:
# execute SQL statements
cursor.execute (SQL)
# get a list of all the records
Results = cursor.fetchall ()
for Row in Results:
ID = Row [0]
name = Row [. 1]
Age = Row [2]
address = Row [. 3]
the create_time = Row [. 4]
Print ( "ID =% S, name =% S, Age =% S , address =% s,create_time=%s"%\
(ID, name, Age, address, the create_time))

the except Exception AS E:
Print ( "Error Query:% S Case"% E)

the finally:
# off wiper is connected
cursor.close ()
# close the connection
db.Close ()

main DEF ():
select_form ()

IF the __name__ == '__main__':
main ()


Guess you like

Origin www.cnblogs.com/Teachertao/p/11285469.html