12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- @Author : liuyuqi
- @Contact : liuyuqi.gov@msn.cn
- @Time : 2019/07/23 10:58:48
- @Version : 1.0
- @License : (C)Copyright 2019
- @Desc : 设置 数据库参数
- '''
- import configparser
- import os
- import pymysql
- src = "C:/Users/liuyuqi/Desktop/crawl-bilibili"
- os.chdir(src)
- conf_dir = "conf/"
- config_file = os.path.join(conf_dir, "mysql.conf")
- section_name = "db1"
- def writeConf(user, pwd, database, host="localhost", port=3306, charset="utf8"):
- cf = configparser.RawConfigParser()
- cf.add_section('db1')
- cf.set('db1', 'host', host)
- cf.set('db1', 'port', port)
- cf.set('db1', 'user', user)
- cf.set('db1', 'pwd', pwd)
- cf.set("db1", "database", database)
- cf.set("db1", "charset", charset)
- cf.add_section("project")
- cf.set("project", "workspace", "C:/Users/liuyuqi/Desktop/crawl-bilibili")
- with open(config_file, 'w') as configfile:
- cf.write(configfile)
- def readConf(section, key):
- config = configparser.RawConfigParser()
- config.read(config_file)
- port = config.get(section, key)
- print(port)
- def readSQL(path):
- with open(path, "r", encoding="utf-8") as f:
- sql = ""
- for line in f.readlines():
- if not line or line == "\n":
- continue
- sql = sql+line
- return sql
- def getDBServer():
- pass
- def getWorkSpace():
- return readConf("project", "workspace")
- def initDB1():
- '''
- 导入数据,pymysql实在垃圾,只能一条一条执行。无法执行sql文件,也就是SQL文件手动读取为一条条在执行。。
- '''
- conn = pymysql.connect("localhost", "lyq", "123456", "bilibili")
- cursor = conn.cursor()
- # 如果没有数据库,则创建一个
- # cursor.execute("CREATE DATABASE bilibili;")
- userSQL = os.path.join(conf_dir, "user.sql")
- sql = readSQL(userSQL)
- cursor.execute(sql)
- videoSQL = os.path.join(conf_dir, "video.sql")
- sql = readSQL(videoSQL)
- cursor.execute(sql)
- def initDB():
- '''
- 导入数据,采用shell命令执行
- '''
- userSQL = os.path.join(conf_dir, "user.sql")
- videoSQL = os.path.join(conf_dir, "video.sql")
- os.system(
- "D:/Program-Files/MySQL/mysql-5.7.17-winx64/bin/mysql.exe -uroot -p123456 --default-character-set=utf8 bilibili < "+userSQL)
- os.system(
- "D:/Program-Files/MySQL/mysql-5.7.17-winx64/bin/mysql.exe -uroot -p123456 --default-character-set=utf8 bilibili < "+videoSQL)
- if __name__ == '__main__':
- writeConf("root", "123456", "bilibili", host="h5.yoqi.me")
- readConf("db1", "host")
- # initDB()
|