【问题标题】:Lua - SQLite3 isn't inserting rows into its databaseLua - SQLite3 没有在其数据库中插入行
【发布时间】:2013-04-09 00:03:56
【问题描述】:

我正在尝试为 Android 手机构建一个费用应用程序,我需要一种显示费用的方法。计划(对于我当前的步骤)是允许用户查看他们的费用。我想显示一个类似日历的屏幕,如果一天至少有一笔费用,则为按钮使用不同的颜色。

我的问题是在sqlite3 表中插入信息。这是我的代码:

require "sqlite3"

--create path
local path = system.pathForFile("expenses.sqlite", system.DocumentsDirectory )
file = io.open( path, "r" )
if( file == nil )then           
    -- Doesn't Already Exist, So Copy it In From Resource Directory                          
    pathSource = system.pathForFile( "expenses.sqlite", system.ResourceDirectory )  
    fileSource = io.open( pathSource, "r" ) 
    contentsSource = fileSource:read( "*a" )                                  
    --Write Destination File in Documents Directory                                  
    pathDest = system.pathForFile( "expenses.sqlite", system.DocumentsDirectory )                 
    fileDest = io.open( pathDest, "w" )                 
    fileDest:write( contentsSource )                 
     -- Done                      
    io.close( fileSource )        
    io.close( fileDest )         
end
db = sqlite3.open( path )

--setup the table if it doesn't exist
local tableSetup = [[CREATE TABLE IF NOT EXISTS expenses (id INTEGER PRIMARY KEY, amount, description, year, month, day);]]
db:exec(tableSetup)
local tableFill = [[INSERT INTO expenses VALUES (NULL,']] .. 15 .. [[',']] .. "Groceries" .. [[',']] .. 2013 .. [[',']] .. 4 .. [[',']] .. 8 ..[[');]]
db:exec(tableFill)

for row in db:nrows("SELECT * FROM expenses") do
        print("hi")
        if row.year == dateTable[i].year and row.month == dateTable[i].month and row.day == dateTable[i].day then
            flag = dateTabel[i].day
        end
end

我到处查看是否使用了错误的 sqlite3 命令,因为我对它不是很熟悉,但是我尝试了所有找到的方法,但没有任何效果。 print("hi") 行不执行,所以这告诉我表中没有行。

另外,如果我说db:nrows("SELECT year, month, day FROM expenses"),sqlite3 会给我一个错误,说没有年份列。我的总体猜测是我没有将信息正确地插入表格中,但我已经尝试了我能想到的一切。有人可以帮忙吗?

【问题讨论】:

  • SO 需要一个更好的 Lua 解释器。

标签: database sqlite calendar lua coronasdk


【解决方案1】:

我发现当前版本的 sqlite3 和我计算机上的版本存在问题。无论如何,我改变了几行,它完美无缺。我更改了 select 语句和 for 循环。

        --sqlite statement
        local check = "SELECT DISTINCT year, month, day FROM expenses WHERE year = '"..dateTable[i].year.."' AND month = '"..dateTable[i].month.."' AND day = '"..dateTable[i].day.."'"

        --check if there is at least one expense for a given day
        --if there isn't one, the for loop won't execute
        for row in db:nrows(check) do
            flag = row.day
        end

如果天数等于标志变量,我继续创建一个具有不同颜色的按钮。

这都在另一个 for 循环中,该循环创建每个 dateTable[i]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 2011-02-16
    • 2022-11-20
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多