11 posts
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
1688 posts
Location
'MURICA
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
11 posts
Posted 21 December 2012 - 09:23 PM
Thanks!
11 posts
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
1054 posts
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.