Nesta Pocket Script TutorialWhat is Nesta Pocket Script ?
The script can be used to process the database of Nesta Pocket.
Nesta Pocket handles multiple databases as the datastore.
The script supports transaction processing. Can be treated as multiple updates.
Neste Pocket Script is using the Lua programming language. Script ListList of scripts that have been created are displayed.
Create new script
To create the script, tap the + icon at the top of the screen.
Edit the scriptEnter the script code in a text editor.
Simple ExampleThis is a simple Lua program.
print("Hello World!")
Execute the scriptWill be displayed in the Output View When you run the following script.
Lua standard functionsYou can also use standard functions of the Lua. a = "one string" b = string.gsub(a, "one", "another") print(a) print(b) Nesta Pocket functions
Function list will be shown when you tap the "f(x)" on the keyboard.
Create Database
Create a new database. -- create database dbname = "sampledb" result = ndb_create(dbname) if (result ~= true) then print(result) end Remove Database
Remove the database.
-- find database
dbname = "sampledb"
if (ndb_find(dbname) == true) then
-- remove database
result = ndb_remove(dbname)
if (result ~= true) then
print(result)
end
end
New Entity
Add a new entity to create the database.
--
-- Function:
-- create database
--
function createDB(dbname)
if (ndb_find(dbname) == true) then
ndb_remove(dbname)
end
local result = ndb_create(dbname)
if (result ~= true) then
-- error
print(result)
return false
end
return true
end
--
-- Function:
-- add fields
-- Number(5), Name, English(3), Mathematics(3), Science(3)
--
function addFields(db)
local result = ndb_field_add(db, "Number", NDB_NUMBER, 0, true)
if (result ~= true) then
-- error
print(result)
return false
end
result = ndb_field_add(db, "Name", NDB_TEXT, 0, true)
if (result ~= true) then
-- error
print(result)
return false
end
result = ndb_field_add(db, "English", NDB_NUMBER, 3, true)
if (result ~= true) then
-- error
print(result)
return false
end
result = ndb_field_add(db, "Mathematics", NDB_NUMBER, 3, true)
if (result ~= true) then
-- error
print(result)
return false
end
result = ndb_field_add(db, "Science", NDB_NUMBER, 3, true)
if (result ~= true) then
-- error
print(result)
return false
end
return true
end
--
-- Function:
-- add new Entity via cursor
--
function newEntity(cursor, number, name, english, mathematics, science)
-- create new Entity
local result = ndb_cursor_new(cursor)
if (result ~= true) then
-- error
print(result)
return false
end
-- Number field
local property = ndb_property_get(cursor, "Number")
if (type(property) == "string") then
-- error
print(property)
return false
end
result = ndb_property_putnum(property, number)
if (result ~= true) then
-- error
print(result)
return false
end
-- Name field
property = ndb_property_get(cursor, "Name")
if (type(property) == "string") then
-- error
print(property)
return false
end
result = ndb_property_putstr(property, name)
if (result ~= true) then
-- error
print(result)
return false
end
-- English field
property = ndb_property_get(cursor, "English")
if (type(property) == "string") then
-- error
print(property)
return false
end
if (string.len(english) > 0) then
result = ndb_property_putnum(property, english)
if (result ~= true) then
-- error
print(result)
return false
end
end
-- Math field
property = ndb_property_get(cursor, "Mathematics")
if (type(property) == "string") then
-- error
print(property)
return false
end
if (string.len(mathematics) > 0) then
result = ndb_property_putnum(property, mathematics)
if (result ~= true) then
-- error
print(result)
return false
end
end
-- Science field
property = ndb_property_get(cursor, "Science")
if (type(property) == "string") then
-- error
print(property)
return false
end
if (string.len(science) > 0) then
result = ndb_property_putnum(property, science)
if (result ~= true) then
-- error
print(result)
return false
end
end
-- Success
return true
end
--
-- Will be executed for script from here.
--
local db = "sampledb"
if (createDB(db) == false) then
return
end
if (addFields(db) == false) then
return
end
local cursor = ndb_cursor_db(db)
if (type(cursor) == "string") then
-- error
print(cursor)
return
end
-- add entity
newEntity(cursor, 12001, "Taro Yamada", 80, 100, 95)
newEntity(cursor, 12002, "Hanako Tanaka", 95, 65, 80)
newEntity(cursor, 12003, "Ichiro Saito", 92, 54, 65)
newEntity(cursor, 12004, "Alice Goto", 100, 54, 47)
newEntity(cursor, 12005, "Lynne Richards", 84, 85, 86)
newEntity(cursor, 12006, "Rabbit Bertleman", 54, 37, "")
newEntity(cursor, 12007, "Shaun Lopez", 78, "", 63)
newEntity(cursor, 12008, "Susumu Inaba", 64, 54, 48)
newEntity(cursor, 12009, "Lisa Sano", 96, 74, 76)
newEntity(cursor, 12010, "Mark Tomson", "", 18, 0)
-- close cursor
ndb_cursor_close(cursor)
Calculate the average scoreTo add average score of English, Mathematics and Science to the new field.
--
-- Function:
-- add average field
--
function addFieldAverage(db)
ndb_field_remove(db, "Average")
local result = ndb_field_add(db, "Average", NDB_NUMBER, 3, true)
if (result ~= true) then
-- error
print(result)
return false
end
end
--
-- Function:
-- get score via cursor(field type is number)
--
function getScore(cursor, fieldname)
local property = ndb_property_get(cursor, fieldname)
if (type(property) == "string") then
-- error
print(property)
return false
end
return ndb_property_getnum(property)
end
--
-- Function:
-- To calculate average of entity.
-- Blank field is not the target of average.
--
function calcEntity(cursor)
local total_score = 0
local score_count = 0
local english = getScore(cursor, "English")
if (english == false) then
-- error
return false
end
if (english ~= nil) then
total_score = total_score + english
score_count = score_count + 1
end
local mathematics = getScore(cursor, "Mathematics")
if (mathematics == false) then
-- error
return false
end
if (mathematics ~= nil) then
total_score = total_score + mathematics
score_count = score_count + 1
end
local science = getScore(cursor, "Science")
if (science == false) then
-- error
return false
end
if (science ~= nil) then
total_score = total_score + science
score_count = score_count + 1
end
local average = 0
if (score_count > 0) then
average = total_score / score_count
-- truncate the decimal point
average = math.floor(average)
end
property = ndb_property_get(cursor, "Average")
if (type(property) == "string") then
-- error
print(property)
return false
end
result = ndb_property_putnum(property, average)
if (result ~= true) then
-- error
print(result)
return false
end
return true
end
--
-- Will be executed for script from here.
--
local db = "sampledb"
-- add average field
if (addFieldAverage(db) == false) then
return
end
local cursor = ndb_cursor_db(db)
if (type(cursor) == "string") then
print(cursor)
return
end
-- calculate average via cursor
local count = ndb_cursor_count(cursor)
for i = 1, count, 1 do
result = calcEntity(cursor)
if (result == true) then
result = ndb_cursor_next(cursor)
if (result == false) then
break
end
end
end
-- close cursor
ndb_cursor_close(cursor)
Gets the data to set the search filterDisplays the data of less than average 60 points.
--
-- Function:
-- Gets string field value via cursor.
--
function getString(cursor, fieldname)
local property = ndb_property_get(cursor, fieldname)
if (type(property) == "string") then
-- error
print(property)
return false
end
return ndb_property_getstr(property)
end
--
-- Function:
-- Gets number field value via cursor.
--
function getNumber(cursor, fieldname)
local property = ndb_property_get(cursor, fieldname)
if (type(property) == "string") then
-- error
print(property)
return false
end
return ndb_property_getnum(property)
end
--
-- Function:
-- Display Number, Name and Average of the entity.
--
function displayEntity(cursor)
-- get value of the Number field.
local number = getNumber(cursor, "Number")
if (number == false) then
return false
end
-- get value of the Name field.
local name = getString(cursor, "Name")
if (name == false) then
return false
end
-- get value of the Average field.
local average = getNumber(cursor, "Average")
if (average == false) then
return false
end
print(number .. " " .. name .. " : " .. average)
return true
end
--
-- Will be executed for script from here.
--
local db = "sampledb"
-- new query
local query = ndb_query_create(db)
if (type(query) == "string") then
-- error
print(query)
return
end
-- new search filter
local filter = ndb_filter_create(query)
if (type(filter) == "string") then
-- error
print(filter)
return
end
-- add the filter condition
-- condition: Average < 60
ndb_filter_add(filter, "Average", NDB_LT, 60)
-- add the filter condition to the query.
ndb_query_add(query, filter)
-- close filter
ndb_filter_close(filter)
-- gets the cursor that matches the filter conditions.
local cursor = ndb_cursor_query(query)
if (type(cursor) == "string") then
-- error
print(cursor)
return
end
-- close query
ndb_query_close(query)
-- cursor is target data.
local count = ndb_cursor_count(cursor)
for i = 1, count, 1 do
result = displayEntity(cursor)
if (result == true) then
result = ndb_cursor_next(cursor)
if (result == false) then
break
end
end
end
-- close cursor
ndb_cursor_close(cursor)
Nesta Pocket Script functions
Author: YAMAMOTO Naoki |