# dbConnect (Server)

> Opens a connection to an SQLite or MySQL database. Queries run on the connection's own thread.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Server
- **Category**: Database
- **URL**: https://wiki.mymp.gg/en/dbConnect

## Syntax

```lua
element|false dbConnect(string type, string target [, string user = "", string password = ""])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `type` | `string` | `"sqlite"` or `"mysql"`. |
| `target` | `string` | For SQLite, the file's path inside the calling resource (folders are created). For MySQL, `key=value` pairs separated by `;`: `dbname`, `host`, `port` and `charset` (`utf8mb4`, `utf8mb3`, `utf8`, `latin1` or `ascii`). |
| `user` | `string` | The MySQL user. *(default: `""`)* |
| `password` | `string` | The MySQL password. *(default: `""`)* |

## Returns

The connection element, or `false` if it could not be opened (the reason goes to the log).

The connection closes when its element is destroyed or the resource stops.

## Example

```lua
local db = dbConnect("sqlite", "data/players.db")
dbExec(db, "CREATE TABLE IF NOT EXISTS players (account TEXT PRIMARY KEY, money INTEGER)")

local mysql = dbConnect("mysql", "dbname=game;host=127.0.0.1;port=3306;charset=utf8mb4", "game", getConvar("db_password"))
```

## See also

- [dbQuery (Server)](https://wiki.mymp.gg/en/dbQuery.md): Sends an SQL query whose answer is read later with dbPoll.
- [dbExec (Server)](https://wiki.mymp.gg/en/dbExec.md): Runs an SQL statement without waiting for an answer.
- [destroyElement (Shared)](https://wiki.mymp.gg/en/destroyElement.md): Destroys an element and all of its children. Before it goes, each one fires onElementDestroy on the server or onClientElementDestroy on the client.
