This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
thatsimplekid's profile picture

[Lua][Question] Need Help Making A Database

Started by thatsimplekid, 21 December 2012 - 12:25 PM
thatsimplekid #1
Posted 21 December 2012 - 01:25 PM
Hello, I am in the process of creating a banking system for my server that uses rednet servers and clients. I will need to make a database that can store PIN numbers and player's balances along with player's IGNs. I know how to use the fs API to write to simple files however I have no experience in databases on CC.
Can somebody please try to point me in the right direction :)/>
Any help is be greatly appreciated!

Thanks in advance

thatsimplekid
Kingdaro #2
Posted 21 December 2012 - 01:32 PM
You could probably just store your information in a table,


local database = {
  ["somePlayerName"] = {
    pin = 5555;
    balance = 100;
  };

  ["someOtherPlayerName"] = {
    pin = 4562;
    balance = 100;
  };
}

then convert and write it with textutils.serialize.


local file = fs.open('.database','w') -- the period in front of the name to make it hidden, just a habit i guess
file.write(textutils.serialize(database))
file.close()

You can load it later, unserialize, then access the player's information.


local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()

print(database.somePlayerName.pin) --> 5555
print(database.someOtherPlayerName.balance) --> 100
thatsimplekid #3
Posted 21 December 2012 - 09:23 PM
Thanks!
thatsimplekid #4
Posted 21 December 2012 - 09:36 PM
You could probably just store your information in a table,


local database = {
  ["somePlayerName"] = {
	pin = 5555;
	balance = 100;
  };

  ["someOtherPlayerName"] = {
	pin = 4562;
	balance = 100;
  };
}

then convert and write it with textutils.serialize.


local file = fs.open('.database','w') -- the period in front of the name to make it hidden, just a habit i guess
file.write(textutils.serialize(database))
file.close()

You can load it later, unserialize, then access the player's information.


local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()

print(database.somePlayerName.pin) --> 5555
print(database.someOtherPlayerName.balance) --> 100

Could i also add things/change things on the fly with this, or would i need to edit the 'local database = {}' bit
Orwell #5
Posted 21 December 2012 - 09:47 PM
You could probably just store your information in a table,


local database = {
  ["somePlayerName"] = {
	pin = 5555;
	balance = 100;
  };

  ["someOtherPlayerName"] = {
	pin = 4562;
	balance = 100;
  };
}

then convert and write it with textutils.serialize.


local file = fs.open('.database','w') -- the period in front of the name to make it hidden, just a habit i guess
file.write(textutils.serialize(database))
file.close()

You can load it later, unserialize, then access the player's information.


local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()

print(database.somePlayerName.pin) --> 5555
print(database.someOtherPlayerName.balance) --> 100

Could i also add things/change things on the fly with this, or would i need to edit the 'local database = {}' bit
Tables (like database is) can be altered by doing so:

database["userName"] = {
    pin = 1984
	balance = 99
} -- this creates a new entry for user "userName"

database["userName"].balance = -1    -- changes the balance 'variable' in the "userName" entry.