【问题标题】:Get the actual array of rows from SQL query with sqlite and Expo使用 sqlite 和 Expo 从 SQL 查询中获取实际的行数组
【发布时间】:2019-07-13 06:52:00
【问题描述】:

我正在尝试从 sqlite 查询结果对象中获取整个结果作为行数组:

我已经尝试过results.rows.item(i).id,它工作得很好,但我需要获取整个行数组而不是一个项目,我尝试使用 Expo 文档中的 rows.array (_number),但我不知道实际如何使用.array(_number)

我的代码 sn-p:

iris.transaction(tx => {
    tx.executeSql(
        `select id, date from days`,
        [],
        (tx, results) => {
            for(i=0; i<results.rows.length; i++){
                var date = results.rows.item(i).date.split('-');
            }
        }
    )
});

【问题讨论】:

    标签: javascript reactjs sqlite react-native expo


    【解决方案1】:

    根据Expo documentresult.rows 有一个_array,它将所有项目作为数组返回,你可以试试

    if (result && result.rows && result.rows._array) {
      /* do something with the items */
      // result.rows._array holds all the results.
    }
    

    希望这会有所帮助!

    【讨论】:

    • 作为 TypeScript 用户的补充说明,TypeScript 似乎真的不喜欢 SQLResultSetRowList - 当我尝试使用 rows._array 时,我得到一个数组说属性 _array 不存在于类型 rows 上。因此,如果您遇到这种情况,只需将 rows 对象重新分配给一个新变量并将其转换为类型 any - 您将失去验证,但您不会再遇到烦人的错误!
    • @ArmandoVasquez 谢谢,它有效。你真的应该考虑在答案中写这个。
    【解决方案2】:

    TypeScript 解决方案在不使用任何东西的情况下正确转换 _array ;)

    tx.executeSql("SELECT * FROM ITEMS", [], (_, { rows }) => {
      const r = rows as unknown as { _array: MyItem[] };
      result = r._array;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-17
      • 2012-03-26
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      相关资源
      最近更新 更多