2013-04-09 67 views
0

我正在尝试为Android手机构建费用应用程序,并且我需要一种显示费用的方法。该计划(对于我目前的步骤)是让用户查看他们的费用。我想显示类似日历的屏幕,并且如果一天至少有一笔费用,则使用不同的颜色作为按钮。Lua - SQLite3未将行插入到其数据库中

我的问题是插入信息到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的给了我一个错误说没有一年列。我的总体猜测是,我没有正确地将信息插入表格,但我试过了我能想到的一切。谁能帮忙?

+0

SO需要一个更好的Lua解释器。 – hjpotter92 2013-04-09 00:45:41

回答

0

我想通了,不存在与sqlite3的当前版本和我有我的电脑上的问题。无论如何,我改变了几条线,它完美无瑕。我改变了选择语句和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 

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

这是所有内部的另一个用于环路创建每个dateTable[i]