CREATETABLE`users` (`id`int(11) NOTNULLAUTO_INCREMENT,`email`varchar(255) COLLATE utf8_bin NOT NULL,`password`varchar(255) COLLATE utf8_bin NOT NULL,PRIMARYKEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_binAUTO_INCREMENT=1 ;
使用
import pymysql.cursors# Connect to the databaseconnection = pymysql.connect(host='localhost', user='user', password='passwd', database='db', cursorclass=pymysql.cursors.DictCursor)with connection:with connection.cursor()as cursor:# Create a new record sql ="INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" cursor.execute(sql, ('webmaster@python.org', 'very-secret'))# connection is not autocommit by default. So you must commit to save# your changes. connection.commit()with connection.cursor()as cursor:# Read a single record sql ="SELECT `id`, `password` FROM `users` WHERE `email`=%s" cursor.execute(sql, ('webmaster@python.org',)) result = cursor.fetchone()print(result)