如何将数据库查询结果中的值存储到机器人框架中的变量中[重复]

Posted

技术标签:

【中文标题】如何将数据库查询结果中的值存储到机器人框架中的变量中[重复]【英文标题】:How to store a value from database query result into a Variable in Robot Framework [duplicate] 【发布时间】:2020-03-11 13:04:30 【问题描述】:

我已经编写了机器人框架代码来运行数据库查询并记录查询结果

Connect To Database pymysql $Database_name    $UserName $Password $DatabaseHost $Port 

Check If Exists In Database SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId    

@QueryResult  Query   SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId

Log Many    @QueryResult                      

查询结果

(56, 'Party Pack (Serves 6-8)', 6, 1, 1, 1, 'Afghani Chicken Tikka Biryani (Heavy Eater)', 0, 11)   
(58, 'Party Pack (Serves 6-8)', 6, 1, 1, 3, 'Chicken Tikka Biryani (Heavy Eater)', 0, 4)    
(61, 'Party Pack (Serves 6-8)', 6, 1, 1, 5, 'Paneer Makhani Biryani (Heavy Eater)', 0, 18)  
(79, 'Party Pack (Serves 6-8)', 6, 1, 1, 74, 'Afghani Veg Biryani (Heavy Eater)', 0, 10)    
(90, 'Party Pack (Serves 6-8)', 6, 1, 1, 89, 'Butter Chicken Biryani (Heavy Eater)', 0, 0)  
(253, 'Party Pack (Serves 6-8)', 6, 1, 1, 250, 'Classic Hyderabadi Chicken Biryani (Heavy Eater)', 0, 0)    
(255, 'Party Pack (Serves 6-8)', 6, 1, 1, 252, 'Classic Hyderabadi Veg Biryani (Heavy Eater)', 0, 15)   
(339, 'Party Pack (Serves 6-8)', 6, 1, 1, 325, 'Awadhi Veg Biryani (Heavy Eater)', 0, 26)   
(340, 'Party Pack (Serves 6-8)', 6, 1, 1, 326, 'Awadhi Chicken Biryani (Heavy Eater)', 0, 0)    
(381, 'Classic Chicken Tikka Roll ', 1, 1, 0, 408, 'Malabari Paratha', 0, 191)  
(383, 'Signature Chicken Seekh Kebab Roll ', 1, 1, 0, 408, 'Malabari Paratha', 0, 191)

请任何人建议我如何将特定行的值存储在变量中

谢谢

【问题讨论】:

假设这些行只是列表项,因此将结果变量视为类似列表的对象。 我对机器人特别陌生,所以请你能准确地说我应该如何继续这个,如果我想存储来自@QueryResult 的第一行的 3 列的假设值 在运行时。因为显示的这些值不是固定的。 是的,感谢托多的帮助 【参考方案1】:

数据库中的每一行基本上都作为一个元组返回。要访问特定行,请使用索引。

Connect To Database pymysql $Database_name    $UserName $Password $DatabaseHost $Port 

Check If Exists In Database SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId    

@QueryResult  Query   SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId

$firstRow     Set Variable   $QueryResult[0]
$secondRow    Set Variable   $QueryResult[1]

在上面的示例中,$firstRow 将包含值 (56, 'Party Pack (Serves 6-8)', 6, 1, 1, 1, 'Afghani Chicken Tikka Biryani (Heavy Eater)', 0, 11)$secondRow 将包含值(58, 'Party Pack (Serves 6-8)', 6, 1, 1, 3, 'Chicken Tikka Biryani (Heavy Eater)', 0, 4)

【讨论】:

非常感谢,它对我有用,我现在仍然需要弄清楚如何从该行值中选择一列。 我想选择单行和单列就像 - $firstRow Set Variable $QueryResult[0] [0] 再次感谢您的帮助。 是的,@TanyaGrover。 $QueryResult[0][0] 会给你第一行和第一列。

以上是关于如何将数据库查询结果中的值存储到机器人框架中的变量中[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何将从“选择”查询中获得的值存储到 ASP.net 中的会话变量中

如何将查询结果中的值转换为变量和变量值?

如何将 HKStatisticsQuery 的结果返回到 Swift 2 中的变量?

如何将 HKStatisticsQuery 的结果返回到 Swift 2 中的变量?

VB.NET是如何使用ADO让存储过程返回数据表中的值呢?求解!

如何查看数据库中的结果是不是与我在数组中的值匹配