三个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3
一个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3,创建的是FTS4的虚拟表
- # -*- coding:utf-8 -*-
- import json
- import sqlite3
- import os, sys
- def join_key_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- result += item + ','
- result = result.rstrip(',')
- return result
- def join_value_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- if isinstance(dict_info[item], str):
- result += "'" + dict_info[item].replace("'", "''") + "',"
- else:
- result += str(dict_info[item]) + ","
- # result += dict_info[item] + ','
- result = result.rstrip(',')
- return result
- def create_tbl_by_json(json_file, tbl_name, db_conn):
- # 打开文件,
- json_handle = open(json_file, "r")
- # 读取第一行,转换成json数据
- line = json_handle.readline()
- json_handle.close()
- # 清除左边的[,右边的,
- line = line.lstrip('[').rstrip('\r\n ,')
- json_line = json.loads(line)
- # 获取到所有key,构建表的创建命令
- create_tbl_str = "create virtual table %s USING fts4( %s );" % (tbl_name, join_key_by_dict(json_line))
- # 打开光标
- cur = db_conn.cursor()
- cur.execute(create_tbl_str)
- db_conn.commit()
- cur.close()
- print('create %s table success[%s]' % (tbl_name, json_file) )
- def insert_record_by_json(json_file, tbl_name, db_conn):
- # 打开文件,
- json_handle = open(json_file, "r")
- # 读取第一行,转换成json数据
- cur = db_conn.cursor()
- count = 0
- for line in json_handle:
- json_line = json.loads(line.lstrip('[').rstrip('\r\n ,]'))
- # 获取到所有key,构建表的创建命令
- key_str = join_key_by_dict(json_line)
- val_str = join_value_by_dict(json_line)
- # 组装命令并执行
- insert_record_str = "INSERT INTO %s (%s) VALUES(%s);" % (tbl_name, key_str, val_str)
- cur.execute(insert_record_str)
- count += 1
- db_conn.commit()
- cur.close()
- json_handle.close()
- print('insert record finish, count: %s' % count )
- def convert_json_to_db(json_file, db_file):
- # 检查json_file是否存在
- if not os.path.exists(json_file):
- print('file not exist: %s' % json_file)
- return
- # 打开数据库连接
- db_conn = sqlite3.connect(db_file)
- tbl_name, _ = os.path.splitext(os.path.basename(json_file))
- # 开始创建表
- create_tbl_by_json(json_file, tbl_name, db_conn)
- # 开始插入记录
- insert_record_by_json(json_file, tbl_name, db_conn)
- # 关闭数据库
- db_conn.close()
- print('Operation done successfully')
- if __name__ == '__main__':
- if len(sys.argv) != 3:
- print('Usage: python %s json_file db_file' % os.path.basename(__file__))
- exit(1)
- json_file = sys.argv[1]
- db_file = sys.argv[2]
- convert_json_to_db(json_file, db_file)
一个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3,创建的是通用表
- # -*- coding:utf-8 -*-
- import json
- import sqlite3
- import os, sys
- def join_key_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- result += item + ','
- result = result.rstrip(',')
- return result
- def join_value_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- if isinstance(dict_info[item], str):
- result += "'" + dict_info[item].replace("'", "''") + "',"
- else:
- result += str(dict_info[item]) + ","
- # result += dict_info[item] + ','
- result = result.rstrip(',')
- return result
- def join_keyvalue_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- if isinstance(dict_info[item], int):
- result += str(item) + " INTEGER,"
- else:
- result += str(item) + " TEXT,"
- # result += dict_info[item] + ','
- result = result.rstrip(',')
- return result
- def create_tbl_by_json(json_file, tbl_name, db_conn):
- # 打开文件,
- json_handle = open(json_file, "r")
- # 读取第一行,转换成json数据
- line = json_handle.readline()
- json_handle.close()
- # 清除左边的[,右边的,
- line = line.lstrip('[').rstrip('\r\n ,')
- json_line = json.loads(line)
- # 获取到所有key,构建表的创建命令
- create_tbl_str = "create table %s (%s);" % (tbl_name, join_keyvalue_by_dict(json_line))
- # 打开光标
- cur = db_conn.cursor()
- cur.execute(create_tbl_str)
- db_conn.commit()
- cur.close()
- print('create %s table success[%s]' % (tbl_name, json_file) )
- def insert_record_by_json(json_file, tbl_name, db_conn):
- # 打开文件,
- json_handle = open(json_file, "r")
- # 读取第一行,转换成json数据
- cur = db_conn.cursor()
- count = 0
- for line in json_handle:
- json_line = json.loads(line.lstrip('[').rstrip('\r\n ,]'))
- # 获取到所有key,构建表的创建命令
- key_str = join_key_by_dict(json_line)
- val_str = join_value_by_dict(json_line)
- # 组装命令并执行
- insert_record_str = "INSERT INTO %s (%s) VALUES(%s);" % (tbl_name, key_str, val_str)
- cur.execute(insert_record_str)
- count += 1
- db_conn.commit()
- cur.close()
- json_handle.close()
- print('insert record finish, count: %s' % count )
- def convert_json_to_db(json_file, db_file):
- # 检查json_file是否存在
- if not os.path.exists(json_file):
- print('file not exist: %s' % json_file)
- return
- # 打开数据库连接
- db_conn = sqlite3.connect(db_file)
- tbl_name, _ = os.path.splitext(os.path.basename(json_file))
- # 开始创建表
- create_tbl_by_json(json_file, tbl_name, db_conn)
- # 开始插入记录
- insert_record_by_json(json_file, tbl_name, db_conn)
- # 关闭数据库
- db_conn.close()
- print('Operation done successfully')
- if __name__ == '__main__':
- if len(sys.argv) != 3:
- print('Usage: python %s json_file db_file' % os.path.basename(__file__))
- exit(1)
- json_file = sys.argv[1]
- db_file = sys.argv[2]
- convert_json_to_db(json_file, db_file)
一个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3,输出的是可以执行的SQL语句
- # -*- coding:utf-8 -*-
- import json
- import os, sys
- def join_key_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- result += item + ','
- result = result.rstrip(',')
- return result
- def join_value_by_dict(dict_info):
- result = ""
- for item in dict_info.keys():
- if isinstance(dict_info[item], str):
- result += "'" + dict_info[item].replace("'", "''") + "',"
- else:
- result += str(dict_info[item]) + ","
- # result += dict_info[item] + ','
- result = result.rstrip(',')
- return result
- def create_tbl_by_json(json_file, tbl_name, sql_file):
- # 打开文件,
- json_handle = open(json_file, "r")
- # 读取第一行,转换成json数据
- line = json_handle.readline()
- json_handle.close()
- # 清除左边的[,右边的,
- line = line.lstrip('[').rstrip('\r\n ,')
- json_line = json.loads(line)
- # 获取到所有key,构建表的创建命令
- create_tbl_str = 'create virtual table %s USING fts4( %s );\n' % (tbl_name, join_key_by_dict(json_line))
- sql_file.write(create_tbl_str)
- # 打开光标
- #print('create %s table success[%s]' % (tbl_name, json_file) )
- def insert_record_by_json(json_file, tbl_name, sql_file):
- # 打开文件,
- json_handle = open(json_file, "r")
- # 读取第一行,转换成json数据
- count = 0
- for line in json_handle:
- json_line = json.loads(line.lstrip('[').rstrip('\r\n ,]'))
- # 获取到所有key,构建表的创建命令
- key_str = join_key_by_dict(json_line)
- val_str = join_value_by_dict(json_line)
- # 组装命令并执行
- insert_record_str = "INSERT INTO %s (%s) VALUES(%s);\n" % (tbl_name, key_str, val_str)
- sql_file.write(insert_record_str)
- count += 1
- json_handle.close()
- #print('insert record finish, count: %s' % count )
- def convert_json_to_sql(json_file, sql_file):
- # 检查json_file是否存在
- if not os.path.exists(json_file):
- print('file not exist: %s' % json_file)
- return
- # 检查sql_file是否存在
- if os.path.exists(sql_file):
- print('file is exist: %s, will overwrite it.' % sql_file)
- # 打开文件
- sql_handle = open(sql_file, "w")
- tbl_name, _ = os.path.splitext(os.path.basename(json_file))
- # 开始创建表
- create_tbl_by_json(json_file, tbl_name, sql_handle)
- # 开始插入记录
- insert_record_by_json(json_file, tbl_name, sql_handle)
- # 关闭文件
- sql_handle.close()
- print('Operation done successfully')
- if __name__ == '__main__':
- if len(sys.argv) != 3:
- print('Usage: python %s json_file db_file' % os.path.basename(__file__))
- exit(1)
- json_file = sys.argv[1]
- sql_file = sys.argv[2]
- convert_json_to_sql(json_file, sql_file)
三个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3的更多相关文章
- MySQL Workbench 6 不能删除数据等问题(“Error Code: 1175”) 和入门教程
网络资料收集 当用MySQL Workbench进行数据库的批量更新时,执行一个语句会碰到以下错误提示: Error Code: 1175 You are using safe...without a ...
- [笔记] MySql Workbench 导出表结构和数据报错 mysqldump: [ERROR] unknown variable 'delayed-insert=FALSE'
下午使用MySql Workbench导出数据库表结构,设置完导出选项后执行导出,报如下错误: :: Dumping nacweixindb (tb_app) Running: mysqldump.e ...
- 只要三步!阿里云DLA帮你处理海量JSON数据
概述 您可能有大量应用程序产生的JSON数据,您可能需要对这些JSON数据进行整理,去除不想要的字段,或者只保留想要的字段,或者仅仅是进行数据查询. 那么,利用阿里云Data Lake Analyti ...
- 利用PHP脚本辅助MySQL数据库管理5-检查异常数据
<?php $dbi = new DbMysql; $dbi->dbh = 'mysql://root:mysql@127.0.0.1/coffeetest'; $map = array( ...
- mysql workbench 导出表结构
Server->Data Export 选择数据库(我的是 lhc库) -> 选择对应表(我的是 device表), Dump Structre and Data 导出表数据和表结构 D ...
- Mysql 导入导出表结构与数据
1.导出整个数据库 mysqldump -u用户名 -p密码 数据库名 > 导出的文件名 C:\Users\jack> mysqldump -uroot -pmysql account ...
- mysql只导出表结构或数据
唯一的非导电结构指南数据 mysqldump -t 数据库名称 -uroot -p > xxx.sql 指南结构不仅指导数据 mysqldump --opt -d 数据库名 -u -p ...
- MySQL Workbench的使用教程 (初级入门版)
MySQL Workbench 是 MySQL AB 最近释放的可视数据库设计工具.这个工具是设计 MySQL 数据库的专用工具. MySQL Workbench 拥有很多的功能和特性:这篇由Djon ...
- (转)MySQL Workbench的使用教程 (初级入门版)
转自:http://www.cnblogs.com/yqskj/archive/2013/03/01/2938027.html MySQL Workbench 是 MySQL AB 最近释放的可视数据 ...
随机推荐
- 单例 ------ JAVA实现
单例:只能实例化一个对象,使用场景比如打印机. 最推荐的是采用饿汉式:双重校验锁用到了大量的语法,不能保证这些语法在所用场合一定没问题,所以不是很推荐:总之简单的才是最好的,就饿汉式!!! C++ 创 ...
- ICPC2017 Urumqi - K - Sum of the Line
题目描述 Consider a triangle of integers, denoted by T. The value at (r, c) is denoted by Tr,c , where 1 ...
- UVA 808 Bee Breeding (坐标法)
https://vjudge.net/problem/UVA-808 #include<cmath> #include<cstdio> #include<algorith ...
- 不使用Tomcat,手写简单的web服务
背景: 公司使用的YDB提供了http的查询数据库服务,直接通过url传入sql语句查询数据-_-||.ydb的使用参照:https://www.cnblogs.com/hd-zg/p/7115112 ...
- SpringBoot Caused by: java.lang.NoClassDefFoundError: org/apache/tomcat/util/descriptor/tld/TldParser
最近尝试着用spring boot ,页面模版使用的jsp,在pom里配置了对jsp的支持: <dependency> <groupId>org.apache.tomcat.e ...
- bzoj 4034: [HAOI2015]树上操作——树链剖分
Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...
- 01背包问题的延伸即变形 (dp)
对于普通的01背包问题,如果修改限制条件的大小,让数据范围比较大的话,比如相比较重量而言,价值的范围比较小,我们可以试着修改dp的对象,之前的dp针对不同的重量限制计算最大的价值.这次用dp针对不同的 ...
- input placeholder 兼容问题
placeholder是html5出的新特性,ie9以下是不兼容的, 那么为了兼容ie9 我们需要对他做处理 //jq的处理方式$(function(){ jQuery('[placeholder] ...
- 3.FireDAC组件快照
TFDManager 连接定义和Connect连接管理 TFDConnection 数据库连接组件,支持三种连接方式:1.持久定义(有一个唯一名称和一个配置文件,可以由FDManager管理) 例: ...
- mybatis-plus的学习
1.mybatisplus 提供了比较齐全的crud即增删改查,不需要在mapper.xml里写sql可以直接调用 原文链接:http://blog.csdn.net/u014519194/artic ...