【问题标题】:DART lang, FUTURE function, chain of thenDART lang,FUTURE 函数,then 链
【发布时间】:2014-09-11 16:52:14
【问题描述】:

我正在使用 dartOracle,想同时使用多个 SQL 语句,但需要确保只有在创建表后才执行 INSERT 语句,我在阅读 thisthis 后编写了以下代码和thisthis 但它不起作用.. 任何想法!

var 结果集;

Future buildDB() {
     var completer = new Completer(); 
     print("Hello, from Future!");
     return completer.future; 
}  

void createTables() {
   Future result= buildDB();

    connect(
       "SYSTEM",
       "password",
       "(DESCRIPTION="
       "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
       "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
  .then(  
  (oracleConnection) {

  result
        .then((_) => resultset = oracleConnection.select('''
                        CREATE TABLE vendors (
                                     vendor_id NUMBER,
                                     vCode NUMBER,
                                     vName VARCHAR(255),
                                     vEmail VARCHAR(255),
                                     PRIMARY KEY (vendor_id))
                      '''))   
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (1,'code1','name1','email1')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (2,'code2','name2','email2')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (3,'code3','name3','email3')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => print('tables created!'));  
  }, 
  onError: (error) {
    print("Failed to create tables, error found: $error");
  });   
}

一旦我执行了这个函数,我就会得到这个:

Observatory listening on http://127.0.0.1:54590
 Hello, vendor!
 Hello, from Future!
 Listening for GET and POST on http://127.0.0.1:8004

之后什么都没有发生,我等了 5 分钟,没有任何变化!

【问题讨论】:

  • 请包含您看到的错误。我想反对票是因为我们不知道出了什么问题。
  • 没有出现错误!刚刚得到这个,没有别的了。天文台在127.0.0.1:54590收听你好,供应商!你好,来自未来!在127.0.0.1:8004上监听 GET 和 POST
  • 如果您只是想测试,请执行以下操作:Future buildDB => new Future.value(true);,它会返回一个带有值的未来。稍后,您可以换成真正的实现。
  • 感谢@SethLadd,它的作用是'Future buildDB() => new Future.value(true);'并稍后称为“buildDB().then”,因为我只需要在我的应用程序中使用一个虚拟的未来函数来确保 SQL 语句以正确的顺序执行,这满足了我的需求:)

标签: dart future


【解决方案1】:

你永远不会打电话给completer.complete()。所以你的result Future 永远不会得到任何数据,因此.then()-chain 永远不会执行。

【讨论】:

【解决方案2】:

谢谢大家,下面的代码非常适合我:

pubspec.yaml:

dependencies:
  oracledart: any

main.dart 文件:

import 'dart:async';
import 'package:oracledart/oracledart.dart';

void main() {
   Future buildDB() => new Future.value(true);     // dummy Future function to ensure SQL statements done in the proper sequence
   print("Hello to vendor tables setup!");
   var vendors = <Map>[];

   connect(
    "SYSTEM",
    "password",
    "(DESCRIPTION="
      "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
      "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
.then(  
  (oracleConnection) {

  buildDB()
        .then((_) => print('Pls wait, sequence of SQL statements will be executed now'))
        .then((_) => oracleConnection.select("""
                    CREATE TABLE vendors (
                                 vendor_id NUMBER,
                                 vCode NUMBER,
                                 vName VARCHAR(255),
                                 vEmail VARCHAR(255),
                                 PRIMARY KEY (vendor_id))
                     """))
        .then((_) => print('table had been created, now will start inserting initial availabe data!'))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (1,101,'vName1','vEmail1@email.com')"))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (2,102,'vName2','vEmail2@email.com')"))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (3,103,'vName3','vEmail3@email.com')"))
        .then((_) => print('data had been inserted, now will run a SELECT statement to show you what had been inserted!'))
        .then((_) {
            var resultset = oracleConnection.select("select * from vendors");
            while(resultset.next()) {
                 print("hello this is: ${resultset.getStringByName('VNAME')}");
                  vendors.add({"code":"${resultset.getStringByName('VCODE')}",
                    "name": "${resultset.getStringByName('VNAME')}",
                    "email": "${resultset.getStringByName('VEMAIL')}"
                  });
               }              
                print('the data entered is:  $vendors'); 
              })
        .then((_) => print('Done, SQL statement completed!'));
  }, 
  onError: (error) {
    print("Failed to connect: $error");
  });   
}

【讨论】:

  • @Robert 你的回答很好,对于我的第一个函数,但是 Seth 方法更容易,因为我只需要一个虚拟的未来,仅此而已,感谢你的努力和时间,以及有用的反馈,不幸的是可以此处不接受 2 个答案
猜你喜欢
  • 2020-06-19
  • 1970-01-01
  • 2020-10-02
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多