【问题标题】:How to create database and table using lua program in corona sdk?如何在 Corona sdk 中使用 lua 程序创建数据库和表?
【发布时间】:2011-09-13 20:27:18
【问题描述】:

我是 lua 编程新手,我不知道如何创建数据库、创建表并检索它。

我只想第一次创建数据库和表。我需要将值插入表中,并且我想从表中检索列表。

我不知道如何开始,因为我是 lua 编程的初学者。你们能帮我或建议任何教程吗?

请帮帮我

谢谢你, 马丹·莫汉

【问题讨论】:

  • 我想在一个 .lua 文件中创建它们,并希望从所需的 .lua 文件中调用它们。如何调用它们从另一个 .lua 文件中插入、检索更新和删除。

标签: sqlite lua coronasdk


【解决方案1】:

我已经关注http://wiki.garrysmod.com/?title=LUA:SQLite_Tutorial 的 sql lite 和 Lua 教程,这很好。在开始之前,您需要安装或复制 lua dlls/modules 到系统路径,以便 lua db 模块能够定位到数据库。

LuaSQL 是客户端库的不错选择..

http://www.capricorn76.com/c76_scriptengine_docsdk/luasql/index.html

【讨论】:

  • @Gopalakrishnan:谢谢老兄,我会调查一下,然后我会回复你。
  • 如何使用lua访问数据库(sqlite3)?
  • 我已完成将值插入表中,我想知道如何更新表中插入的记录。需要 tat 的语法。因为我是 Corona 的新手,我需要你方的帮助.
【解决方案2】:

您在标签中提到了 Corona,尽管您在问题中没有提到这一点。这会有所不同,因为 Corona 内置了对 SQLite 的支持,但不支持任何其他类型的数据库。

他们的网站上有关于如何使用数据库功能的文档:http://developer.anscamobile.com/content/data-storage

请注意,他们的示例都使用 [[ ]] 语法作为数据库命令,但我发现它看起来更好,使用“”更容易理解

【讨论】:

    【解决方案3】:

    这是一个老问题,但我会解释我的做法:

    1. 下载 Sqlite 浏览器 (https://github.com/sqlitebrowser/sqlitebrowser/releases)
    2. 创建一个空数据库
    3. 把它放在你的项目文件夹中
    4. 在您的代码中将其复制到 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()
        -- I dont create a Primary Key manually as Sqlite creates ROWID for us
        db:exec( "CREATE TABLE IF NOT EXISTS categories (category TEXT, description TEXT, icon_location TEXT )" )
    end
    

    您可以使用 db:exec 轻松更新和插入记录(db 是数据库的本地变量)。如果查询完成且没有任何错误,则 db:exec 返回 0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2014-01-28
      • 2013-06-04
      • 2018-01-16
      相关资源
      最近更新 更多