python with pymysql operation module database MySQL, additions and deletions to achieve check

Here write four main functions, additions and deletions to check

The following is the style of the target database, the database: testpy, the data table: movie1, (misspelled not changed, Zhu), there are four fields, id is the primary key.

 

 

 Charles, the following example to add a function name is ready to run

 1 #查select
 2 def select():
 3     #创建connection连接
 4     conn = pymysql.connect(
 5         host = 'localhost',
 6         port = 3306,
 7         user = 'root',
 8         password = 'mysql_123456',
 9         database = 'testpy',
10         charset = 'utf8'
11     )
 12      # obtain cursor object
 13 is      cursor = conn.cursor ()
 14      #sql statement
 15      SQL = " SELECT daoyan, dizhi, mingzi from MOVIE1 WHERE daoyan = 'NA'; " 
16      # execute the statement
 . 17      the cursor.execute (SQL)
 18 is      # Get query data
 . 19      #RET = cursor.fetchone ()
 20 is      #RET cursor.fetchmany = ( . 3 )
 21 is      RET = cursor.fetchall ()
 22 is      # close the object, connection
 23 is      cursor.close ()
 24      conn.Close ()
 25      # print the results
 26     for i in ret:
27         print(i)

increase

 1 #增:insert
 2 def insert():
 3     #创建connection连接
 4     conn = pymysql.connect(
 5         host = 'localhost',
 6         port = 3306,
 7         user = 'root',
 8         password = 'mysql_123456',
 9         database = 'testpy',
10         charset = 'utf8'
11     )
12     # Obtain a cursor object
 13      cursor = conn.cursor ()
 14      #sql statement
 15      SQL = " INSERT INTO MOVIE1 values ( '0', 'Luke', 'HTTP: //www.luke.com', 'your sister') ; " 
16      # execute the statement
 . 17      the cursor.execute (SQL)
 18 is      # commit the transaction
 . 19      conn.commit ()
 20 is      last_id = cursor.lastrowid
 21 is      # id number returned insert
 22 is      Print (last_id)
 23 is      # close the object, connection
 24      Cursor. use Close ()
 25      conn.Close ()

delete

 1 #删:delete
 2 def delete():
 3     #创建connection连接
 4     conn = pymysql.connect(
 5         host = 'localhost',
 6         port = 3306,
 7         user = 'root',
 8         password = 'mysql_123456',
 9         database = 'testpy',
10         charset = 'utf8'
11     )
12     # Obtain cursor object
 13 is      cursor = conn.cursor ()
 14      #sql statement
 15      SQL = " Delete from MOVIE1 WHERE ID = 149; " 
16      # execute the statement
 . 17      the cursor.execute (SQL)
 18 is      # commit the transaction
 . 19      conn.commit ()
 20      # close objects, the connection
 21 is      cursor.close ()
 22 is      conn.Close ()

change

 1 #改:update
 2 def update():
 3     #创建connection连接
 4     conn = pymysql.connect(
 5         host = 'localhost',
 6         port = 3306,
 7         user = 'root',
 8         password = 'mysql_123456',
 9         database = 'testpy',
10         charset = 'utf8'
11     )
12     # Obtain cursor object
 13 is      cursor = conn.cursor ()
 14      #sql statement
 15      SQL = " Update MOVIE1 SET daoyan = 'Luke' WHERE ID = 2; " 
16      # execute the statement
 . 17      the cursor.execute (SQL)
 18 is      # commit the transaction
 19      conn.commit ()
 20 is      # close the object, connection
 21 is      cursor.close ()
 22 is      conn.Close ()

The above four functions written in a py in, you can call the function, it uses its own backup.

Guess you like

Origin www.cnblogs.com/passagain/p/11491201.html