0
# SQLite3
1
2
SQLite3 provides asynchronous, non-blocking SQLite3 bindings for Node.js. It offers a comprehensive interface for SQLite database operations including query execution, parameter binding, full Buffer/Blob support, extensive debugging capabilities, query serialization API, and extension support.
3
4
## Package Information
5
6
- **Package Name**: sqlite3
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install sqlite3`
10
11
## Core Imports
12
13
```javascript
14
const sqlite3 = require('sqlite3').verbose();
15
```
16
17
For ES modules:
18
19
```javascript
20
import sqlite3 from 'sqlite3';
21
const { Database, Statement, Backup } = sqlite3;
22
```
23
24
## Basic Usage
25
26
```javascript
27
const sqlite3 = require('sqlite3').verbose();
28
const db = new sqlite3.Database(':memory:');
29
30
db.serialize(() => {
31
db.run("CREATE TABLE lorem (info TEXT)");
32
33
const stmt = db.prepare("INSERT INTO lorem VALUES (?)");
34
for (let i = 0; i < 10; i++) {
35
stmt.run("Ipsum " + i);
36
}
37
stmt.finalize();
38
39
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
40
console.log(row.id + ": " + row.info);
41
});
42
});
43
44
db.close();
45
```
46
47
## Architecture
48
49
SQLite3 is built around several key components:
50
51
- **Database Class**: Main interface for database connections and operations
52
- **Statement Class**: Prepared statement interface for efficient query execution
53
- **Backup Class**: Database backup and restoration functionality
54
- **Event System**: EventEmitter-based events for trace, profile, change, and error handling
55
- **Native Bindings**: C++ implementation using Node-API for cross-platform compatibility
56
- **Connection Caching**: Built-in database connection caching via `sqlite3.cached`
57
58
## Capabilities
59
60
### Database Operations
61
62
Core database functionality for connecting, querying, and managing SQLite databases. Supports both callback-based and event-driven patterns.
63
64
```javascript { .api }
65
class Database extends EventEmitter {
66
constructor(filename: string, mode?: number, callback?: (err: Error | null) => void);
67
68
run(sql: string, params?: any, callback?: (this: RunResult, err: Error | null) => void): this;
69
get<T>(sql: string, params?: any, callback?: (this: Statement, err: Error | null, row: T) => void): this;
70
all<T>(sql: string, params?: any, callback?: (this: Statement, err: Error | null, rows: T[]) => void): this;
71
each<T>(sql: string, params?: any, callback?: (this: Statement, err: Error | null, row: T) => void, complete?: (err: Error | null, count: number) => void): this;
72
73
// Enhanced event management with automatic SQLite configuration
74
addListener(event: string, listener: (...args: any[]) => void): this;
75
removeListener(event: string, listener: (...args: any[]) => void): this;
76
removeAllListeners(event?: string): this;
77
}
78
```
79
80
[Database Operations](./database.md)
81
82
### Prepared Statements
83
84
Statement preparation and execution with parameter binding. Provides efficient query execution for repeated operations.
85
86
```javascript { .api }
87
class Statement extends EventEmitter {
88
bind(params?: any, callback?: (err: Error | null) => void): this;
89
run(params?: any, callback?: (this: RunResult, err: Error | null) => void): this;
90
get<T>(params?: any, callback?: (this: RunResult, err: Error | null, row?: T) => void): this;
91
all<T>(params?: any, callback?: (this: RunResult, err: Error | null, rows: T[]) => void): this;
92
finalize(callback?: (err: Error) => void): Database;
93
}
94
```
95
96
[Prepared Statements](./statement.md)
97
98
### Database Backup
99
100
Database backup and restoration functionality with step-by-step control and progress monitoring.
101
102
```javascript { .api }
103
class Backup extends EventEmitter {
104
step(pages: number, callback?: (err: Error | null) => void): this;
105
finish(callback?: (err: Error | null) => void): this;
106
107
readonly remaining: number;
108
readonly pageCount: number;
109
readonly completed: boolean;
110
readonly failed: boolean;
111
readonly idle: boolean;
112
}
113
```
114
115
[Database Backup](./backup.md)
116
117
### Constants and Configuration
118
119
SQLite constants for database modes, error codes, and configuration limits.
120
121
```javascript { .api }
122
// Database mode flags
123
const OPEN_READONLY: number;
124
const OPEN_READWRITE: number;
125
const OPEN_CREATE: number;
126
127
// Error codes
128
const OK: number;
129
const ERROR: number;
130
const BUSY: number;
131
// ... and many more
132
133
// Version information
134
const VERSION: string;
135
const VERSION_NUMBER: number;
136
```
137
138
[Constants and Configuration](./constants.md)
139
140
## Utility Functions
141
142
### Verbose Mode
143
144
```javascript { .api }
145
/**
146
* Enables verbose mode with enhanced stack trace support
147
* @returns The sqlite3 module with verbose mode enabled
148
*/
149
function verbose(): sqlite3;
150
```
151
152
### Database Caching
153
154
```javascript { .api }
155
const cached: {
156
/**
157
* Returns cached database instances based on filename
158
* @param filename - Database file path
159
* @param mode - Optional database mode
160
* @param callback - Optional callback function
161
* @returns Database instance (cached or new)
162
*/
163
Database(filename: string, mode?: number, callback?: (this: Database, err: Error | null) => void): Database;
164
};
165
```
166
167
## Types
168
169
```javascript { .api }
170
interface RunResult extends Statement {
171
/** ID of the last inserted row */
172
lastID: number;
173
/** Number of rows changed by the last statement */
174
changes: number;
175
}
176
177
interface sqlite3 {
178
Database: typeof Database;
179
Statement: typeof Statement;
180
Backup: typeof Backup;
181
cached: typeof cached;
182
verbose(): this;
183
184
// All constants (mode flags, error codes, limits)
185
OPEN_READONLY: number;
186
OPEN_READWRITE: number;
187
// ... (all other constants)
188
}
189
```