如何通过Lua程序在Corona SDK中创建数据库和表?
2011-6-13 11:13:8
收藏:0
阅读:173
评论:3
我刚开始学习Lua编程,还不知道如何创建数据库,包括创建表格和检索数据。我想创建一个数据库,并且只需在第一次运行时创建表格。我需要将值插入表格中,并从表格中检索这些值列表。因为我是Lua编程的初学者,不知道该如何入手。请大家帮忙或建议我可以参考哪些教程。谢谢!Madan Mohan
原文链接 https://stackoverflow.com/questions/6329653
点赞
stackoverflow用户686008
你在标签里提到了 Corona,尽管你的问题中没有提到。这很重要,因为 Corona 内置支持 SQLite,但不支持其他类型的数据库。
他们的网站上有关于如何使用数据库功能的文档:http://developer.anscamobile.com/content/data-storage
请注意,他们的示例都使用 [[ ]] 语法进行数据库命令,但我发现使用“ ”更美观且更易于理解。
2011-06-14 11:45:39
stackoverflow用户2524586
这是一个老问题,但我将解释我的做法:
- 下载Sqlite Browser(https://github.com/sqlitebrowser/sqlitebrowser/releases)
- 创建一个空的数据库
- 将它放在你的项目文件夹中
- 在你的代码中将其复制到system.DocumentsDirectory中:
local function copyDatabase()
util.copyFile( "model.db", system.ResourceDirectory, db_name, system.DocumentsDirectory, false )
end
util.copyFile:
function M.copyFile( srcName, srcPath, dstName, dstPath, overwrite )
local results = false
local srcPath = M.doesFileExist( srcName, srcPath )
if ( srcPath == false ) then
return nil -- nil = source file not found
end
--check to see if destination file already exists
if not ( overwrite ) then
if ( M.doesFileExist( dstName, dstPath ) ) then
return 1 -- 1 = file already exists (dont overwrite)
end
end
--copy the source file to the destination file
local rfilePath = system.pathForFile( srcName, srcPath )
local wfilePath = system.pathForFile( dstName, dstPath )
local rfh = io.open( rfilePath, "rb" )
local wfh = io.open( wfilePath, "wb" )
if not ( wfh ) then
print( "writeFileName open error!" )
return false
else
--read the file from 'system.ResourceDirectory' and write to the destination directory
local data = rfh:read( "*a" )
if not ( data ) then
print( "read error!" )
return false
else
if not ( wfh:write( data ) ) then
print( "write error!" )
return false
end
end
end
results = 2 -- 2 = file copied successfully!
--clean up file handles
rfh:close()
wfh:close()
return results
end
打开数据库的方法:
local function openDatabase()
local path = system.pathForFile( db_name, system.DocumentsDirectory )
db = sqlite3.open( path )
end
创建一些表
local function createTables()
-- 我不手动创建主键,因为Sqlite为我们创建了ROWID
db:exec( "CREATE TABLE IF NOT EXISTS categories (category TEXT, description TEXT, icon_location TEXT )" )
end
你可以使用db:exec(db是对数据库的本地变量)轻松地更新和插入记录。如果查询完成且没有任何错误,db:exec将返回0。
2015-09-19 20:04:51
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
我已经按照 http://wiki.garrysmod.com/?title=LUA:SQLite_Tutorial 进行了SQLite & Lua教程,效果不错。在开始之前,你需要安装或复制lua dlls / 模块到系统路径,以便lua db模块能够找到数据库。
LuaSQL 是客户端库的好选择。
http://www.capricorn76.com/c76_scriptengine_docsdk/luasql/index.html