一个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3,创建的是FTS4的虚拟表

  1. # -*- coding:utf-8 -*-
  2. import json
  3. import sqlite3
  4. import os, sys
  5.  
  6. def join_key_by_dict(dict_info):
  7. result = ""
  8. for item in dict_info.keys():
  9. result += item + ','
  10. result = result.rstrip(',')
  11. return result
  12.  
  13. def join_value_by_dict(dict_info):
  14. result = ""
  15. for item in dict_info.keys():
  16. if isinstance(dict_info[item], str):
  17. result += "'" + dict_info[item].replace("'", "''") + "',"
  18. else:
  19. result += str(dict_info[item]) + ","
  20. # result += dict_info[item] + ','
  21. result = result.rstrip(',')
  22. return result
  23.  
  24. def create_tbl_by_json(json_file, tbl_name, db_conn):
  25. # 打开文件,
  26. json_handle = open(json_file, "r")
  27. # 读取第一行,转换成json数据
  28. line = json_handle.readline()
  29. json_handle.close()
  30.  
  31. # 清除左边的[,右边的,
  32. line = line.lstrip('[').rstrip('\r\n ,')
  33.  
  34. json_line = json.loads(line)
  35. # 获取到所有key,构建表的创建命令
  36. create_tbl_str = "create virtual table %s USING fts4( %s );" % (tbl_name, join_key_by_dict(json_line))
  37.  
  38. # 打开光标
  39. cur = db_conn.cursor()
  40. cur.execute(create_tbl_str)
  41. db_conn.commit()
  42. cur.close()
  43. print('create %s table success[%s]' % (tbl_name, json_file) )
  44.  
  45. def insert_record_by_json(json_file, tbl_name, db_conn):
  46. # 打开文件,
  47. json_handle = open(json_file, "r")
  48. # 读取第一行,转换成json数据
  49. cur = db_conn.cursor()
  50. count = 0
  51. for line in json_handle:
  52. json_line = json.loads(line.lstrip('[').rstrip('\r\n ,]'))
  53. # 获取到所有key,构建表的创建命令
  54. key_str = join_key_by_dict(json_line)
  55. val_str = join_value_by_dict(json_line)
  56. # 组装命令并执行
  57. insert_record_str = "INSERT INTO %s (%s) VALUES(%s);" % (tbl_name, key_str, val_str)
  58. cur.execute(insert_record_str)
  59. count += 1
  60. db_conn.commit()
  61. cur.close()
  62. json_handle.close()
  63. print('insert record finish, count: %s' % count )
  64.  
  65. def convert_json_to_db(json_file, db_file):
  66. # 检查json_file是否存在
  67. if not os.path.exists(json_file):
  68. print('file not exist: %s' % json_file)
  69. return
  70.  
  71. # 打开数据库连接
  72. db_conn = sqlite3.connect(db_file)
  73.  
  74. tbl_name, _ = os.path.splitext(os.path.basename(json_file))
  75. # 开始创建表
  76. create_tbl_by_json(json_file, tbl_name, db_conn)
  77.  
  78. # 开始插入记录
  79. insert_record_by_json(json_file, tbl_name, db_conn)
  80.  
  81. # 关闭数据库
  82. db_conn.close()
  83. print('Operation done successfully')
  84.  
  85. if __name__ == '__main__':
  86. if len(sys.argv) != 3:
  87. print('Usage: python %s json_file db_file' % os.path.basename(__file__))
  88. exit(1)
  89. json_file = sys.argv[1]
  90. db_file = sys.argv[2]
  91. convert_json_to_db(json_file, db_file)

一个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3,创建的是通用表

  1. # -*- coding:utf-8 -*-
  2. import json
  3. import sqlite3
  4. import os, sys
  5.  
  6. def join_key_by_dict(dict_info):
  7. result = ""
  8. for item in dict_info.keys():
  9. result += item + ','
  10. result = result.rstrip(',')
  11. return result
  12.  
  13. def join_value_by_dict(dict_info):
  14. result = ""
  15. for item in dict_info.keys():
  16. if isinstance(dict_info[item], str):
  17. result += "'" + dict_info[item].replace("'", "''") + "',"
  18. else:
  19. result += str(dict_info[item]) + ","
  20. # result += dict_info[item] + ','
  21. result = result.rstrip(',')
  22. return result
  23.  
  24. def join_keyvalue_by_dict(dict_info):
  25. result = ""
  26. for item in dict_info.keys():
  27. if isinstance(dict_info[item], int):
  28. result += str(item) + " INTEGER,"
  29. else:
  30. result += str(item) + " TEXT,"
  31. # result += dict_info[item] + ','
  32. result = result.rstrip(',')
  33. return result
  34.  
  35. def create_tbl_by_json(json_file, tbl_name, db_conn):
  36. # 打开文件,
  37. json_handle = open(json_file, "r")
  38. # 读取第一行,转换成json数据
  39. line = json_handle.readline()
  40. json_handle.close()
  41.  
  42. # 清除左边的[,右边的,
  43. line = line.lstrip('[').rstrip('\r\n ,')
  44.  
  45. json_line = json.loads(line)
  46. # 获取到所有key,构建表的创建命令
  47. create_tbl_str = "create table %s (%s);" % (tbl_name, join_keyvalue_by_dict(json_line))
  48.  
  49. # 打开光标
  50. cur = db_conn.cursor()
  51. cur.execute(create_tbl_str)
  52. db_conn.commit()
  53. cur.close()
  54. print('create %s table success[%s]' % (tbl_name, json_file) )
  55.  
  56. def insert_record_by_json(json_file, tbl_name, db_conn):
  57. # 打开文件,
  58. json_handle = open(json_file, "r")
  59. # 读取第一行,转换成json数据
  60. cur = db_conn.cursor()
  61. count = 0
  62. for line in json_handle:
  63. json_line = json.loads(line.lstrip('[').rstrip('\r\n ,]'))
  64. # 获取到所有key,构建表的创建命令
  65. key_str = join_key_by_dict(json_line)
  66. val_str = join_value_by_dict(json_line)
  67. # 组装命令并执行
  68. insert_record_str = "INSERT INTO %s (%s) VALUES(%s);" % (tbl_name, key_str, val_str)
  69. cur.execute(insert_record_str)
  70. count += 1
  71. db_conn.commit()
  72. cur.close()
  73. json_handle.close()
  74. print('insert record finish, count: %s' % count )
  75.  
  76. def convert_json_to_db(json_file, db_file):
  77. # 检查json_file是否存在
  78. if not os.path.exists(json_file):
  79. print('file not exist: %s' % json_file)
  80. return
  81.  
  82. # 打开数据库连接
  83. db_conn = sqlite3.connect(db_file)
  84.  
  85. tbl_name, _ = os.path.splitext(os.path.basename(json_file))
  86. # 开始创建表
  87. create_tbl_by_json(json_file, tbl_name, db_conn)
  88.  
  89. # 开始插入记录
  90. insert_record_by_json(json_file, tbl_name, db_conn)
  91.  
  92. # 关闭数据库
  93. db_conn.close()
  94. print('Operation done successfully')
  95.  
  96. if __name__ == '__main__':
  97. if len(sys.argv) != 3:
  98. print('Usage: python %s json_file db_file' % os.path.basename(__file__))
  99. exit(1)
  100. json_file = sys.argv[1]
  101. db_file = sys.argv[2]
  102. convert_json_to_db(json_file, db_file)

  

一个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3,输出的是可以执行的SQL语句

  1. # -*- coding:utf-8 -*-
  2. import json
  3. import os, sys
  4.  
  5. def join_key_by_dict(dict_info):
  6. result = ""
  7. for item in dict_info.keys():
  8. result += item + ','
  9. result = result.rstrip(',')
  10. return result
  11.  
  12. def join_value_by_dict(dict_info):
  13. result = ""
  14. for item in dict_info.keys():
  15. if isinstance(dict_info[item], str):
  16. result += "'" + dict_info[item].replace("'", "''") + "',"
  17. else:
  18. result += str(dict_info[item]) + ","
  19. # result += dict_info[item] + ','
  20. result = result.rstrip(',')
  21. return result
  22.  
  23. def create_tbl_by_json(json_file, tbl_name, sql_file):
  24. # 打开文件,
  25. json_handle = open(json_file, "r")
  26. # 读取第一行,转换成json数据
  27. line = json_handle.readline()
  28. json_handle.close()
  29.  
  30. # 清除左边的[,右边的,
  31. line = line.lstrip('[').rstrip('\r\n ,')
  32.  
  33. json_line = json.loads(line)
  34. # 获取到所有key,构建表的创建命令
  35. create_tbl_str = 'create virtual table %s USING fts4( %s );\n' % (tbl_name, join_key_by_dict(json_line))
  36. sql_file.write(create_tbl_str)
  37.  
  38. # 打开光标
  39. #print('create %s table success[%s]' % (tbl_name, json_file) )
  40.  
  41. def insert_record_by_json(json_file, tbl_name, sql_file):
  42. # 打开文件,
  43. json_handle = open(json_file, "r")
  44. # 读取第一行,转换成json数据
  45. count = 0
  46. for line in json_handle:
  47. json_line = json.loads(line.lstrip('[').rstrip('\r\n ,]'))
  48. # 获取到所有key,构建表的创建命令
  49. key_str = join_key_by_dict(json_line)
  50. val_str = join_value_by_dict(json_line)
  51. # 组装命令并执行
  52. insert_record_str = "INSERT INTO %s (%s) VALUES(%s);\n" % (tbl_name, key_str, val_str)
  53. sql_file.write(insert_record_str)
  54. count += 1
  55. json_handle.close()
  56. #print('insert record finish, count: %s' % count )
  57.  
  58. def convert_json_to_sql(json_file, sql_file):
  59. # 检查json_file是否存在
  60. if not os.path.exists(json_file):
  61. print('file not exist: %s' % json_file)
  62. return
  63.  
  64. # 检查sql_file是否存在
  65. if os.path.exists(sql_file):
  66. print('file is exist: %s, will overwrite it.' % sql_file)
  67.  
  68. # 打开文件
  69. sql_handle = open(sql_file, "w")
  70.  
  71. tbl_name, _ = os.path.splitext(os.path.basename(json_file))
  72. # 开始创建表
  73. create_tbl_by_json(json_file, tbl_name, sql_handle)
  74.  
  75. # 开始插入记录
  76. insert_record_by_json(json_file, tbl_name, sql_handle)
  77.  
  78. # 关闭文件
  79. sql_handle.close()
  80. print('Operation done successfully')
  81.  
  82. if __name__ == '__main__':
  83. if len(sys.argv) != 3:
  84. print('Usage: python %s json_file db_file' % os.path.basename(__file__))
  85. exit(1)
  86. json_file = sys.argv[1]
  87. sql_file = sys.argv[2]
  88. convert_json_to_sql(json_file, sql_file)

  

 
 
 
 
 
 

三个通用的脚本,处理MySQL WorkBench导出表的JSON数据进SQLITE3的更多相关文章

  1. MySQL Workbench 6 不能删除数据等问题(“Error Code: 1175”) 和入门教程

    网络资料收集 当用MySQL Workbench进行数据库的批量更新时,执行一个语句会碰到以下错误提示: Error Code: 1175 You are using safe...without a ...

  2. [笔记] MySql Workbench 导出表结构和数据报错 mysqldump: [ERROR] unknown variable 'delayed-insert=FALSE'

    下午使用MySql Workbench导出数据库表结构,设置完导出选项后执行导出,报如下错误: :: Dumping nacweixindb (tb_app) Running: mysqldump.e ...

  3. 只要三步!阿里云DLA帮你处理海量JSON数据

    概述 您可能有大量应用程序产生的JSON数据,您可能需要对这些JSON数据进行整理,去除不想要的字段,或者只保留想要的字段,或者仅仅是进行数据查询. 那么,利用阿里云Data Lake Analyti ...

  4. 利用PHP脚本辅助MySQL数据库管理5-检查异常数据

    <?php $dbi = new DbMysql; $dbi->dbh = 'mysql://root:mysql@127.0.0.1/coffeetest'; $map = array( ...

  5. mysql workbench 导出表结构

    Server->Data Export 选择数据库(我的是 lhc库) -> 选择对应表(我的是  device表), Dump Structre and Data 导出表数据和表结构 D ...

  6. Mysql 导入导出表结构与数据

    1.导出整个数据库 mysqldump -u用户名 -p密码  数据库名 > 导出的文件名  C:\Users\jack> mysqldump -uroot -pmysql account ...

  7. mysql只导出表结构或数据

    唯一的非导电结构指南数据 mysqldump -t 数据库名称 -uroot -p > xxx.sql 指南结构不仅指导数据 mysqldump    --opt -d  数据库名 -u -p ...

  8. MySQL Workbench的使用教程 (初级入门版)

    MySQL Workbench 是 MySQL AB 最近释放的可视数据库设计工具.这个工具是设计 MySQL 数据库的专用工具. MySQL Workbench 拥有很多的功能和特性:这篇由Djon ...

  9. (转)MySQL Workbench的使用教程 (初级入门版)

    转自:http://www.cnblogs.com/yqskj/archive/2013/03/01/2938027.html MySQL Workbench 是 MySQL AB 最近释放的可视数据 ...

随机推荐

  1. 单例 ------ JAVA实现

    单例:只能实例化一个对象,使用场景比如打印机. 最推荐的是采用饿汉式:双重校验锁用到了大量的语法,不能保证这些语法在所用场合一定没问题,所以不是很推荐:总之简单的才是最好的,就饿汉式!!! C++ 创 ...

  2. 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 ...

  3. UVA 808 Bee Breeding (坐标法)

    https://vjudge.net/problem/UVA-808 #include<cmath> #include<cstdio> #include<algorith ...

  4. 不使用Tomcat,手写简单的web服务

    背景: 公司使用的YDB提供了http的查询数据库服务,直接通过url传入sql语句查询数据-_-||.ydb的使用参照:https://www.cnblogs.com/hd-zg/p/7115112 ...

  5. SpringBoot Caused by: java.lang.NoClassDefFoundError: org/apache/tomcat/util/descriptor/tld/TldParser

    最近尝试着用spring boot ,页面模版使用的jsp,在pom里配置了对jsp的支持: <dependency> <groupId>org.apache.tomcat.e ...

  6. bzoj 4034: [HAOI2015]树上操作——树链剖分

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...

  7. 01背包问题的延伸即变形 (dp)

    对于普通的01背包问题,如果修改限制条件的大小,让数据范围比较大的话,比如相比较重量而言,价值的范围比较小,我们可以试着修改dp的对象,之前的dp针对不同的重量限制计算最大的价值.这次用dp针对不同的 ...

  8. input placeholder 兼容问题

    placeholder是html5出的新特性,ie9以下是不兼容的, 那么为了兼容ie9  我们需要对他做处理 //jq的处理方式$(function(){ jQuery('[placeholder] ...

  9. 3.FireDAC组件快照

    TFDManager 连接定义和Connect连接管理  TFDConnection 数据库连接组件,支持三种连接方式:1.持久定义(有一个唯一名称和一个配置文件,可以由FDManager管理) 例: ...

  10. mybatis-plus的学习

    1.mybatisplus 提供了比较齐全的crud即增删改查,不需要在mapper.xml里写sql可以直接调用 原文链接:http://blog.csdn.net/u014519194/artic ...