1. 增加数据
import pymysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "insert into admin(username,password,mobile) value (%s, %s, %s)"
cursor.execute(sql, [username, password, mobile])
conn.commit()
cursor.close()
conn.close()
2. 查询数据
import pymysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select * from admin")
v1 = cursor.fetchone()
v2 = cursor.fetchall()
cursor.close()
conn.close()
3. 删除数据
import pymysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("delete from admin where id = 4")
conn.commit()
cursor.close()
conn.close()
4. 修改数据
import pymysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("update admin set mobile=%s where id=%s", ["18888888888", 5, ])
conn.commit()
cursor.close()
conn.close()