企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 20. 数据库文件导入 ## 安装数据库 在开发完成后,导出需要用到的数据表,保存插件所在文件夹根目录,命名为`install.sql`,在安装的时候导入数据库表。 你需要把表的前缀替换成 `@prefix@`,如下所示 ``` <pre class="calibre16">``` CREATE TABLE IF NOT EXISTS `@prefix@tablename` <span class="token1">(</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">.</span><span class="token1">.</span><span class="token1">.</span> <span class="token1">)</span> ENGINE<span class="token">=</span>InnoDB DEFAULT CHARSET<span class="token">=</span>utf8mb4<span class="token1">;</span> ``` ``` - 手动导入方式 ``` <pre class="calibre16">``` <span class="token5">// 文件绝对路径</span> $sql <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>addon_path<span class="token1">.</span><span class="token4">'data'</span><span class="token1">.</span>DIRECTORY_SEPARATOR<span class="token1">.</span><span class="token4">'install.sql'</span><span class="token1">;</span> <span class="token5">// 写入数据库</span> <span class="token6">create_sql</span><span class="token1">(</span>$sql<span class="token1">)</span><span class="token1">;</span> ``` ``` ## 升级数据库 插件所在文件夹根目录,命名为`upgrade.sql`,在更新的时候会执行该文件。 同样你需要把表的前缀替换成 `@prefix@` ## 演示数据数据库文件 插件根目录命名为:`demodata.sql`,在安装的时候用户勾选演示数据,即可导入演示数据。 数据库里面的表前缀使用`@prefix@` 代替,如下 ``` <pre class="calibre16">``` insert into `@prefix@archives` values <span class="token1">(</span><span class="token3">75</span><span class="token1">,</span><span class="token3">20</span><span class="token1">,</span><span class="token3">1</span><span class="token1">,</span><span class="token3">2</span><span class="token1">,</span><span class="token4">'测试'</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token4">'show.html'</span><span class="token1">,</span><span class="token3">1637382374</span><span class="token1">,</span><span class="token3">1637382417</span><span class="token1">,</span><span class="token3">1637382417</span><span class="token1">,</span>NULL<span class="token1">,</span><span class="token3">0</span><span class="token1">,</span><span class="token4">'default'</span><span class="token1">,</span><span class="token4">''</span><span class="token1">,</span><span class="token3">0</span><span class="token1">,</span><span class="token4">'zh-cn'</span><span class="token1">,</span><span class="token4">'normal'</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 效果 ![](https://img.kancloud.cn/a8/62/a862577c15d82029eec038a799b99758_799x595.png)