目录
创建数据库
1.下载pymysql
pip install pymysql
2.导入库
import pymysql
3.连接数据库
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = 'root'
DBNAME = 'dbtest'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
except pymysql.Error as e:
print('数据库连接失败'+str(e))
创建一张新表
1.声明一个游标
cur = db.cursor()
2.创建表之前先检查是否存在
cur.execute('DROP TABLE IF EXISTS Student')
3.编辑sql语句
sqlQuery = "CREATE TABLE Student(Name CHAR(20) NOT NULL ,Email CHAR(20),Age int )"
删除表中数据
1.编辑sql语句
sqlQuery = "DELETE FROM Student where Name=%s"
2.编辑更新的信息
value = ('John')