首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >corona能打开MySQL DB吗?

问corona能打开MySQL DB吗?
EN

Stack Overflow用户
提问于 2014-03-31 08:56:08
回答 1查看 1.8K关注 0票数 1

我只是想知道,如果MySQL可以用于日冕,或者MySQL db可以用sqlite3打开,或者如果我使用MySQL db或sqlite3 db有什么区别吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-31 09:54:52

Corona为连接到SQLite驱动程序提供了自己的库,这里可以访问这些驱动程序的文档。

代码语言:javascript
复制
local sqlite3 = 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 (id INTEGER PRIMARY KEY, 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
local testvalue = {}
testvalue[1] = 'Hello'
testvalue[2] = 'World'
testvalue[3] = 'Lua'
local tablefill =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[2]..[['); ]]
local tablefill2 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[2]..[[',']]..testvalue[1]..[['); ]]
local tablefill3 =[[INSERT INTO test VALUES (NULL, ']]..testvalue[1]..[[',']]..testvalue[3]..[['); ]]
db:exec( tablefill )
db:exec( tablefill2 )
db:exec( tablefill3 )

--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, 30 * row.id, null, 16)
    t:setFillColor( 1, 0, 1 )
end

--setup the system listener to catch applicationExit
Runtime:addEventListener( "system", onSystemEvent )

您还可以使用第三方库(或电池)创建连接。其中一个常用的库是LuaSQL。

Corona社区上的一个线程向您展示了如何建立连接将LuaSQL用于MySQL服务器。

代码语言:javascript
复制
local luasql = require "luasql.mysql";
env = assert((luasql.mysql()), "Uh oh, couldn't load driver")
conn = assert(env:connect("database","username","password","localhost"), "Oops!!")
cur = assert (conn:execute ("SELECT * from table_1" ))
row = cur:fetch ({}, "a")
while row do
  print ("\n------ new row ---------\n")
  table.foreach (row, print)
  row = cur:fetch (row, "a")
end
cur:close()
conn:close()
env:close()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22757394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档