从CSV插入表格的AlaSQL不起作用

时间:2016-10-21 01:41:03

标签: javascript sql csv alasql

尝试将CS​​V数据复制到AlaSQL的内部表。 但SELECT * INTO tab FROM CSV() - 根本不起作用。在此之后没有任何改变。表tab仍为空,但直接选择正常。我做错了什么?



<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
  <div id="res1"></div>
  <div id="res2"></div>
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/alasql/0.3.3/alasql.min.js"></script>
  
	<script type="text/sql" id='sql'>
		CREATE table tab(c1 integer);
		
		select count(*) as c1
		INTO tab 
		from CSV("https://rawgit.com/thephpleague/csv/master/examples/data/prenoms.csv");
		-- tab still empty!
		
		select *
		into HTML("#res1",{headers:true})
		from tab;

		--direct select works
		select count(*) as c1
		into HTML("#res2",{headers:true})
		from CSV("https://rawgit.com/thephpleague/csv/master/examples/data/prenoms.csv");

	</script>
	<script type="text/javascript">
		alasql('SOURCE "#sql"')
	</script>

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

与文件交互会使请求异步。目前,lib无法弄清楚如何在多语句命令中等待异步语句的响应。

要解决他的问题,你可以对总声明中的每个异步chuck使用promise表示法:

<script type="text/sql" id='sql'>
    CREATE table tab(c1 integer);

    select count(*) as c1
    INTO tab 
    from CSV("https://rawgit.com/thephpleague/csv/master/examples/data/prenoms.csv");
    -- tab still empty!
</script>
<script type="text/sql" id='sql2'>
    select *
    into HTML("#res1",{headers:true})
    from tab;

    --direct select works
    select count(*) as c1
    into HTML("#res2",{headers:true})
    from CSV("https://rawgit.com/thephpleague/csv/master/examples/data/prenoms.csv");

</script>
<script type="text/javascript">
    alasql.promise(['SOURCE "#sql"','SOURCE "#sql2"'])
</script>