如何将脚本中一些变量放到配置文件中,从配置文件读取并解析?

比如ODBC连接需要的连接字符串,放到配置文件中,便于维护。

connDict = dict(`ip`port`db`uid`pwd`driver, [`192.168.xxx.xxx, `xxxxx, `xxxx, `user, "passwd", `MySQL])
odbcconn = odbc::connect("Driver={" + connDict.driver + "};Server=" + connDict.ip + ";Port=" + connDict.port + ";Database=" + connDict.db + ";Uid=" + connDict.uid + ";Pwd=" + connDict.pwd + ";")
请先 登录 后评论

1 个回答

Juntao Wang

执行下面脚本,可以将字典转换为符合DolphinDB 规范的JSON 格式数据,放到一个JSON文件中。

connDict = dict(`ip`port`db`uid`pwd`driver, [`192.168.xxx.xxx, `xxxxx, `xxxx, `user, "passwd", `MySQL])
toJson(connDict)

自定义函数,读取JSON文件并转化为字典。

def getMysqlConnDictFromJson(filePath) {
	f = file(filePath)
	arr = f.readLines()
	json = reduce(concat, trim(arr[arr != string(NULL)]))
	return fromJson(json)
}

connDict = getMysqlConnDictFromJson("/data/software/dolphin/server/mysqlconn.json")



请先 登录 后评论