You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pymysql
class MysqlConfig:
user = 'root' # MySQL用户名
password = '123456' # MySQL密码
db = None
cursor = None
@staticmethod
def create_mysql():
try:
db = pymysql.connect(host="localhost", user=MysqlConfig.user, password=MysqlConfig.password,
database=None) # 数据库链接,不指明具体的数据库
cursor = db.cursor() # 获得游标对象
sql_1 = "CREATE DATABASE IF NOT EXISTS python_test;" # 创建名为python_test的数据库
sql_2 = "CREATE TABLE IF NOT EXISTS t_student( sid bigint(20) PRIMARY KEY AUTO_INCREMENT ," \
" sno varchar(20) , sname varchar(20), " \
"sex varchar(20), age int, phone varchar(20), dormNo varchar(20)); "
sql_3 = "CREATE TABLE IF NOT EXISTS t_user( uid bigint(20) PRIMARY KEY AUTO_INCREMENT ," \
" user_name varchar(20) , user_password varchar(20),UNIQUE INDEX (user_name));"
cursor.execute(sql_1) # 执行语句
MysqlConfig.db = pymysql.connect(host="localhost", user=MysqlConfig.user, password=MysqlConfig.password,
database="python_test") # 数据库链接指定名为python_test的数据库
MysqlConfig.cursor = MysqlConfig.db.cursor() # 使用 cursor() 方法创建一个游标对象 cursor
MysqlConfig.cursor.execute(sql_2) # 执行语句
MysqlConfig.cursor.execute(sql_3)
except Exception as ex:
print("出现异常:%s" % ex)