admin 发布的文章

一个通用的脚本,处理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)

  

 

 

 

 
 
 
 
 
 

CMake的build.make,每次都是cd xxx目录,然后再编译

 

而编译时,GCC会取当前路径保存进调试信息的DT_AT_comp_dir,GCC的编译器选项-fdebug-prefix-map=old=new,允许将路径做一个映射替换,比如将/usr/loca/src/gowork/src/kurento/kms-core/替换成./

同时DT_AT_name编译进的会是相应源代码文件的路径信息,也会受到-fdebug-prefix-map选项的影响。

如上例,最终编译的调试信息中,DT_AT_comp_dir会是./obj-x86_64-linux-gnu/src/gst-plugins/

而DT_AT_name会是./src/gst-plugins/kmscore.c。

GDB调试时,对于DT_AT_name是相对路径,会与DT_AT_comp_dir进行拼装,路径信息会拼装成./obj-x86_64-linux-gnu/src/gst-plugins/./src/gst-plugins/kmscore.c

就会出现源代码找不到的情况,这时候只能强制在编译时去除-fdebug-prefix-map选项,由于CMAKE在新版本的C_FLAGS默认就启用了-fdebug-prefix-map

因此只能在CMakeLists.txt中去除相应选项,类似如下代码,插入到合适位置(正则表达式不完善,各位可以自行调整)

STRING( REGEX REPLACE "\\-fdebug-prefix-map=[a-zA-Z0-9/=.\\-]*" " " CMAKE_C_FLAGS           ${CMAKE_C_FLAGS} )
STRING( REGEX REPLACE "\\-fdebug-prefix-map=[a-zA-Z0-9/=.\\-]*" " " CMAKE_CXX_FLAGS         ${CMAKE_CXX_FLAGS} )
STRING( REGEX REPLACE "\\-fdebug-prefix-map=[a-zA-Z0-9/=.\\-]*" " " CMAKE_C_FLAGS_DEBUG     ${CMAKE_C_FLAGS_DEBUG} )
STRING( REGEX REPLACE "\\-fdebug-prefix-map=[a-zA-Z0-9/=.\\-]*" " " CMAKE_CXX_FLAGS_DEBUG   ${CMAKE_CXX_FLAGS_DEBUG} )
STRING( REGEX REPLACE "\\-fdebug-prefix-map=[a-zA-Z0-9/=.\\-]*" " " CMAKE_C_FLAGS_RELEASE   ${CMAKE_C_FLAGS_RELEASE} )
STRING( REGEX REPLACE "\\-fdebug-prefix-map=[a-zA-Z0-9/=.\\-]*" " " CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} )

  

 

KMS使用CLion作为IDE来调试,打开kms相应模块的目录,CLion自动识别相应的CMakeLists.txt。

然而会make失败,经搜索,看到Clion使用的自带的cmake,因此系统中安装的模块,都是不支持的

直接将/usr/local/clion-2018.3.1/bin/cmake下的linux目录改名备份

然后链接/usr为linux,就可以编译通过了。