livecode从sqlite检索记录

时间:2014-01-03 21:20:59

标签: database sqlite livecode

我在sqlite中有一个表,表名为birthday,行有> id,name,day,month。 我从我的数组中获取系统日期并将其转换为此日期/日期。 我正在使用andre的dblib来检索它,就像下面的代码一样

 on mouseUp
  put the system date into tdate
   convert tDate to short system date
convert tDate to dateitems 
  --put (the item 3 of tDate) & "/" & (the item 2 of tDate) into field "birthday"
  put (the item 3 of tDate) & "/" & (the item 2 of tDate) into today

  dbcolumns "day,month"
  put dbGet("giortes") into myRecords
  repeat for each key tKey in myRecords
    put myRecords[tKey]["day"] &"/"& myRecords[tKey]["month"] & cr after myGiortes[1]
  end repeat
put 0 into check
put false into verify
repeat for each line tempItem in myGiortes[1]
   add 1 to t
   if tempItem = today then
      put true into check
      exit repeat
   end if
end repeat
if check then
  --!! test
  put today into fld tfld

end if

end mouseUp

所以我得到的结果就像 12/08 一样。 是否可以将结果与sqlite中的行匹配? 我已经重现了上面的代码。 我从数组中获取结果并将其与转换的系统日期相匹配。 现在我想从数据库中获取所有Row;)

2 个答案:

答案 0 :(得分:1)

您的代码剪辑非常简短。例如,如果您发布了dbGet函数,它可能会有所帮助。你确定myRecords是一个阵列吗?您可以使用

进行检查
if myRecords is an array then

您不清楚使用哪个行和列分隔符,但我认为它们是换行符和制表符。您可以执行以下操作:

dbcolumns "day,month"
put dbGet("giortes") into myRecords
if myRecords is not an array then split myRecords by return and tab
repeat for each key tKey in myRecords
  put myRecords[tKey]["day"] &"/"& myRecords[tKey]["month"] & cr after myGiortes[1]
end repeat

显然,这只有在myRecords不是数组才是罪魁祸首时才有效。如果您的SQL语法有问题,myRecords可能为空或包含不同的密钥。

如果myRecords实际上是一个错误而你只是想找到哪一天= 12和月= 8的tkey,你可以调整你的重复循环:

repeat for each key tKey in myRecords
  if myRecords[tKey]["day"] is 12 and myRecords[tKey]["month"] is 8 then
    put myRecords[tKey]["day"] &"/"& myRecords[tKey]["month"] & cr after myGiortes[1]
  end if
end repeat

您也可以使用数据库中正确的SQL语法直接执行此操作:

put "SELECT * FROM giortes WHERE day='12' AND month='8'" into mySQL
// I start variables with "my", it isn't a reference to MySQL
put revDataFromQuery(cr,tab,gDatabaseID,mySQL) into myGiortes
if myGiortes begins with "revdberr" then
  beep
  answer error myGiortes
else
  split myData by cr and tab
end if

您还需要与revOpenDatabase建立连接。为此,您需要知道SQLite文件的路径。

put revOpenDatabase("sqlite",mySQliteFilePath,,,) into gDatabaseID

在此行之后,您可以运行上一个代码段。将gDatabaseID声明为脚本顶部的全局变量。

更新1

如果您更喜欢使用Andre的数据库,可以使用dbWhere命令:

set the itemDel to slash
put item 1 of the date into myMonth
put item 2 of the date into myDay
dbColumns "day,month,id" // I have added id
dbWhere "month",myMonth
dbWhere "day",myDay
put dbGet("giortes") into myGiortes
dbResetQuery

这应足以回答您的问题。你不再需要重复循环了。我只是想知道为什么你会想要这个,因为现在你只得到一个列表:

1  12,8
2  12,8
3  12,8
etc

如果您想知道记录数,可能需要使用SQLite的COUNT函数。

更新2

我更改了“Update 1”下的代码。如果您确实只是在寻找或今天过生日的人的身份证明,那么您可以使用dbColumns "id"代替dbColumnds "day,month,id"。这会更有意义,因为我之前示例中的示例数据是无用的。如果运行修改后的代码,则应获得与

类似的数据
1  12,8,1
2  12,8,11
3  12,8,23
etc

即。最后一列包含ID号。您也可以只检索ID号。如果你这样做,你得到

1  1
2  11
3  23
etc

答案 1 :(得分:1)

如果您要查找当前用户的生日,则将当前用户的id添加到'myUserId'变量中,并使用Andre的框架中的以下dbWhere子句

dbWhere "id",myUserId

当然,您可能正在努力完成不同的事情?