# SQLite快速入门

![](https://libs.websoft9.com/Websoft9/DocsPicture/zh/sqlite/sqlite-gui-websoft9.png)

### 准备[​](https://support.websoft9.com/docs/sqlite#%E5%87%86%E5%A4%87) <a href="#zhun-bei" id="zhun-bei"></a>

部署 创达睿通 提供的 SQLite 之后，需完成如下的准备工作：

1. 在云控制台获取您的 **服务器公网IP地址**
2. 在云控制台安全组中，确保 **Inbound（入）规则** 下的 **TCP:9090** 端口已经开启
3. 在服务器中查看 SQLite 的 **默认账号和密码**
4. 若想用域名访问 SQLite，务必先完成 **域名五步设置** 过程

### SQLite 初始化向导[​](https://support.websoft9.com/docs/sqlite#sqlite-%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%91%E5%AF%BC) <a href="#sqlite-chu-shi-hua-xiang-dao" id="sqlite-chu-shi-hua-xiang-dao"></a>

#### 详细步骤[​](https://support.websoft9.com/docs/sqlite#%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4) <a href="#xiang-xi-bu-zhou" id="xiang-xi-bu-zhou"></a>

1. 使用 SSH 工具，连接到服务器
2. 运行 `sqlite3` 命令，显然类型下面的结果，即表明运行正常

   ```
   [root@VM-0-11-centos ~]# sqlite3
   SQLite version 3.29.0 2019-07-10 17:32:03
   Enter ".help" for usage hints.
   Connected to a transient in-memory database.
   Use ".open FILENAME" to reopen on a persistent database.
   sqlite>
   ```
3. 验证 SQLite 可视化管理工具

> 需要了解更多 SQLite 的使用，请参考官方文档：SQLite 教程

#### 出现问题？[​](https://support.websoft9.com/docs/sqlite#%E5%87%BA%E7%8E%B0%E9%97%AE%E9%A2%98) <a href="#chu-xian-wen-ti" id="chu-xian-wen-ti"></a>

若碰到问题，请第一时刻联系 **技术支持**。也可以先参考下面列出的问题定位或 **FAQ** 尝试快速解决问题。

### SQLite 使用入门[​](https://support.websoft9.com/docs/sqlite#sqlite-%E4%BD%BF%E7%94%A8%E5%85%A5%E9%97%A8) <a href="#sqlite-shi-yong-ru-men" id="sqlite-shi-yong-ru-men"></a>

下面以 **SQLite 程序连接** 作为一个任务，帮助用户快速入门：

### SQLite 常用操作[​](https://support.websoft9.com/docs/sqlite#sqlite-%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C) <a href="#sqlite-chang-yong-cao-zuo" id="sqlite-chang-yong-cao-zuo"></a>

#### 程序连接[​](https://support.websoft9.com/docs/sqlite#%E7%A8%8B%E5%BA%8F%E8%BF%9E%E6%8E%A5) <a href="#cheng-xu-lian-jie" id="cheng-xu-lian-jie"></a>

开发需要连接 SQLite，首先需要保证已经安装了对应的 SQlite 连接模块：

* PHP 默认安装并启用了 SQLite 扩展
* Python 默认安装了 SQLite 模块
* Java 需自行安装SQLite JDBC Driver

下面是一个典型的 PHP 连接 SQLite 的程序段：

```
<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('test.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
?>
```

#### 图形化工具[​](https://support.websoft9.com/docs/sqlite#gui) <a href="#gui" id="gui"></a>

本部署方案中预装 SQLite 数据库管理工具 `CloudBeaver` 。

下面我们完成的介绍如何使用可视化工具管理 SQLite。

**准备**[**​**](https://support.websoft9.com/docs/sqlite#%E5%87%86%E5%A4%87-1)

1. 使用 SSH 连接 SQLite 所在的服务器，在 SQLite 数据文件目录下创建一个数据库

   ```
   # 创建数据库
   cd /data/apps/cloudbeaver/volumes
   sqlite3 testDB.db

   # 增加表
   sqlite> CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
   )
   ```
2. 登录云控制台，开启服务器 安全组 9090 端口
3. 完成 CloudBeaver 初始化

**配置**[**​**](https://support.websoft9.com/docs/sqlite#%E9%85%8D%E7%BD%AE)

完成上述准备工作后，我们开始连接 SQLite 数据库：

1. 登录 CloudBeaver 控制台，打开：【Connection】>【Manual】，选择 **SQLite**&#x20;

   <figure><img src="https://libs.websoft9.com/Websoft9/DocsPicture/en/cloudbeaver/cloudbeaver-openconn-websoft9.png" alt=""><figcaption></figcaption></figure>
2. 参考下面的建议，设置连接信息，然后点击【Save】

   * Driver 保持默认的 SQLite
   * Connection Name 设置为一个便于识别的名字即可
   * Database 为 SQLite 数据库文件的路径，路径前缀必须是：**/opt/cloudbeaver/workspace/**，再接上文件名称

   ![](https://libs.websoft9.com/Websoft9/DocsPicture/en/cloudbeaver/cloudbeaver-openconnsqlite-websoft9.png)
3. 设置信息保存后，使用这个 SQLite 连接，输入数据库的账号和密码&#x20;

   <figure><img src="https://libs.websoft9.com/Websoft9/DocsPicture/en/cloudbeaver/cloudbeaver-conlogin-websoft9.png" alt=""><figcaption></figcaption></figure>
4. 成功连接到 SQLite，发现可以看到之前创建的 Company 表&#x20;

   <figure><img src="https://libs.websoft9.com/Websoft9/DocsPicture/en/cloudbeaver/cloudbeaver-listtable-websoft9.png" alt=""><figcaption></figcaption></figure>

### SQLite 参数[​](https://support.websoft9.com/docs/sqlite#sqlite-%E5%8F%82%E6%95%B0) <a href="#sqlite-can-shu" id="sqlite-can-shu"></a>

SQLite 应用中包含 Nginx, Docker, CloudBeaver 等组件，可通过 **通用参数表** 查看路径、服务、端口等参数。

通过运行 `docker ps`，可以查看到 SQLite 运行时所有的 Container：

```
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
```

下面仅列出 SQLite 本身的参数：

#### 路径[​](https://support.websoft9.com/docs/sqlite#path) <a href="#path" id="path"></a>

SQLite 可执行程序： */usr/bin/sqlite3*\
SQLite 数据库文件目录： */data/apps/cloudbeaver/volumes*

#### 端口[​](https://support.websoft9.com/docs/sqlite#%E7%AB%AF%E5%8F%A3) <a href="#duan-kou" id="duan-kou"></a>

| 端口号  | 用途                       | 必要性 |
| ---- | ------------------------ | --- |
| 9090 | 通过 HTTP 访问 SQLite 可视化工具台 | 可选  |

#### 版本[​](https://support.websoft9.com/docs/sqlite#%E7%89%88%E6%9C%AC) <a href="#ban-ben" id="ban-ben"></a>

```
sqlite3 --version
```

#### 命令行[​](https://support.websoft9.com/docs/sqlite#%E5%91%BD%E4%BB%A4%E8%A1%8C) <a href="#ming-ling-hang" id="ming-ling-hang"></a>

SQLite 提供了强大的的命令行工具 `sqlite3`

**程序命令行**[**​**](https://support.websoft9.com/docs/sqlite#%E7%A8%8B%E5%BA%8F%E5%91%BD%E4%BB%A4%E8%A1%8C)

```
# 创建数据库
sqlite3 testDB.db

# 查看帮助
sqlite3 --help

# 查看版本
sqlite3 --version
```

**交互式命令行**[**​**](https://support.websoft9.com/docs/sqlite#%E4%BA%A4%E4%BA%92%E5%BC%8F%E5%91%BD%E4%BB%A4%E8%A1%8C)

以 `sqlite3 testDB.db` 命令，进入 SQLite 运行状态后，开始使用数据库交互式命令行

```
# 获取帮助
sqlite> .help

# 查询数据库列表
sqlite> .database

# 附件一个数据库。
# 数据库名称 main 和 temp 被保留用于主数据库和存储临时表及其他临时数据对象的数据库，不可用于附加
sqlite> ATTACH DATABASE 'myDB.db' as 'TEST'

# 创建表
sqlite> CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
)

# 查询表
sqlite> .tables

```

#### API[​](https://support.websoft9.com/docs/sqlite#api) <a href="#api" id="api"></a>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.imdcinsights.com/ying-yong-zhong-xin/yun-shu-ju-ku/sqlite-kuai-su-ru-men.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
