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

Database Api

Started by UnethicalClown, 23 July 2013 - 02:04 PM
UnethicalClown #1
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 data

eg.

print(accounts[1][1])
print(accounts[1][2])
this will output the name and then the password



I 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
Parmacoy #2
Posted 23 July 2013 - 06:34 PM
Still lacking alot of features from the quick read i had, but keep it up, from my experience, the less interaction someone has to do with computercraft, the better. Why not focus on getting functionality like mysql, eg

SELECT * FROM test WHERE foo = '$bar'
that means SELECT ALL FROM <table/tables> WHERE <Field> = variable

im sure if you added in advanced search functionality / sorting, it would be a very good program, nice work so far
UnethicalClown #3
Posted 24 July 2013 - 12:07 AM
Still lacking alot of features from the quick read i had, but keep it up, from my experience, the less interaction someone has to do with computercraft, the better. Why not focus on getting functionality like mysql, eg

SELECT * FROM test WHERE foo = '$bar'
that means SELECT ALL FROM <table/tables> WHERE <Field> = variable

im sure if you added in advanced search functionality / sorting, it would be a very good program, nice work so far

thanks man you have given me quite a few Ideas indeed
UMayBleed #4
Posted 25 July 2013 - 01:26 PM
Neat! Although I am working on a Database already for computercraft using legitiment MySQL commands, like It parses stuff like "SELECT * FROM `cc`.`users` WHERE `username` = 'UMayBleed';" It is really complicated compared to this. you have functions for each command, unlike me, where I use a single function for all commands.
Great work though!

Also 1 question, How would you insert a row? you have
"-dbWrite(name,tbl)
Writes a table to the database."

But that makes a table, so how would you insert a row?
UnethicalClown #5
Posted 26 July 2013 - 08:51 AM
I realize how stupid this API is ATM but I am still working on it so ideas is what I need also it might take a while to release this is like the prototype or alpha version as well as being kinda just for people who don't know how to work with tables
Thib0704 #6
Posted 26 July 2013 - 09:13 AM
This is a good idea, But is missing features.