gpt4 book ai didi

database - livecode 从 sqlite 中检索记录

转载 作者:搜寻专家 更新时间:2023-10-30 23:10:41 25 4
gpt4 key购买 nike

我在 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 中的一行相匹配吗?我已经重现了上面的代码。我从数组中获取结果并将其与转换后的系统日期相匹配。现在我想从数据库中获取所有行 ;)

最佳答案

您的代码非常简短。例如,如果您发布了 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 实际上是一个错误,而您只想找到 day=12 和 month=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

我已经更改了“更新 1”下的代码。如果您确实只是在查找今天生日的人的 ID,那么您可以使用 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

关于database - livecode 从 sqlite 中检索记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com