引入pymysql库
import pymysql
连接数据库:
# 连接数据库 db = pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='root', db='xyx', charset='utf8' ) cursor = db.cursor()
执行sql语句(create 表的语句):
sql = "CREATE TABLE thumb (id INT auto_increment PRIMARY KEY ,thumb CHAR(200) NOT NULL UNIQUE)ENGINE=innodb DEFAULT CHARSET=utf8;"
# 执行SQL语句
cursor.execute(sql)
执行结果:
完整代码:
import pymysql # 连接数据库 db = pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='root', db='xyx', charset='utf8' ) cursor = db.cursor() sql = "CREATE TABLE thumb (id INT auto_increment PRIMARY KEY ,thumb CHAR(200) NOT NULL UNIQUE)ENGINE=innodb DEFAULT CHARSET=utf8;" # 执行SQL语句 cursor.execute(sql) # 关闭光标对象 cursor.close() # 关闭数据库连接 db.close()