Detailed use of pymysql in Python for MySQL additions and deletions to change the operation (synthesis)

Source: Internet
Author: User
This article mainly introduces the use of Pymysql in Python to add and delete MySQL in the operation, through the Pymysql to the database for the search and deletion, has a certain reference value, interested can understand.

This time the pymysql will be used to make a change to the MySQL additions and deletions of all operations, equivalent to the first five times the summary:

Check the database First:

Now write the source code for the increase and deletion check operation, the source code is:

#!/usr/bin/python #coding: GBK import pymysql from builtins import int #将MysqlHelper的几个函数写出来 def conndb ():   #连接数据库 conn=pymysql.connect (host= "localhost", user= "root", passwd= "zx69728537", db= "student");   Cur=conn.cursor ();  return (conn,cur);   def exeupdate (conn,cur,sql): #更新或插入操作 sta=cur.execute (SQL);   Conn.commit ();  return (STA);   def exedelete (conn,cur,ids): #删除操作 sta=0;   For Eachid in Ids.split ("): Sta+=cur.execute (" Delete from students where id=%d "% (int (eachid)));   Conn.commit ();      return (STA);   def exequery (cur,sql): #查找操作 cur.execute (SQL);    return (cur);   def connclose (conn,cur): #关闭连接, releasing Resources cur.close ();  Conn.close (); Result=true; Print ("Please select the above four actions: 1, modify record, 2, add record, 3, query record, 4, delete record.") Press Q for Exit) "); Conn,cur=conndb (); Number=input ();     while (result): if (number== ' Q '): print ("End operation");   Break     elif (int (number) ==1): Sql=input ("Please enter an UPDATE statement:");       Try:exeupdate (conn, cur, SQL);     Print ("Update succeeded"); ExcePT Exception:print ("update failed");   Raise       elif (int (number) ==2): Sql=input ("Please enter new statement:");         Try:exeupdate (conn, cur, SQL);       Print ("new success");         Except Exception:print ("New Failure");   Raise     elif (int (number) ==3): Sql=input ("Please enter query statement:");       Try:cur=exequery (cur, SQL);     For item in Cur:print ("id=" +str (item[0]) + "name=" +item[1]);       Except Exception:print ("Error in Query");   Raise     elif (int (number) ==4): Ids=input ("Please enter an ID, separated by a space");       TRY:EXEDELETE (conn, cur, Ids);     Print ("delete succeeded");       Except Exception:print ("delete failed");   Raise     Else:print ("Illegal input, will end operation!");     Result=false;   Break Print ("Please select the above four actions: 1, modify record, 2, add record, 3, query record, 4, delete record.")   Press Q for Exit) "); Number=input ("Please select operation");

Now run the program to observe:

Now query the database:

You can see a new piece of data in the database and now update the data:

Now we are not querying the database, directly in the program to query,

You can see that the name of the fourth data has been updated to Bingwen, now delete, delete the fourth record,

Now we are querying in the database to see if that data is deleted,

You can see that the delete operation has been successful and now exits the current program.

So, pymysql to MySQL database additions and deletions to the operation was completed.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we have a lot of support

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.