*.sql文件导入数据库通常比较方便。
其他格式如csv、xsl等文件导入比较费劲。
推荐一个针对csv\xsl的导入工具:https://github.com/zhengze/file_to_db.git
如果文件第一行是table的字段名,自动建表+导入数据,实在是省事。
具体用法请看README:
load data from csv/xlsx into database(mysql/postgresql)
pipenv install #或者
pip install -r requirements.txt
DATABASE_URI=postgresql+psycopg2://admin:1234@localhost/test
python file_to_db.py -h
usage: file_to_db.py [-h] [--file FILE] [--table TABLE]optional arguments: -h, --help show this help message and exit --file FILE, -f FILE the file path. --table TABLE, -t TABLE the table name.
示例:
python file_to_db.py -f user -t ./test.csv
test.csv内容如下:
name,agezhangsan,15
联系客服