Skip to content

Getting Started

LODS edited this page Mar 4, 2023 · 4 revisions

In this tutorial you will learn to make a simple setup of DB Manager.

Connecting to database

To connect to the database, you must create a DB Manager instance. This can be by either passing the connection parameters separately to the DBManager constructor.

-- Option 1: SQLite
local conn = DBManager:new({
    dialect = "sqlite",
    storage = "databases/db.sqlite"
})

-- Option 2: MySQL
local conn = DBManager:new({
    dialect = "mysql",
    host = "localhost",
    port = 3306,
    username = "root",
    password = "123456",
    database = "test_db_manager",
})

Testing the connection

You can use the getConnection() method to test if the connection is OK:

-- conn was the variable used in the exemple above.
if (conn:getConnection()) then
    outputDebugString("Connection has been established successfully.", 3)
else
    error("Unable to connect to the database.", 2)
end

Closing the connection

DB Manager will keep the connection by default, and use the same connection for all queries. If you need to close the connection, call conn:close()

NOTE: Once conn:close() has been called, it's impossible to open a new connection. You will need to create a new DB Manager instance to access your database again.

Clone this wiki locally