1"use strict";(self.webpackChunknonebot=self.webpackChunknonebot||[]).push([["313"],{95809(e,n,l){l.r(n),l.d(n,{metadata:()=>r,default:()=>p,frontMatter:()=>o,contentTitle:()=>c,toc:()=>u,assets:()=>d});var r=JSON.parse('{"id":"best-practice/database/README","title":"\u6570\u636E\u5E93","description":"nonebot-plugin-orm \u662F NoneBot \u7684\u6570\u636E\u5E93\u652F\u6301\u63D2\u4EF6\u3002","source":"@site/versioned_docs/version-2.5.0/best-practice/database/README.mdx","sourceDirName":"best-practice/database","slug":"/best-practice/database/","permalink":"/docs/best-practice/database/","draft":false,"unlisted":false,"editUrl":"https://github.com/nonebot/nonebot2/edit/master/website/versioned_docs/version-2.5.0/best-practice/database/README.mdx","tags":[],"version":"2.5.0","lastUpdatedBy":"noneflow[bot]","lastUpdatedAt":1775014365000,"frontMatter":{},"sidebar":"tutorial","previous":{"title":"\u5185\u7F6E\u7EC4\u4EF6","permalink":"/docs/best-practice/alconna/builtins"},"next":{"title":"\u7528\u6237\u6307\u5357","permalink":"/docs/best-practice/database/user"}}
1'),a=l(62540),s=l(43023),t=l(84993),i=l(36031);let o={},c="\u6570\u636E\u5E93",d={},u=[{value:"\u5B89\u88C5",id:"\u5B89\u88C5",level:2},{value:"\u6570\u636E\u5E93\u9A71\u52A8\u548C\u540E\u7AEF",id:"\u6570\u636E\u5E93\u9A71\u52A8\u548C\u540E\u7AEF",level:2},{value:"SQLite",id:"sqlite",level:3},{value:"PostgreSQL",id:"postgresql",level:3},{value:"MySQL / MariaDB",id:"mysql--mariadb",level:3},{value:"\u4F7F\u7528",id:"\u4F7F\u7528",level:2}];function h(e){let n={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,s.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(n.header,{children:(0,a.jsx)(n.h1,{id:"\u6570\u636E\u5E93",children:"\u6570\u636E\u5E93"})}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.a,{href:"https://github.com/nonebot/plugin-orm",children:(0,a.jsx)(n.code,{children:"nonebot-plugin-orm"})})," \u662F NoneBot \u7684\u6570\u636E\u5E93\u652F\u6301\u63D2\u4EF6\u3002\n\u672C\u63D2\u4EF6\u57FA\u4E8E ",(0,a.jsx)(n.a,{href:"https://www.sqlalchemy.org/",children:"SQLAlchemy"})," \u548C ",(0,a.jsx)(n.a,{href:"https://alembic.sqlalchemy.org/",children:"Alembic"}),"\uFF0C\u63D0\u4F9B\u4E86\u8BB8\u591A\u4E0E NoneBot \u7D27\u5BC6\u96C6\u6210\u7684\u529F\u80FD\uFF1A"]}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsx)(n.li,{children:"\u591A Engine / Connection \u652F\u6301"}),"\n",(0,a.jsx)(n.li,{children:"Session \u7BA1\u7406"}),"\n",(0,a.jsx)(n.li,{children:"\u5173\u7CFB\u6A21\u578B\u7BA1\u7406\u3001\u4F9D\u8D56\u6CE8\u5165\u652F\u6301"}),"\n",(0,a.jsx)(n.li,{children:"\u6570\u636E\u5E93\u8FC1\u79FB"}),"\n"]}),"\n",(0,a.jsx)(n.h2,{id:"\u5B89\u88C5",children:"\u5B89\u88C5"}),"\n",(0,a.jsxs)(i.A,{groupId:"install",children:[(0,a.jsx)(t.A,{value:"cli",label:"\u4F7F\u7528 nb-cli",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"nb plugin install nonebot-plugin-orm\n"})})}),(0,a.jsx)(t.A,{value:"pip",label:"\u4F7F\u7528 pip",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"pip install nonebot-plugin-orm\n"})})}),(0,a.jsx)(t.A,{value:"pdm",label:"\u4F7F\u7528 pdm",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"pdm add nonebot-plugin-orm\n"})})})]}),"\n",(0,a.jsx)(n.h2,{id:"\u6570\u636E\u5E93\u9A71\u52A8\u548C\u540E\u7AEF",children:"\u6570\u636E\u5E93\u9A71\u52A8\u548C\u540E\u7AEF"}),"\n",(0,a.jsx)(n.p,{children:"\u672C\u63D2\u4EF6\u53EA\u63D0\u4F9B\u4E86 ORM \u529F\u80FD\uFF0C\u6CA1\u6709\u6570\u636E\u5E93\u540E\u7AEF\uFF0C\u4E5F\u6CA1\u6709\u76F4\u63A5\u8FDE\u63A5\u6570\u636E\u5E93\u540E\u7AEF\u7684\u80FD\u529B\u3002\n\u6240\u4EE5\u4F60\u9700\u8981\u53E6\u884C\u5B89\u88C5\u6570\u636E\u5E93\u9A71\u52A8\u548C\u6570\u636E\u5E93\u540E\u7AEF\uFF0C\u5E76\u4E14\u914D\u7F6E\u6570\u636E\u5E93\u8FDE\u63A5\u4FE1\u606F\u3002"}),"\n",(0,a.jsx)(n.h3,{id:"sqlite",children:"SQLite"}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.a,{href:"https://www.sqlite.org/",children:"SQLite"})," \u662F\u4E00\u4E2A\u8F7B\u91CF\u7EA7\u7684\u5D4C\u5165\u5F0F\u6570\u636E\u5E93\uFF0C\u5B83\u7684\u6570\u636E\u4EE5\u5355\u6587\u4EF6\u7684\u5F62\u5F0F\u5B58\u50A8\u5728\u672C\u5730\uFF0C\u4E0D\u9700\u8981\u5355\u72EC\u7684\u6570\u636E\u5E93\u540E\u7AEF\u3002\nSQLite \u975E\u5E38\u9002\u5408\u7528\u4E8E\u5F00\u53D1\u73AF\u5883\u548C\u5C0F\u578B\u5E94\u7528\uFF0C\u4F46\u662F\u4E0D\u9002\u5408\u7528\u4E8E\u5927\u578B\u5E94\u7528\u7684\u751F\u4EA7\u73AF\u5883\u3002"]}),"\n",(0,a.jsx)(n.p,{children:"\u867D\u7136\u4E0D\u9700\u8981\u53E6\u884C\u5B89\u88C5\u6570\u636E\u5E93\u540E\u7AEF\uFF0C\u4F46\u4F60\u4ECD\u7136\u9700\u8981\u5B89\u88C5\u6570\u636E\u5E93\u9A71\u52A8\uFF1A"}),"\n",(0,a.jsxs)(i.A,{groupId:"install",children:[(0,a.jsx)(t.A,{value:"pip",label:"\u4F7F\u7528 pip",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:'pip install "nonebot-plugin-orm[sqlite]"\n'})})}),(0,a.jsx)(t.A,{value:"pdm",label:"\u4F7F\u7528 pdm",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:'pdm add "nonebot-plugin-orm[sqlite]"\n'})})})]}),"\n",(0,a.jsxs)(n.p,{children:["\u9ED8\u8BA4\u60C5\u51B5\u4E0B\uFF0C\u6570\u636E\u5E93\u6587\u4EF6\u4E3A ",(0,a.jsx)(n.code,{children:"<data path>/nonebot-plugin-orm/db.sqlite3"}),"\uFF08\u6570\u636E\u76EE\u5F55\u7531 ",(0,a.jsx)(n.a,{href:"../data-storing",children:"nonebot-plugin-localstore"})," \u63D0\u4F9B\uFF09\u3002\n\u6216\u8005\uFF0C\u4F60\u53EF\u4EE5\u901A\u8FC7\u914D\u7F6E ",(0,a.jsx)(n.code,{children:"SQLALCHEMY_DATABASE_URL"})," \u6765\u6307\u5B9A\u6570\u636E\u5E93\u6587\u4EF6\u8DEF\u5F84\uFF1A"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"SQLALCHEMY_DATABASE_URL=sqlite+aiosqlite:///file_path\n"})}),"\n",(0,a.jsx)(n.h3,{id:"postgresql",children:"PostgreSQL"}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.a,{href:"https://www.postgresql.org/",children:"PostgreSQL"}
1)," \u662F\u4E16\u754C\u4E0A\u6700\u5148\u8FDB\u7684\u5F00\u6E90\u5173\u7CFB\u6570\u636E\u5E93\u4E4B\u4E00\uFF0C\u5BF9\u5404\u79CD\u9AD8\u7EA7\u4E14\u5E7F\u6CDB\u5E94\u7528\u7684\u529F\u80FD\u6709\u6700\u597D\u7684\u652F\u6301\uFF0C\u662F\u4E2D\u5C0F\u578B\u5E94\u7528\u7684\u9996\u9009\u6570\u636E\u5E93\u3002"]}),"\n",(0,a.jsxs)(i.A,{groupId:"install",children:[(0,a.jsx)(t.A,{value:"pip",label:"\u4F7F\u7528 pip",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"pip install nonebot-plugin-orm[postgresql]\n"})})}),(0,a.jsx)(t.A,{value:"pdm",label:"\u4F7F\u7528 pdm",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"pdm add nonebot-plugin-orm[postgresql]\n"})})})]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"SQLALCHEMY_DATABASE_URL=postgresql+psycopg://user:password@host:port/dbname[?key=value&key=value...]\n"})}),"\n",(0,a.jsx)(n.h3,{id:"mysql--mariadb",children:"MySQL / MariaDB"}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.a,{href:"https://www.mysql.com/",children:"MySQL"})," \u548C ",(0,a.jsx)(n.a,{href:"https://mariadb.com/",children:"MariaDB"})," \u662F\u7ECF\u5178\u7684\u5F00\u6E90\u5173\u7CFB\u6570\u636E\u5E93\uFF0C\u9002\u5408\u7528\u4E8E\u4E2D\u5C0F\u578B\u5E94\u7528\u3002"]}),"\n",(0,a.jsxs)(i.A,{groupId:"install",children:[(0,a.jsx)(t.A,{value:"pip",label:"\u4F7F\u7528 pip",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"pip install nonebot-plugin-orm[mysql]\n"})})}),(0,a.jsx)(t.A,{value:"pdm",label:"\u4F7F\u7528 pdm",children:(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"pdm add nonebot-plugin-orm[mysql]\n"})})})]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"SQLALCHEMY_DATABASE_URL=mysql+aiomysql://user:password@host:port/dbname[?key=value&key=value...]\n"})}),"\n",(0,a.jsx)(n.h2,{id:"\u4F7F\u7528",children:"\u4F7F\u7528"}),"\n",(0,a.jsxs)(n.p,{children:["\u672C\u63D2\u4EF6\u63D0\u4F9B\u4E86\u6570\u636E\u5E93\u8FC1\u79FB\u529F\u80FD\uFF08\u6B64\u529F\u80FD\u4F9D\u8D56\u4E8E ",(0,a.jsx)(n.a,{href:"../../quick-start#%E5%AE%89%E8%A3%85%E8%84%9A%E6%89%8B%E6%9E%B6",children:"nb-cli \u811A\u624B\u67B6"}),"\uFF09\u3002\n\u5728\u5B89\u88C5\u4E86\u65B0\u7684\u63D2\u4EF6\u6216\u673A\u5668\u4EBA\u4E4B\u540E\uFF0C\u4F60\u9700\u8981\u6267\u884C\u4E00\u6B21\u6570\u636E\u5E93\u8FC1\u79FB\u64CD\u4F5C\uFF0C\u5C06\u6570\u636E\u5E93\u540C\u6B65\u81F3\u4E0E\u673A\u5668\u4EBA\u4E00\u81F4\u7684\u72B6\u6001\uFF1A"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"nb orm upgrade\n"})}),"\n",(0,a.jsx)(n.p,{children:"\u8FD0\u884C\u5B8C\u6BD5\u540E\uFF0C\u53EF\u4EE5\u68C0\u67E5\u4E00\u4E0B\uFF1A"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-shell",children:"nb orm check\n"})}),"\n",(0,a.jsxs)(n.p,{children:["\u5982\u679C\u8F93\u51FA\u662F ",(0,a.jsx)(n.code,{children:"\u6CA1\u6709\u68C0\u6D4B\u5230\u65B0\u7684\u5347\u7EA7\u64CD\u4F5C"}),"\uFF0C\u90A3\u4E48\u606D\u559C\u4F60\uFF0C\u6570\u636E\u5E93\u5DF2\u7ECF\u8FC1\u79FB\u5B8C\u6210\u4E86\uFF0C\u4F60\u53EF\u4EE5\u542F\u52A8\u673A\u5668\u4EBA\u4E86\u3002"]})]})}function p(e={}){let{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(h,{...e})}):h(e)}},90118(e,n,l){l.d(n,{OC:()=>h,O_:()=>m,uc:()=>b,vT:()=>d});var r=l(62540),a=l(63696),s=l(49519),t=l(92882),i=l(76274),o=l(635),c=l(56113);function d(e){return a.Children.toArray(e).filter(e=>"\n"!==e)}function u({value:e,tabValues:n}){return n.some(n=>n.value===e)}function h(e){let n,{defaultValue:l,queryString:r=!1,groupId:d}=e,h=function(e){let{values:n,children:l}=e;return(0,a.useMemo)(()=>{let e=n??a.Children.toArray(l).flatMap(e=>{if(!e)return[];if((0,a.isValidElement)(e)&&function(e){let{props:n}=e;return!!n&&"object"==typeof n&&"value"in n}(e))return[e];let n="string"==typeof e.type?e.type:e.type.name;throw Error(`Docusaurus error: Bad <Tabs> child <${n}>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop. 2If you do not want to pass on a "value" prop to the direct children of <Tabs>, you can also pass an explicit <Tabs values={...}> prop.`)}).map(({props:{value:e,label:n,attributes:l,default:r}})=>({value:e,label:n,attributes:l,default:r})),r=(0,o.XI)(e,(e,n)=>e.value===n.value);if(r.length>0)throw Error(`Docusaurus error: Duplicate values "${r.map(e=>`'${e.value}'`).join(", ")}" found in <Tabs>. Every value needs to be unique.`);return e},[n,l])}(e),[p,b]=(0,a.useState)(()=>(function({defaultValue:e,tabValues:n}){if(0===n.length)throw Error("Docusaurus error: the <Tabs> component requires at least one <TabItem> children component");if(e){if(!u({value:e,tabValues:n}))throw Error(`Docusaurus error: The <Tabs> has a defaultValue "${e}" but none of its children has the corresponding value. Available values are: ${n.map(e=>e.value).join(", ")}. If you intend to show no default tab, use defaultValue={null} instead.`);return e}let l=n.find(e=>e.default)??n[0];if(!l)throw Error("Unexpected error: 0 tabValues");return l.value})({defaultValue:l,tabValues:h})),[m,x]=function({queryString:e=!1,groupId:n}){let l=(0,s.W6)(),r=function({queryString:e=!1,groupId:n}){if("string"==typeof e)return e;if(!1===e)return null;if(!0===e&&!n)throw Error('Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".');return n??null}({queryString:e,groupId:n});return[(0,i.aZ)(r),(0,a.useCallback)(e=>{if(!r)return;let n=new URLSearchParams(l.location.search);n.set(r,e),l.replace({...l.location,search:n.toString()})},[r,l])]}({queryString:r,groupId:d}),[j,f]=function({groupId:e}){let n=e?`docusaurus.tab.${e}`:null,[l,r]=(0,c.Dv)(n);return[l,(0,a.useCallback)(e=>{n&&r.set(e)},[n,r])]}({groupId:d}),g=u({value:n=m??j,tabValues:h})?n:null;return(0,t.A)(()=>{g&&b(g)},[g]),{selectedValue:p,selectValue:(0,a.useCallback)(e=>{if(!u({value:e,tabValues:h}))throw Error(`Can't select invalid tab value=${e}`);b(e),x(e),f(e)},[x,f,h]),tabValues:h,lazy:e.lazy??!1,block:e.block??!1}}let p=(0,a.createContext)(null);function b(){let e=a.useContext(p);if(!e)throw Error("useTabsContext() must be used within a Tabs component");return e}function m(e){return(0,r.jsx)(p.Provider,{value:e.value,children:e.children})}},84993(e,n,l){l.d(n,{A:()=>i});var r=l(62540);l(63696);var a=l(11750),s=l(90118);function t({children:e,className:n,hidden:l}){return(0,r.jsx)("div",{role:"tabpanel",className:(0,a.A)("tab-panel",n),hidden:l,children:e})}function i({children:e,className:n,value:l}){let{selectedValue:a,lazy:o}=(0,s.uc)(),c=l===a;return!c&&o?null:(0,r.jsx)(t,{className:n,hidden:!c,children:e})}},36031(e,n,l){l.d(n,{A:()=>h});var r=l(62540);l(63696);var a=l(11750),s=l(49130),t=l(90118),i=l(72489),o=l(48682);function c({className:e}){let{selectedValue:n,selectValue:l,tabValues:s,block:o}=(0,t.uc)(),d=[],{blockElementScrollPositionUntilNextRender:u}
2=(0,i.a_)(),h=e=>{let r=e.currentTarget,a=s[d.indexOf(r)].value;a!==n&&(u(r),l(a))},p=e=>{let n=null;switch(e.key){case"Enter":h(e);break;case"ArrowRight":{let l=d.indexOf(e.currentTarget)+1;n=d[l]??d[0];break}case"ArrowLeft":{let l=d.indexOf(e.currentTarget)-1;n=d[l]??d[d.length-1]}}n?.focus()};return(0,r.jsx)("div",{className:"not-prose",children:(0,r.jsxs)("ul",{role:"tablist","aria-orientation":"horizontal",className:(0,a.A)("tabs tabs-list tabs-lifted",o&&"tabs-block",e),children:[s.map(({value:e,label:l,attributes:s})=>(0,r.jsx)("li",{role:"tab",tabIndex:n===e?0:-1,"aria-selected":n===e,ref:e=>{d.push(e)},onKeyDown:p,onClick:h,...s,className:(0,a.A)("tab tab-lifted tab-item",s?.className,n===e&&"tab-active tab-item-active"),children:l??e},e)),(0,r.jsx)("div",{className:"tab tab-lifted tab-item tab-spacer"})]})})}function d({children:e}){return(0,r.jsx)("div",{children:e})}function u({className:e,children:n}){return(0,r.jsxs)("div",{className:(0,a.A)(s.G.tabs.container,"tabs-container"),children:[(0,r.jsx)(c,{className:e}),(0,r.jsx)(d,{children:n})]})}function h(e){let n=(0,o.A)(),l=(0,t.OC)(e);return(0,r.jsx)(t.O_,{value:l,children:(0,r.jsx)(u,{className:e.className,children:(0,t.vT)(e.children)})},String(n))}},43023(e,n,l){l.d(n,{R:()=>t,x:()=>i});var r=l(63696);let a={},s=r.createContext(a);function t(e){let n=r.useContext(s);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:t(e.components),r.createElement(s.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.