Create data table
import pymysql

db = pymysql.connect("localhost","root","","hank")

cursor = db.cursor()

cursor.execute("DROP TABLE IF EXISTS leo")

sql = """CREATE TABLE leo (
            FIRST_NAME CHAR(20) NOT NULL,
            LAST_NAME CHAR(20),
            AGE INT,
            SEX CHAR(1),
            INCOME FLOAT)"""

cursor.execute(sql)

db.close()

Add data

import pymysql

db = pymysql.connect("localhost","root","","hank")

cursor = db.cursor()

sql = """INSERT INTO leo(
         FIRST_NAME,LAST_NAME,AGE,SEX,INCOME)
         VALUES("JOE","B",20,"F",4000) """

try:
    #Execute sql statements
    cursor.execute(sql)
    #Submit to database    
    db.commit()

except:
    db.rollback()

db.close()

mysql> select * from leo; +------------+-----------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+-----------+------+------+--------+ | Mike | A | 30 | M | 6000 | | JOE | B | 20 | F | 4000 | +------------+-----------+------+------+--------+ 2 rows in set (0.00 sec)


Inquire

import pymysql

db = pymysql.connect("localhost","root","","hank")

cursor = db.cursor()

sql = "SELECT * FROM leo "

try: 
    cursor.execute(sql)
    results = cursor.fetchall()
    for row in results:
        fname = row[0]
        lname = row[1]
        age = row[2]
        sex = row[3]
        income = row[4]
        print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" %  (fname,lname,age,sex,income))

except:
    print("Error:unable to fetch data")

fname=Mike,lname=A,age=30,sex=M,income=6000 fname=JOE,lname=B,age=20,sex=F,income=4000


database changes

import pymysql

db = pymysql.connect("localhost","root","","hank")

cursor = db.cursor()

sql = "UPDATE leo SET AGE = AGE + 1 WHERE SEX = "%c"" % ("M")  #Find the sex gender M and update its age + 1

try:
    cursor.execute(sql)
    db.commit()
except:
    db.rollback()

db.close()

mysql> select * from leo; +------------+-----------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+-----------+------+------+--------+ | Mike | A | 31 | M | 6000 | | JOE | B | 20 | F | 4000 | +------------+-----------+------+------+--------+ 2 rows in set (0.00 sec)


delete operation

import pymysql

db = pymysql.connect("localhost","root","","hank")

cursor = db.cursor()

sql = "delete from leo where name = hankleo"

try:
    cursor.execute(sql)
    db.commit()
except:
    db.rollback()

db.close()

Related articles

MYSQL Slow Query Logs

When you are a developer, happy coding every day at work. Suddenly, one day DBA came to the door and said that he sent you or your department manager a lot of sql, and you need to urgently deal with these slow queries that are about to hang the system

mysql binlog transaction logs

binlog is binary log, binary log file, this file records all mysql dml operations. Through the binlog log, we can do data recovery, master-resident replication and master-slave replication, etc. Developers may not pay much attention to binlog

nacos Deployment

1. Download the latest nacos installation package from https://github.com/alibaba/nacos, I downloaded nacos-server-1.3.1.tar.gz 2. Move to the Linux installation directory and use the command "tar -zxvf nacos-server-1.3.1.tar.gz" to decompress t