2013-04-09 66 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]