DART lang,FUTURE函数,链 [英] DART lang, FUTURE function, chain of then

查看:316
本文介绍了DART lang,FUTURE函数,链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dartOracle,并且想要使用许多SQL语句在一起,但需要确保INSERT语句只有在创建表后执行,我写下面的代码,阅读 this 但它不工作..任何想法!

I'm using dartOracle, and wanted to use many SQL statements together, but need to make sure the INSERT statement is executed only after getting the table created, I wrote the below code after reading this and this and this and this but it is not working.. any thought!


var resultset;

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

一旦我执行该函数,我得到:

once I execute the function, I get this:

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

and nothing happen after that, I waited for 5 minutes, with no changes!

推荐答案

pubspec.yaml:

pubspec.yaml:

dependencies:
  oracledart: any

main.dart文件:

the main.dart file:

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

这篇关于DART lang,FUTURE函数,链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆