conn = pymysql.connect(host='127.0.0.1',port = 3306,user='user',passwd='passwd',db ='db_name',charset="utf8")
# createdatabase.py#!/usr/bin/env python
import os
import re
import sys
import pymysql
from importlib import import_module
# search the dirname of settings.py and import it
with open('manage.py') as f:
s = f.read()
d = re.search(r'DJANGO_SETTINGS_MODULE.*?,\s*"(.+?)\.settings', s).group(1)
assert 'settings.py' in os.listdir(d)
mo = import_module('{d}.localsettings'.format(d=d))
def getconf(alias='default'):
dbconf = mo.DATABASES.get(alias)
config = {'host': dbconf.get('HOST'),
'user': dbconf.get('USER'),
'passwd': dbconf.get('PASSWORD'),
'port': dbconf.get('PORT'),
'charset': 'utf8', }
config = {k: v for k, v in config.items() if v is not None}
db_name = dbconf.get('NAME')
return config, db_name
def creat_db(config, db_name):
try:
conn = pymysql.connect(**config)
cur = conn.cursor()
if '-d' in sys.argv:
cur.execute('drop database {}'.format(db_name))
print('success to execute `drop database {}`'.format(db_name))
command = 'create database {} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'.format(db_name)
cur.execute(command)
print('success to execute `{}`'.format(command))
# conn.select_db(database)
conn.commit()
cur.close()
conn.close()
except Exception as e:
print("SQL Error: {e}".format(e=e))
def main():
creat_db(*getconf())
# creat_db(*getconf('mysql_property'))
估计是密码错了,你直接在命令行运行:
mysql -u root -p root看看密码是不是root
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)