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

[Lua] My DB API spawns "nil" Databases

Started by Mirodin, 14 March 2013 - 09:57 PM
Mirodin #1
Posted 14 March 2013 - 10:57 PM
Hello guys,

I started learning Lua just a week ago so please do not laugh at me if there are absurd programming defects :)/>
The last two days I spend quite a bunch of hours making my own DB Api for my Sorting System with the Interactive Sorter from MiscPeripheral.
Everything seems to work fine, but when I use my sorting script "einlesen" (the second one) CC spawns me a new DB called "nil". I know that is because of some mistake in declaring variables but I can not find the problem.

So I would appreciate it if somebody can help me out (and also for all bugfixes :rolleyes:/> )

My DB-API:

-- variables
daten = {}
werte = {}
local pfad = "data"

-- functions
function erstellen(name) -- Creates a new DB
  name = name or "data"
  if not fs.exists(pfad)then
	fs.makeDir(pfad)
	print("Database dir created!")
  end
  if not fs.exists(fs.combine(pfad, name)) then
	speichern(name)
	if name == "data" then
	  print("Standard-Database \""..name.."\" created!")
	else
	  print("Database \""..name.."\" created!")
	end
  end
  return true
end
function laden(name) -- Loads DB
  name = name or "data"
  erstellen(name)
  local file = fs.open(fs.combine(pfad, name), "r")
  local tmp = file.readAll()
  file.close()
  daten = textutils.unserialize(tmp)
  print("Database \""..name.."\" loaded!")
  return daten
end
function speichern(name) -- Saves DB
  name = name or "data"
  if not fs.exists(pfad) then
	erstellen(name)
  end
  local file = fs.open(fs.combine(pfad, name), "w")
  file.write(textutils.serialize(daten))
  file.close()
  print("Database \""..name.."\" saved!")
end
function einfuegen(name, werte) -- Inserts new data
  name = name or "data"
  laden(name)
  if type(werte) == "table" then
	for a=1, #werte do
	  table.insert(daten, werte[a])
	  print("Item "..werte[a].." in "..name.." saved!")
	end
  else
	table.insert(daten, werte)
	print("Item "..werte.." in "..name.." saved!")
  end
  speichern(name)
end

"Insert Script" aka einlesen:

os.loadAPI("db")
-- Variablen
local Args = { ... }
local menge = tonumber(Args[1])

menge = menge or 1
for m=1, menge do
  local event, item = os.pullEvent("isort_item")
  db.einfuegen("sorter", item)
end

P.S. Sorry for german text and variables but its a lot easier for me having "familiar words" in the beginning ^_^/>
If its better for you, I will try to translate them.

Cheers
Mirodin
Edited on 15 March 2013 - 08:28 AM
Lyqyd #2
Posted 15 March 2013 - 03:58 AM
Split into new topic.
JokerRH #3
Posted 15 March 2013 - 04:21 AM
Can you describe the error a littlebit more?
Maybe show the complete error and write a marker at the position in the code here (The forum doesn't show lines :(/> )
Doyle3694 #4
Posted 15 March 2013 - 05:12 AM
Havn't got the time to read through everything, but a problem might be if you try to copy a table, it will be a pointer and not a copy
Mirodin #5
Posted 15 March 2013 - 09:22 AM
Ok, I will try to explain it a little bit better. First: there is no error (message), because it is not an error. It is a bug, which spawns everytime I use the function einfuegen(name, item) (it is the forth function in the api) a new database on my HD named nil. So I am pretty sure there is some problem with handling the name argument in the api.

I made the mistake nesting the functions in the API, so it´s a bit complicated.

So this is the intended procedure:

1. function einfuegen handels the DB name and the item ID and calls
2. function laden loads the existing DB (handels name) and calls
3. function erstellen creates a new DB if there is no (handels name) and calls
4. function speichern creates an file on HD containing {} names name
5. function erstellen prints "Database name created!"
6. function einfuegen inserts the item ID in the previously loaded DB and calls
7. function speichern to write the new data in the DB name on HD

So the problem is:
First run of that cascade prints "Database name created!"
Inserting an item prints "Database nil created!", but there was no new create routine in my opinion and especially none with name = nil :(/>

I will also edit my first post to remove irrelevant parts of the scripts, sorry for that :)/>
theoriginalbit #6
Posted 15 March 2013 - 09:39 AM
I cannot seem to replicate the problem in cc-emu with the script

http://puu.sh/2hIlW
Mirodin #7
Posted 15 March 2013 - 11:46 AM
There is an emulator for CC??? :blink:/> :blink:/> :blink:/>
Awesome B)/>

Back to topic:
Hm, I messed around with the code the last two hours and somehow the problem seems to be gone, but I have no idea why…

Nevermind, thanks a lot to all of you for trying to help me

I´ll be back ;)/>
theoriginalbit #8
Posted 15 March 2013 - 11:53 AM
There is an emulator for CC??? :blink:/> :blink:/> :blink:/>
Indeed there is
Mirodin #9
Posted 15 March 2013 - 12:15 PM
Thanks man! That saves me quite a lot of time since my machine lags the hell out of it in MC because of my graphic driver…. dual boot Win7 runs MC smooth :angry:/>