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

api help

Started by Creeper9207, 18 February 2015 - 10:04 PM
Creeper9207 #1
Posted 18 February 2015 - 11:04 PM
os.unloadAPI("dbapi")
os.loadAPI("dbapi")
dbapi.setValue("dbfile", 1, ".-.")
dbapi.getValue("dbfile", 1)
if value == nil then
print("noluck")
else
print(value)
end

^tries to access api, always returns nil/noluck

vAPI

–Creeper9207 Code: database (API)–
tDB = {}
function setValue(database, key, value)
local tFile = fs.open(database, "w")
table.insert(tDB, key, value)
tFile.write(textutils.serialize(tDB))
tFile.flush()
tFile.close()
end
function getValue(tDatabase, tKey)
local tFile = fs.open(tDatabase, "r")
local tData = tFile.readAll()
tRealData = textutils.unserialize(tData)
value = tRealData[tKey]
tFile.close()
end
Lyqyd #2
Posted 18 February 2015 - 11:06 PM
They're in different function environments. You should be returning values from the API and putting the return values into local variables in your code.
Creeper9207 #3
Posted 18 February 2015 - 11:09 PM
how would i do that?
MKlegoman357 #4
Posted 18 February 2015 - 11:14 PM
The problem here is that whatever global variable you set inside the API after you load it is not that easily accessible. Even though it is still possible to access it you shouldn't have to. The right way here would be to return your value from the function:


local function getName ()
  return "Adam" --# return any value you want here
end

local name = getName() --# catch that value and put it into a variable

print( name ) --> Adam
Edited on 18 February 2015 - 10:16 PM
Quintuple Agent #5
Posted 18 February 2015 - 11:15 PM
At the end of your functions just do 'return [thisVar]' and when you are calling the api function do myVar=thisapi.myCode() to set
example with your code:
your api

function getValue(tDatabase, tKey)
local tFile = fs.open(tDatabase, "r")
local tData = tFile.readAll()
tRealData = textutils.unserialize(tData)
value = tRealData[tKey]
tFile.close()
return value
end
and your main script

os.unloadAPI("dbapi")
os.loadAPI("dbapi")
dbapi.setValue("dbfile", 1, ".-.")
local value=dbapi.getValue("dbfile", 1)
if value == nil then
print("noluck")
else
print(value)
end
Creeper9207 #6
Posted 18 February 2015 - 11:15 PM
ok thanks guys
InDieTasten #7
Posted 18 February 2015 - 11:20 PM
how would i do that?
Just add in the line return tRealData at the end of your function getValue.
Then you just have to use your getValue function like: local variable = dbapi.getValue(…)

Edit: Striking everything since there were replies I didn't noticed beforehand -.-
Edited on 18 February 2015 - 10:21 PM
Creeper9207 #8
Posted 19 February 2015 - 01:52 AM
api:

--Creeper9207 Code: database (API)--
tDB = {}
function setValue(database, key, value)
  local tFile = fs.open(database, "w")
  table.insert(tDB, key, value)
  tFile.write(textutils.serialize(tDB))
  tFile.flush()
  tFile.close()
end
function getValue(tDatabase, tKey)
  local tFile = fs.open(tDatabase, "r")
  local tData = tFile.readAll()
  tRealData = textutils.unserialize(tData)
  local value = tRealData[tKey]
  tFile.close()
  return value
end
Main script: (part that errors)

--Creeper9207 Code: database editor terminal--
os.unloadAPI("dbapi")
os.loadAPI("dbapi")
running = 1
args = { ... }
thefile = args[2]
if args[2] then
if args[1] == "write" then
print("Editing " .. args[2])
while true do
  if running == 1 then
  print("Enter key to modify or type 'exit' to exit")
  io.write("dbTerm> ")
  a = io.read()
  if a == "exit" then
	shell.run("shell")
	running = 0
  else
  print("Enter a value")
  io.write("dbTerm> ")
  b = io.read()
  dbapi.setValue(args[2], a, B)/>/>
  print(a .. " set to " .. b .. " in " .. args[2])
  end
end
end
elseif args[1] == "read" then
  print("Reading " .. args[2])
  while true do
  if running == 1 then
	print("Enter key to view or 'exit' to exit")
	io.write("dbTerm> ")
	g = io.read()
	if g == "exit" then
	shell.run("shell")
	running = 0
	else
    value = dbapi.getValue(args[2], g)
	sleep(0.01)
	print(value)
	end
	end
	end
else
  print("usage: dbTerm <read/write> <database file>")
end
else
print("usage: dbTerm <read/write> <database file>")
end
one that works:

os.unloadAPI("dbapi")
os.loadAPI("dbapi")
value = dbapi.getValue("datanew", 19)

print(value)
database:

{
  [ 19 ] = "seventeen",
}

dbapi.getValue() fails in the main script for NO KNOWN REASON
Edited on 19 February 2015 - 12:53 AM
InDieTasten #9
Posted 19 February 2015 - 01:53 AM
What does the error message tell? Or is just not working, but finishes back to shell without any error?
Creeper9207 #10
Posted 19 February 2015 - 01:55 AM
no error, just does not work, (gives a nil value)
Edited on 19 February 2015 - 12:55 AM
InDieTasten #11
Posted 19 February 2015 - 01:58 AM
at which arguments to your mainscript?
Creeper9207 #12
Posted 19 February 2015 - 02:03 AM
dbapitest read datanew

all other argments work, just not read
InDieTasten #13
Posted 19 February 2015 - 02:04 AM
What do you type in for g as it asks for a key to review ?
Creeper9207 #14
Posted 19 February 2015 - 02:07 AM
19
InDieTasten #15
Posted 19 February 2015 - 02:10 AM
Oh, well, I think the problem is in the type of g. As you read it in, you are asking your api for the key "19" as a string, rather than 19 as a number. You would have to tonumber it, or have your information indexed after strings

Also, your writing will not work, as you are setting data to a capital B, rather than uncap b ;)/>
Creeper9207 #16
Posted 19 February 2015 - 02:17 AM
So g.toInt()? And why would I work in set value and not in get?

And writing does work I wrote the database with it

I think something wired happened when I posted it

Plus it doesn't error
InDieTasten #17
Posted 19 February 2015 - 02:18 AM
To get your input converted to a number, you just do tonumber(g)
Creeper9207 #18
Posted 19 February 2015 - 02:24 AM
Any Idea why writing worked w/ o convo?

Well hey it worked thx man
Lyqyd #19
Posted 19 February 2015 - 02:27 AM
Threads merged. Please stick to just one topic for all questions concerning a given piece of code.
HPWebcamAble #20
Posted 19 February 2015 - 02:44 AM
In Lua, its tonumber(thing)
Edit: Ninjad


Maybe this is why:
Also ninjad. But still a concern.
(around line 21 in the main script)

b = id.read()
dbapi.setValue(args[2], a, B)/> --# you use a capital 'b' here but lowercase for read()
Edited on 19 February 2015 - 01:46 AM