CRUD operations python mysql

# ### python CRUD operations mysql

mysql python operation enabled by default transaction must be after additions and deletions to submit data,
will make a difference to the database, otherwise the default rollback
submit data conn.commit ()
rollback data conn.rollback ()

the Execute execute sql
executemany execute multiple sql (when the insert, may be used)

pymysql.connect = conn (Host = " 127.0.0.1 " , the User = " root " , password = " 123456 " , Database = " testdb1 " )
 "" " This statement can be found so that the data field becomes: record a dictionary for easy viewing "" " 
the Cursor = conn.cursor (the Cursor = pymysql.cursors.DictCursor)


By #

# SQL = "INSERT INTO T1 (FIRST_NAME, last_name, Age, Sex, Money) values (% S,% S,% S,% S,% S)" 
SQL = " INSERT INTO T1 values (% S,% S, S%,% S,% S,% S) " 
# once inserted into a data 
RES = the cursor.execute (SQL, ( " circumferential " , " Yongling " , 81,1,9.9,8 ))
 Print (RES)
 # inserting a plurality of data 
# RES = cursor.executemany (SQL, (( "it", "training", 20,0,15000), ( "normal", "away", 90,0,10000), ( "Li "," de bright ", 18,1,8.8))) 
# Print (RES) # represents the number of data insertion 

# get id number of the last piece of data in a single statement for execution and returns the last id 
Print (cursor.lastrowid) 
 '' ' '' 'If there is no check on id number is zero, this is the real id id, rather than count of how many lines, your search is incremented
That field, according to the field increment the last value inserted to your return, there is no increment field all returns 0
# If you are performing multiple data executemany, obtained by way of a reverse search 
# the SELECT T1 from the above mentioned id the Order by the above mentioned id desc limit 1

 

# Change

= SQL " Update SET FIRST_NAME T1 WHERE ID =% S% S = " 
RES = the cursor.execute (SQL, ( " King pock " ,. 8 ))
 Print (RES) 

IF RES:
     Print ( " Update Success " )
 the else :
     Print ( " update failed " )

 

# Charles

= SQL " SELECT * from T1 "  # . 6 ~ 65 
RES = the cursor.execute (SQL)
 Print (RES) 

# (. 1) acquires a data either fetchone 
RES = cursor.fetchone ()
 Print (RES) # { 'ID':. 6 , 'first_name': 'constant', 'last_name': 'far', 'Age': 90, 'Sex': 0, 'Money': 10000.0} 

# (2) acquiring a plurality of data fetchmany default search a last time data query, obtaining down 
data cursor.fetchmany = (. 3 )
 Print (data)

 for row in data:
 if row["sex"]==1:
 sex="男"
 else:
 sex="女"
 print("姓:{},名:{},年龄:{},性别:{},收入:{}".format(row["first_name"],row["last_name"],row["age"],sex, row["money"]))

 

# ### scroll you can customize the location of the query

= SQL " the SELECT * from T1 the WHERE the above mentioned id> = 50 " 
RES = cursor.execute (SQL)
 # 1. Scroll relative 
# to search a check of = 50 the above mentioned id 
RES = cursor.fetchone ()
 Print (RES)
 # again after id = 3 search scroll 50, and 51 is started from the roller id = 3 => id = the roll 53, the check to check 54 is id = 
cursor.scroll (. 3, MODE = " relative " )
 # again after two scroll 57 is 
cursor.scroll (2, MODE = " relative " )
 # in forward roller 2 
cursor.scroll (2, MODE = " relative " )
 # 2. scroll absolute phase operation to the beginning of the first data 
cursor.scroll (0, mode =" Absolute " ) 
cursor.scroll ( 5, the MODE = " Absolute " )
 # performing CRUD, he must submit the data really make changes enabled by default transaction 
conn.commit () 
cursor.close () 
conn.Close ()

 

 

Guess you like

Origin www.cnblogs.com/banbosuiyue/p/11967898.html