通过外部函数使用返回值

时间:2017-08-16 08:12:35

标签: sqlite react-native

我正在尝试实现这个函数,帮助我在外部使用select的结果,但是setState函数不起作用。那我该怎么办呢?

selectFromTable(table_name, selected_columns, conditions) {
        console.log('selecting data...')
        let command = new SelectCommand();
        let db_cmd = command.select_from_table(table_name, selected_columns, conditions); //db_cmd = select * from Person for exemple

        var that = this;
        db.transaction((txn) => {
            txn.executeSql(db_cmd, [],  (tx, res) => {
                that.setState({select_res, res});
            });
        });
        return this.state.select_res;
    }

4 个答案:

答案 0 :(得分:2)

试试这个:

selectFromTable(table_name, selected_columns, conditions, callback) {
    return new Promise(function(resolve,reject) {

        let command = new SelectCommand();
        let db_cmd = command.select_from_table(table_name, selected_columns, conditions);

        db.transaction((txn) => {
            txn.executeSql(db_cmd, [], (tx, res) => {
                resolve(JSON.parse(JSON.stringify(res)));
            });
        }); 
    });
}

然后,使用它:

db_cmd.selectFromTable('Agriculteur', ['*'], null).then((result) => {
  //here do what you want with the results
})

答案 1 :(得分:1)

你可以试试这个:

selectFromTable(table_name, selected_columns, conditions, callback) {
    console.log('selecting data...')
    let command = new SelectCommand();
    let db_cmd = command.select_from_table(table_name, selected_columns, conditions);

    let result = new Promise((resolve, reject) => {
        db.transaction((txn) => {
            txn.executeSql(db_cmd, [], (tx, res) => {
                resolve(res);
            });
        });
    });
    result.then((res) => {
        return res;
    });
}

答案 2 :(得分:0)

一个想法是使用promises或callbacks。我期待db.transaction需要一些时间才能返回结果。因为操作是异步的,所以在返回的那一刻,你将不会在select_res中产生结果:)

你可以试试这个:

selectFromTable(table_name, selected_columns, conditions, callback) {
    console.log('selecting data...')
    let command = new SelectCommand();
    let db_cmd = command.select_from_table(table_name, selected_columns, conditions);

    let result = new Promise((resolve, reject) => {
        db.transaction((txn) => {
            txn.executeSql(db_cmd, [], (tx, res) => {
                resolve(res);
            });
        });
    });
    result.then((res) => {
        return res;
    });
}

答案 3 :(得分:0)

这是我的承诺或回调的解决方案

<强>回调

selectFromTable(table_name, selected_columns, conditions, callback) {
        console.log('selecting data...')
        let command = new SelectCommand();
        let db_cmd = command.select_from_table(table_name, selected_columns, conditions);

        var that = this;
        db.transaction((txn) => {
            txn.executeSql(db_cmd, [],  (tx, res) => {
                callback(res)
            });
        });
    }

当我尝试使用它时,我使用

db_cmd.selectFromTable('Person', ['*'], null, function(res) {
            console.log(res);
        });

<强>承诺

async selectFromTable(table_name, selected_columns, conditions, callback) {
        console.log('selecting data...')
        let command = new SelectCommand();
        let db_cmd = command.select_from_table(table_name, selected_columns, conditions);

        var result = await db.transaction(async (txn) => {
            var select_res = await txn.executeSql(db_cmd, [],  (tx, res) => {
                select_res = res;
                return select_res;
            });
            result = select_res;
            return result;
        });
        return result;
    }

当我尝试使用它时,我使用

create_data_base = async () => {
        let db_cmd = new DB_Command();
        var res = await db_cmd.selectFromTable('Agriculteur', ['*'], null).then((x) => {
            return x;
        });
        console.log(res);
    }

因此,如果我在此代码中有任何问题,请帮助我。