{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shelve\n",
    "\n",
    "# 要合并的shelve数据库路径\n",
    "shelve_files = ['D:/contract_data1.dat', 'D:/contract_data2.dat', 'D:/contract_data3.dat']\n",
    "# 合并后的新数据库路径\n",
    "new_shelve_file = 'D:/contract_data3.dat'\n",
    "\n",
    "# 创建一个新的shelve数据库来存储合并后的内容\n",
    "with shelve.open(new_shelve_file, writeback=True) as new_db:\n",
    "    for shelve_file in shelve_files:\n",
    "        try:\n",
    "            with shelve.open(shelve_file) as db:\n",
    "                for key in db:\n",
    "                    if key in new_db:\n",
    "                        print(f\"Warning: Key {key} already exists in the new database. Overwriting.\")\n",
    "                    new_db[key] = db[key]\n",
    "        except Exception as e:\n",
    "            print(f\"Error processing {shelve_file}: {e}\")\n",
    "\n",
    "print(f\"Databases merged into {new_shelve_file}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shelve\n",
    "import os\n",
    "\n",
    "# 要合并的shelve数据库路径\n",
    "shelve_files = [r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview1', r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview2', r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview3']\n",
    "# 合并后的新数据库路径\n",
    "new_shelve_file = r'D:\\bar_overview'\n",
    "\n",
    "# 创建一个新的shelve数据库来存储合并后的内容\n",
    "with shelve.open(new_shelve_file, writeback=True) as new_db:\n",
    "    for shelve_file in shelve_files:\n",
    "        # 检查文件是否存在\n",
    "        if not os.path.exists(shelve_file):\n",
    "            print(f\"错误：文件 {shelve_file} 不存在。\")\n",
    "            continue\n",
    "        try:\n",
    "            # 打开并读取shelve数据库\n",
    "            with shelve.open(shelve_file) as db:\n",
    "                for key in db:\n",
    "                    if key in new_db:\n",
    "                        print(f\"警告：键 {key} 已存在于新数据库中。将覆盖。\")\n",
    "                    new_db[key] = db[key]\n",
    "        except Exception as e:\n",
    "            print(f\"处理文件 {shelve_file} 时出错：{e}\")\n",
    "            if 'db type could not be determined' in str(e):\n",
    "                print(f\"提示：文件 {shelve_file} 可能已损坏或不是一个shelve数据库。\")\n",
    "            continue\n",
    "\n",
    "print(f\"数据库已合并到 {new_shelve_file}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shelve\n",
    "f_shelve = shelve.open(r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview1') # 创建一个文件句柄\n",
    "# 使用for循环打印内容\n",
    "for k,v in f_shelve.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shelve\n",
    "\n",
    "# 打开所有源 shelve 数据库\n",
    "db1 = shelve.open(r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview1')\n",
    "db2 = shelve.open(r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview2')\n",
    "db3 = shelve.open(r'C:\\Users\\Administrator\\.vntrader\\elite_db\\bar_overview3')\n",
    "\n",
    "# 创建一个新的目标 shelve 数据库\n",
    "merged_db = shelve.open(r'D:\\bar_overview')\n",
    "\n",
    "# 将第一个数据库的所有条目添加到新的数据库中\n",
    "for key in db1:\n",
    "    merged_db[key] = db1[key]\n",
    "\n",
    "# 将第二个数据库的所有条目添加到新的数据库中\n",
    "for key in db2:\n",
    "    merged_db[key] = db2[key]\n",
    "\n",
    "# 将第三个数据库的所有条目添加到新的数据库中\n",
    "for key in db3:\n",
    "    merged_db[key] = db3[key]\n",
    "\n",
    "# 关闭所有数据库\n",
    "db1.close()\n",
    "db2.close()\n",
    "db3.close()\n",
    "merged_db.close()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
