Posted 23 July 2013 - 04:04 PM
DataBase api
Created by UnethicalClown and MKlegoman357
This API contains some basic functions for creating a database and is very useful if you are not familiar with making a database in computercraft. I will be updating this. Please tell me if you want something added or if something does not work correctly. For now all you need is a working knowledge of tables.
Functions:
Spoiler
- dbCheck(name):checks if the database or (name) exists.
-dbLoad(name)
Loads all data from the data Base.
-dbWrite(name,tbl)
Writes a table to the database.
-dbCreate(dbName)
Creates a database named (name).
-dbNewAccount(dbName,name,pass)
a function designed specially to make accounts for a login or server it will save the name and pass to the database as well as each person having his/her own ID.
Examples:
Spoiler
if dbCheck("users") == true then
dbNewAccount("users","Andrew","Andy555")
else
dbCreate("users")
dbNewAccount("users","Andrew","Andy555")
end
this code has now made a data base that contains an acount as it is the first to be added its ID is 1 its name is Andrew and its pass is Andy555 to access this data use the following
accounts = dbLoad("users")
now you can use then dataeg.
print(accounts[1][1])
print(accounts[1][2])
this will output the name and then the passwordI hope you guys find this useful =D
Pastebin link: http://pastebin.com/6MnHjV9g
Spoiler
--Created by UnethicalClown and MKlegoman357
function dbCheck(name)
if fs.exists(name) == true then
return true
else
return false
end
end
function dbLoad(name)
local temp = fs.open(name,"r")
local tmp = temp.readAll()
temp.close()
return textutils.unserialize(tmp)
end
function dbWrite(name,tbl)
local temp = fs.open(name,"w")
temp.write(textutils.serialize(tbl))
temp.close()
end
function dbCreate(dbName)
local temp = fs.open(dbName,"w")
temp.close()
end
function dbNewAccount(dbName,name,pass)
accounts = dbLoad(dbName)
if accounts == nil then
accounts = {
{
name,
pass
}
}
else
local num = #accounts+1
accounts[num] = {
{
name,
pass
}
}
end
dbWrite(dbName,accounts)
end