Corona SDK:将本地Sqlite数据库转换为Json字符串,然后使用Php添加到数据库


Corona SDK: Convert local Sqlite database to Json string then use Php to add to database

我有一个看起来像这样的lua文件:

display.setStatusBar( display.HiddenStatusBar )
-- Add onscreen text
local label1 = display.newText( "SQLite demo", 20, 30, native.systemFontBold, 60 )
label1:setTextColor( 190, 190, 255 )
local label2 = display.newText( "Creates or opens a local database", 20, 100, native.systemFont, 40 )
label2:setTextColor( 190, 190, 255 )
local label3 = display.newText( "(Data is shown below)", 20, 140, native.systemFont, 40 )
label3:setTextColor( 255, 255, 190 )
--Include sqlite
require "sqlite3"
--Open data.db.  If the file doesn't exist it will be created
local path = system.pathForFile("data.db", system.DocumentsDirectory)
db = sqlite3.open( path )
--Handle the applicationExit event to close the db
local function onSystemEvent( event )
        if( event.type == "applicationExit" ) then
            db:close()
        end
end
--Setup the table if it doesn't exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS test (content, content2);]]
print(tablesetup)
db:exec( tablesetup )
--Add rows with a auto index in 'id'. You don't need to specify a set of values because we're populating all of them
testvalue = {2}
local id = system.getInfo("deviceID")
testvalue[1] = id
local time_stamp = os.time()
testvalue[2] = time_stamp

local tablefill =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[['); ]]
db:exec( tablefill )

--print the sqlite version to the terminal
print( "version " .. sqlite3.version() )
--print all the table contents
for row in db:nrows("SELECT * FROM test") do
  local text = row.content.." "..row.content2
  local t = display.newText(text, 20, 160 + (60 * row.id), native.systemFont, 40)
  t:setTextColor(255,0,255)
end
--setup the system listener to catch applicationExit
Runtime:addEventListener( "system", onSystemEvent )

所有这些操作都是将设备的唯一 ID 和时间戳存储在本地 sqlite 数据库中。我需要获取此信息并将其放入 json 字符串中。我还有一个简单的本地主机数据库,称为"testdata",有两列;"id"和"time"。我需要获取我的 json 字符串并使用 php 文件将信息插入我的数据库。

我没有使用php的经验,所以我发现这很困难。

谢谢!

这篇博文可能对您有价值:

http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/

本周的Corona Geek也对此进行了一些详细的讨论:

http://www.youtube.com/watch?v=4J9A6M3Ii-A

相关文章: