sqljocky querying database synchronously

后端 未结 1 1613
面向向阳花
面向向阳花 2021-01-15 11:13

I am trying to query a MySQL database synchronously with sqljocky. I have a Load class that runs the query and gets the data then imports the data into a

相关标签:
1条回答
  • 2021-01-15 11:55

    My guess is you want to do something like this (Note untested code):

    Future<DBObject> load(DBObject object, String id) {
       var query = "select ... where id='$id'"; // Note: check for SQL injection.
       return pool.query(query)
        .then((result) => result.toList())
        .then((list) => list.forEach((row) => object.import(row)))
        .then((_) => object);
    }
    

    Have you read this article about using Futures?

    The key point is if a method is asynchronous, and the calling code needs to wait for it to complete, then it must return a Future object (or a Stream in some cases). There is no way in Dart to get a function to "block" waiting for an asynchronous result. Feel free to ask some more questions in the comments.

    0 讨论(0)
提交回复
热议问题