local db_env = nil
local db_conn = nil
function main()
while not isSampAvailable() do wait(0) end
local status, lib = pcall(require, "luasql.postgres")
if not status then
print("ОШИБКА: Не удалось загрузить luasql.postgres. Ошибка: " .. tostring(lib))
return
end
sampRegisterChatCommand("dbtest", function()
lua_thread.create(function()
if db_conn then
sampAddChatMessage("[DB] Соединение уже установлено. Для сброса используйте /dbclose.", 0xFFFF00)
return
end
local luasql = require("luasql.postgres")
db_env = luasql.postgres()
if not db_env then
print("ОШИБКА: Не удалось создать окружение luasql.")
return
end
local conn, err = db_env:connect("test_db", "postgres", "228228")
if not conn then
print("ОШИБКА ПОДКЛЮЧЕНИЯ: " .. tostring(err))
if db_env then db_env:close() end
db_env = nil
return
end
db_conn = conn
sampAddChatMessage("[DB] Успешно подключено!", 0x00FF00)
local cursor, err_exec = db_conn:execute("SELECT nickname, score FROM players ORDER BY score DESC")
if not cursor then
print("ОШИБКА ЗАПРОСА: " .. tostring(err_exec))
return
end
sampAddChatMessage("--- Игроки в базе данных ---", 0x00FFFF)
local row = cursor:fetch({}, "a")
while row do
sampAddChatMessage(string.format("Ник: %s, Очки: %d", row.nickname, row.score), 0x00FFFF)
row = cursor:fetch({}, "a")
end
cursor:close()
end)
end)
sampRegisterChatCommand("dbclose", function()
if db_conn then
db_conn:close()
db_conn = nil
end
if db_env then
db_env:close()
db_env = nil
end
sampAddChatMessage("[DB] Соединение с базой данных закрыто.", 0x00FF00)
end)
sampAddChatMessage("[DB] Скрипт загружен. Команды: /dbtest, /dbclose", 0x00FF00)
wait(-1)
end
function onScriptTerminate(script, quitGame)
if db_conn then db_conn:close() end
if db_env then db_env:close() end
end