osc/research/cmip6/db_utils/db_create.py
2025-05-28 19:16:17 +08:00

94 lines
2.2 KiB
Python

import pymysql
kv = {
'id': 118,
'version': 8,
'access': 53,
'activity_drs': 15,
'activity_id': 15,
'cf_standard_name': 53,
'citation_url': 147,
'data_node': 25,
'data_specs_version': 12,
'dataset_id_template_': 141,
'datetime_start': -3,
'datetime_stop': -3,
'directory_format_template_': 162,
'experiment_id': 14,
'experiment_title': 45,
'frequency': 7,
'further_info_url': 103,
'geo_units': 17,
'grid': 164,
'grid_label': 8,
'index_node': 22,
'instance_id': 98,
'institution_id': 23,
'latest': 0,
'master_id': 88,
'member_id': 15,
'mip_era': 9,
'model_cohort': 14,
'nominal_resolution': 14,
'north_degrees': -2,
'number_of_aggregations': -1,
'number_of_files': -1,
'pid': 53,
'product': 16,
'project': 9,
'realm': 10,
'replica': 0,
'size': -1,
'source_id': 21,
'source_type': 47,
'south_degrees': -2,
'sub_experiment_id': 8,
'table_id': 9,
'title': 88,
'type': 7,
'url': 473,
'variable': 15,
'variable_id': 15,
'variable_long_name': 58,
'variable_units': 14,
'variant_label': 15,
'xlink': 248,
'_version_': -1,
'retracted': 0,
'_timestamp': -3,
'score': -2,
'east_degrees': -2,
'geo': 102,
'height_bottom': -2,
'height_top': -2,
'height_units': 11,
'west_degrees': -2,
'branch_method': 12,
'creation_date': 20,
'short_description': 42,
'datetime_end': 20,
'metadata_format': 7
}
if __name__ == "__main__":
db = pymysql.connect(host='39.101.194.63', port=23306,
user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
cursor = db.cursor()
db_keys = []
for key in kv:
_length = kv[key]
_type = 'varchar({})'.format(_length)
if _length == -3:
_type = 'datetime'
if _length == -2:
_type = 'float'
if _length == -1:
_type = 'int'
if _length == 0:
_type = 'tinyint'
db_keys.append("{} {} null".format(key, _type))
SQL_CREATE = 'create table jd_data.cmip6_search_result ({});'.format(", ".join(db_keys))
cursor.execute(SQL_CREATE)
db.commit()
db.close()