Operating python --- mysql database

1, the beginning of the use of pymysql

import pymysql

db_config = {
    'host' :'127.0.0.1',
    'user':'root',
    'password':'123456',
    'port' :3306,
    'database':'test',
}

conn = pymysql.connect(**db_config)
cursor = conn.cursor()

query_sql = " The SELECT * = MobilePhone, the FROM TEST_DATA the WHERE '17,122,223,333' " 

the cursor.execute (query_sql) 

RESULT1 = cursor.fetchall () # nested tuple of tuples 
Print (type (RESULT1)) 
Print (RESULT1) 

the cursor.execute (query_sql) # here you need to perform it again to 
result2 = cursor.fetchone () 
Print (of the type (result2)) 
Print (result2) 

cursor.close () 
conn.Close () 

console output:
 < class  ' tuple ' > 
(( 32 173 ,   ' 17,122,223,333 ' , . 1 , A datetime.datetime ( 2018 , . 9, 12, 22, 32, 27), 0.0, 0.0),)
<class 'tuple'>
(32173, '17122223333', 1,  datetime.datetime(2018, 9, 12, 22, 32, 27), 0.0, 0.0)

 

2, the cursor

Cursor (cursor)

   A cursor is an open system for the user data buffer, storing the results of the SQL statement, the user can obtain one by one from the cursor SQL statement record, and assigned to the primary variable, referred python further treatment, only once a set of main variables storing a record, only the primary variable and can not fully meet the requirements of the SQL statements to the application output data.

  

  Advantages cursor and cursor: In the database, the cursor is a very important concept. Cursor provides a flexible means of the data retrieved from the table is operated, in essence, the cursor is actually a record of each extraction mechanism comprises a plurality of data records from the result set. Cursor always associated with an SQL select statement as a cursor result set (zero may be a bar, one or retrieved by the select statement associated plurality of records) and the position of the cursor result set to a specific recording composition. When deciding on the result set processing, you must declare a cursor result set point.

 

Reference Bowen: https: //www.cnblogs.com/huangdongju/p/7871677.html

Reference Bowen: https: //www.cnblogs.com/oukele/p/10684639.html

Guess you like

Origin www.cnblogs.com/hzgq/p/12114295.html