DART lang,FUTURE功能,链接然后

时间:2014-09-11 16:52:15

标签: dart future

我正在使用dartOracle,并希望一起使用许多SQL语句,但需要确保只在获取表创建后才执行INSERT语句,我在阅读this和{{后写了下面的代码3}}和this以及this但它不起作用......任何想法!

    var resultset;

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分钟,没有任何变化!

2 个答案:

答案 0 :(得分:2)

您永远不会致电completer.complete()。因此,result Future永远不会获得任何数据,因此.then() - 链永远不会执行。

答案 1 :(得分:0)

感谢大家,以下代码与我完美配合:

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");
  });   
}