I found myself in need of a mini database to store structured ingame session data so I came up with this
the beauty of Lua, these seemingly simple 4 functions, nothing more than table wrappers, actually offer a pretty powerful database system, Lua tables rock, I had almost forgotten how much I love working with this language
Edit: 7/4/2016
I have revised this script and included the ability to save a database to disk and reload it using json, so your games can now have persistent data, I have also created a function table for the script, its just how I like to format groups of functions in Lua scripts
the core: database functions and JSON include
-- load json module
JSON = assert(loadfile "scriptbank\\custom\\json.lua")() --- EDIT PATH TO YOUR FILES
-- internal database table
tbl_database={}
-- public function table
Database={}
function Database.Create(ID) -- creates a database
tbl_database[ID]={}
end
function Database.AddRow(ID, tbl_data) -- adds a row to the database
table.insert(tbl_database[ID], tbl_data)
end
function Database.CountRows(ID) -- counts database rows
return #tbl_database[ID]
end
function Database.GetRow(ID, row) -- get a database row data
return tbl_database[ID][row]
end
function Database.RemoveRow(ID, row) -- removes a database row
table.remove(tbl_database[ID],row)
end
function Database.Clear(ID) -- clear a database
tbl_database[ID]=nil
end
function Database.Save(ID, filename)
local file = io.open (filename , "w+")
local json_text = JSON:encode(tbl_database[ID])
file:write(json_text)
file:close()
end
function Database.Load(ID, filename)
local tbl_return={}
local file = io.open (filename , "r")
if file then
tbl_database[ID] = JSON:decode(file:read("*a"), "bla")
file:close()
end
end
and a full example
-- load json module
JSON = assert(loadfile "scriptbank\\custom\\json.lua")() --- EDIT PATH TO YOUR FILES
-- internal database table
tbl_database={}
-- public function table
Database={}
function Database.Create(ID) -- creates a database
tbl_database[ID]={}
end
function Database.AddRow(ID, tbl_data) -- adds a row to the database
table.insert(tbl_database[ID], tbl_data)
end
function Database.CountRows(ID) -- counts database rows
return #tbl_database[ID]
end
function Database.GetRow(ID, row) -- get a database row data
return tbl_database[ID][row]
end
function Database.RemoveRow(ID, row) -- removes a database row
table.remove(tbl_database[ID],row)
end
function Database.Clear(ID) -- clear a database
tbl_database[ID]=nil
end
function Database.Save(ID, filename)
local file = io.open (filename , "w+")
local json_text = JSON:encode(tbl_database[ID])
file:write(json_text)
file:close()
end
function Database.Load(ID, filename)
local tbl_return={}
local file = io.open (filename , "r")
if file then
tbl_database[ID] = JSON:decode(file:read("*a"), "bla")
file:close()
end
end
-- Example code
tmpVar_LoadOrCreate="create" -- set to "anything" to disable creation code and load saved database
function database_init(e)
if tmpVar_LoadOrCreate == "create" then
-- firse we define the database
Database.Create("testDB")
-- now we add some data
tbl_data={} -- define the data table
-- index the table item by column name "column1/2/3/4"
tbl_data["column1"]="row 1 - item 1" -- add column 1 data
tbl_data["column2"]="row 1 - item 2" -- add column 1 data
tbl_data["column3"]="row 1 - item 3" -- add column 1 data
tbl_data["column4"]="row 1 - item 4" -- add column 1 data
-- now add the above table to the db table
Database.AddRow("testDB", tbl_data)
-- add a couple more items
tbl_data={}
tbl_data["column1"]="row 2 - item 1"
tbl_data["column2"]="row 2 - item 2"
tbl_data["column3"]="row 2 - item 3"
tbl_data["column4"]="row 2 - item 4"
Database.AddRow("testDB", tbl_data)
tbl_data={}
tbl_data["column1"]="row 3 - item 1"
tbl_data["column2"]="row 3 - item 2"
tbl_data["column3"]="row 3 - item 3"
tbl_data["column4"]="row 3 - item 4"
Database.AddRow("testDB", tbl_data)
Database.Save("C:\\Users\\Main\\AppData\\Local\\luadatabase.dat")
else
-- set tmpVar_LoadOrCreate="anything" to enable loading from disk
Database.Load("testDB", "C:\\Users\\Main\\AppData\\Local\\luadatabase.dat")
Database.RemoveRow("testDB", 1)
end
end
function database_main(e)
-- count the rows added to the database
rows=Database.CountRows("testDB")
-- display row count
Text(1, 1, 1, "Contains " ..rows.. " rows")
-- get data for row 1
local t_data = Database.GetRow("testDB",1)
-- display data
Text(1, 5, 1, "Column 1="..t_data["column1"])
Text(1, 9, 1, "Column 2="..t_data["column2"])
Text(1, 14, 1, "Column 3="..t_data["column3"])
Text(1, 18, 1, "Column 4="..t_data["column4"])
-- display all data
for i=1, rows do
-- get data for row 1
local t_data = Database.GetRow("testDB",i)
-- display data
str="Column 1="..t_data["column1"]
str=str.."Column 2="..t_data["column2"]
str=str.."Column 3="..t_data["column3"]
str=str.."Column 4="..t_data["column4"]
Text(20, i*5, 1, str)
end
end
function database_exit(e)
Database.Save("testDB", "C:\\Users\\Main\\AppData\\Local\\luadatabase.dat")
end
and thats it, nothing that cant be done in script with tables anyway, this just makes it a lot more coherent and user friendly
the script and the JSON module are both attached