# 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);
}
```


---

# 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://discord-base.gitbook.io/discord-base/utilities/local-mysql.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.
