Nesta Pocket スクリプト入門Nesta Pocket スクリプトとは ?
Nesta Pocket はデータの登録と検索に対応したカード型データベースのアプリです。
Nesta Pocket は複数のデータベースをデータストアとして管理しています。
トランザクション処理に対応していますので複数の更新をまとめて行えます。
Neste Pocket はスクリプト言語としてシンプルで高速な Luaを内蔵しています。
Lua から Nesta Pocket のデータストアにアクセスするための関数が用意されています。
スクリプト一覧
作成されたスクリプトが一覧として表示されます。 新規スクリプトの作成
スクリーン上部にある + アイコンをタップすることでスクリプト名を入力するビューが表示されます。 スクリプトの編集
スクリプトをキーボードを使用して編集します。 簡単なスクリプト一番簡単な Luaプログラムです。 print("Hello World!") スクリプトの実行
スクリーン右下にあるプレイアイコンをタップすることでスクリプトが実行されます。 Lua 標準関数スクリプト内で Lua が用意している標準関数を使用することが可能です。 a = "one string" b = string.gsub(a, "one", "another") print(a) print(b) Nesta Pocket 関数
キーボードの f(x) をタップすることで Nesta Pocket にアクセスするための関数の一覧が表示されます。 データベースの新規作成
データベースを新規に作成するスクリプトです。 -- create database dbname = "sampledb" result = ndb_create(dbname) if (result ~= true) then print(result) end データベースの削除
データベースを削除するスクリプトです。 -- find database dbname = "sampledb" if (ndb_find(dbname) == true) then -- remove database result = ndb_remove(dbname) if (result ~= true) then print(result) end end 新規エンティティの作成
データベースにフィールドを設定してエンティティを追加します。 -- -- 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 -- 学籍番号(5), 氏名, 英語(3), 数学(3), 科学(3) -- function addFields(db) local result = ndb_field_add(db, "学籍番号", NDB_NUMBER, 0, true) if (result ~= true) then -- error print(result) return false end result = ndb_field_add(db, "氏名", NDB_TEXT, 0, true) if (result ~= true) then -- error print(result) return false end result = ndb_field_add(db, "英語", NDB_NUMBER, 3, true) if (result ~= true) then -- error print(result) return false end result = ndb_field_add(db, "数学", NDB_NUMBER, 3, true) if (result ~= true) then -- error print(result) return false end result = ndb_field_add(db, "科学", 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, "学籍番号") 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, "氏名") 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, "英語") 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, "数学") 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, "科学") 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 -- -- スクリプトはここから実行されます。 -- 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, "山田 太郎", 80, 100, 95) newEntity(cursor, 12002, "田中 花子", 95, 65, 80) newEntity(cursor, 12003, "斉藤 一郎", 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) フィールドの追加平均のフィールドを追加して、すべてのエンティティの英語、数学、科学の平均点を計算して出力します。 -- -- Function: -- add average field -- function addFieldAverage(db) ndb_field_remove(db, "平均") local result = ndb_field_add(db, "平均", 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, "英語") 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, "数学") 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, "科学") 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, "平均") 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 -- -- スクリプトはここから実行されます。 -- 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) 検索条件を指定したデータ抽出平均が60点未満のデータを表示します。 -- -- 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, "学籍番号") if (number == false) then return false end -- get value of the Name field. local name = getString(cursor, "氏名") if (name == false) then return false end -- get value of the Average field. local average = getNumber(cursor, "平均") if (average == false) then return false end print(number .. " " .. name .. " : " .. average) return true end -- -- スクリプトはここから実行されます。 -- 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, "平均", 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 スクリプト関数
Author: YAMAMOTO Naoki |