快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-data-storage-sqlite`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/data-storage-sqlite/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-data-storage-sqlite` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-data-storage-sqlitebunx cap sync导入
“导入”章节import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';快速入门
章节标题为“快速入门”import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.openStore({ database: 'app', encrypted: true });await CapgoCapacitorDataStorageSqlite.set({ key: 'session', value: JSON.stringify(session) });const { value } = await CapgoCapacitorDataStorageSqlite.get({ key: 'session' });示例:命名空间表
章节标题:“示例:命名空间表”await CapgoCapacitorDataStorageSqlite.setTable({ table: 'drafts' });await CapgoCapacitorDataStorageSqlite.set({ key: 'invoice-42', value: draftJson });常见坑点
章节标题为“常见陷阱”- 使用前先打开:每次读写都需要一个已打开的 store 句柄。
- 加密范围:加密仅适用于 iOS 和 Android 原生 store,不适用于 Web 端 IndexedDB 路径。
- 退出登录时关闭:
clear()加closeStore()可防止共享设备上出现过期数据。
API 概览
章节标题:“API 概览”openStore
标题为 “openStore” 的章节打开存储
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.openStore({ database: 'database', table: 'table', encrypted: false, mode: 'encryption', autoVacuum: 'none',});closeStore
“closeStore” 部分关闭存储
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.closeStore({ database: 'database' });isStoreOpen
“isStoreOpen” 部分检查存储是否已打开
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.isStoreOpen({ database: 'database' });console.log(result);isStoreExists
“isStoreExists” 部分检查存储是否存在
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.isStoreExists({ database: 'database' });console.log(result);deleteStore
“deleteStore” 部分删除存储
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.deleteStore({ database: 'database', table: 'table', encrypted: false, mode: 'encryption', autoVacuum: 'none',});setTable
标题为“setTable”的章节在现有存储中设置或添加表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.setTable({ table: 'table' });set
标题为“set”的章节使用给定的键和值存储数据
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.set({ key: 'key-123' });get
标题为“get”的章节根据给定的数据键获取数据值
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.get({ key: 'key-123' });console.log(result);remove
标题为“remove”的章节删除指定键的数据
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.remove({ key: 'key-123' });clear
标题为“clear”的章节清空数据存储(删除所有键)
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.clear();iskey
标题为“iskey”的章节检查数据键是否存在
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.iskey({ key: 'key-123' });console.log(result);keys
标题为“keys”的章节获取数据键列表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.keys();console.log(result);values
标题为“values”的章节获取数据值列表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.values();console.log(result);filtervalues
标题为“filtervalues”的章节获取过滤键的数据值列表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.filtervalues({ filter: 'filter' });console.log(result);keysvalues
标题为“keysvalues”的章节获取数据键值对列表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.keysvalues();console.log(result);isTable
标题为“isTable”的章节检查表是否存在
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.isTable({ table: 'table' });console.log(result);tables
标题为“tables”的章节获取当前存储的表列表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.tables();console.log(result);deleteTable
标题为“deleteTable”的部分删除表
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
await CapgoCapacitorDataStorageSqlite.deleteTable({ table: 'table' });importFromJson
标题为“importFromJson”的部分从 JSON 导入数据库
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.importFromJson({ jsonstring: 'jsonstring' });console.log(result);isJsonValid
标题为“isJsonValid”的部分检查 JSON 对象是否有效
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.isJsonValid({ jsonstring: 'jsonstring' });console.log(result);exportToJson
标题为“exportToJson”的章节将给定数据库导出为 JSON 对象
import { CapgoCapacitorDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
const result = await CapgoCapacitorDataStorageSqlite.exportToJson();console.log(result);类型参考
“类型参考” 章节capOpenStorageOptions
标题为“capOpenStorageOptions”的章节export interface capOpenStorageOptions { /** * The storage database name */ database?: string; // default: // ios, android: storageSQLite // web : storageIDB /** * The storage table name */ table?: string; // default: // ios, android: storage_table // web: storage_store /** * Set to true for database encryption */ encrypted?: boolean; // only for ios and android /*** * Set the mode for database encryption * ["encryption", "secret","newsecret"] */ mode?: string; // only for ios and android}capStorageOptions
标题为“capStorageOptions”的章节export interface capStorageOptions { /** * The storage name */ database: string;}capDataStorageResult
标题为“capDataStorageResult”的章节export interface capDataStorageResult { /** * result set to true when successful else false */ result?: boolean; /** * a returned message */ message?: string;}capTableStorageOptions
标题为“capTableStorageOptions”的章节export interface capTableStorageOptions { /** * The storage table name */ table: string;}capDataStorageOptions
标题为“capDataStorageOptions”的章节export interface capDataStorageOptions { /** * The data name */ key: string; /** * The data value when required */ value?: string;}capValueResult
标题为 “capValueResult” 的章节export interface capValueResult { /** * the data value for a given data key */ value: string;}capKeysResult
标题为 “capKeysResult” 的章节export interface capKeysResult { /** * the data key list as an Array */ keys: string[];}capValuesResult
标题为 “capValuesResult” 的章节export interface capValuesResult { /** * the data values list as an Array */ values: string[];}capFilterStorageOptions
标题为 “capFilterStorageOptions” 的章节export interface capFilterStorageOptions { /** * The filter data for filtering keys * * ['%filter', 'filter', 'filter%'] for * [starts with filter, contains filter, ends with filter] */ filter: string;}capKeysValuesResult
标题为 “capKeysValuesResult” 的章节export interface capKeysValuesResult { /** * the data keys/values list as an Array of {key:string,value:string} */ keysvalues: any[];}capTablesResult
标题为 “capTablesResult” 的章节export interface capTablesResult { /** * the tables list as an Array */ tables: string[];}capStoreImportOptions
标题为“capStoreImportOptions”的章节export interface capStoreImportOptions { /** * Set the JSON object to import * */ jsonstring?: string;}单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划存储和文件处理,请将其与 使用 @capgo/capacitor-data-storage-sqlite 关于使用 @capgo/capacitor-data-storage-sqlite 的原生能力, @capgo/capacitor-data-storage-sqlite 关于 @capgo/capacitor-data-storage-sqlite 中的实现细节, @capgo/capacitor-file 关于 @capgo/capacitor-file 中的实现细节, 使用 @capgo/capacitor-file 用于“使用 @capgo/capacitor-file”中的原生能力,以及 @capgo/capacitor-uploader 了解 @capgo/capacitor-uploader. 的实现细节