> For the complete documentation index, see [llms.txt](https://discord-base.gitbook.io/discord-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://discord-base.gitbook.io/discord-base/utilities/local-mysql.md).

# Local MySQL

## Constructor

The constructor initializes a new instance of the `sql` class, allowing you to specify whether to use an in-memory database or a file-based database.

```typescript
constructor(location: boolean | string);
```

<table><thead><tr><th width="165">parameter</th><th>description</th></tr></thead><tbody><tr><td>location</td><td>A boolean for an in-memory database (<code>true</code>), or a string for a file-based database (specifying the database filename without the extension).</td></tr></tbody></table>

## Methods

### `get`

Returns the SQLite database instance.

```typescript
public get get(): sqlite3.Database | null;
```

### `query`

Executes a SQL query with optional parameters.

```typescript
public query<T>(query: string, params: any[] = []): Promise<T[]>;
```

<table><thead><tr><th width="165">parameter</th><th>description</th></tr></thead><tbody><tr><td>query</td><td>The SQL query to execute.</td></tr><tr><td>params</td><td>Optional parameters for the SQL query.ms</td></tr></tbody></table>

### `refresh`

Refreshes the SQLite database connection.

```typescript
public refresh(): Promise<void>;
```

## File-Based Example

Here’s how you can use the `sql` class to create a database and execute a query:

```typescript
import sql from "@utils/sql";
const db = new sql("data");

try {
    await db.query("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)");
    await db.query("INSERT INTO users (name) VALUES (?)", ["dragos"]);
    
    const results = await db.query("SELECT * FROM users");
    console.log(results);
} catch (error) {
    console.error("Database error:", error);
}
```

## In-Memory Database Example

To create an in-memory database, simply pass `true` to the constructor:

```typescript
import sql from "@utils/sql";
const db = new sql(true);

try {
    await db.query("CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, value TEXT)");
    await db.query("INSERT INTO items (value) VALUES (?)", ["sample"]);
    
    const results = await db.query("SELECT * FROM items");
    console.log(results);
} catch (error) {
    console.error("Database error:", error);
}
```
