def init_database(host, user, passwd, dbname):
warnings.filterwarnings('ignore', message = "Table.*already exists")
warnings.filterwarnings('ignore', message = "Can't create.*database exists")
#???if not exists???? = =
db = MySQLdb.connect(host, user, passwd)
tx = db.cursor()
tx.execute('set names utf8mb4')
tx.execute('create database if not exists `%s`default charset utf8mb4\
default collate utf8mb4_general_ci;' % MySQLdb.escape_string(dbname))
#?????????
#???MySQLdb???????? ??????
db.select_db(dbname)
tx.execute("create table if not exists thread(\
id BIGINT(12), title VARCHAR(100), author VARCHAR(30), reply_num INT(4),\
good BOOL, PRIMARY KEY (id)) CHARSET=utf8mb4;")
tx.execute("create table if not exists post(\
id BIGINT(12), floor INT(4), author VARCHAR(30), content TEXT,\
time DATETIME, comment_num INT(4), thread_id BIGINT(12),PRIMARY KEY (id),\
FOREIGN KEY (thread_id) REFERENCES thread(id)) CHARSET=utf8mb4;")
tx.execute("create table if not exists comment(id BIGINT(12),\
author VARCHAR(30), content TEXT, time DATETIME, post_id BIGINT(12),\
PRIMARY KEY (id), FOREIGN KEY (post_id) REFERENCES post(id)) CHARSET=utf8mb4;")
db.commit()
db.close()
warnings.resetwarnings()
warnings.filterwarnings('ignore', message = ".*looks like a ")
# bs.get_text???url?????????
评论列表
文章目录