Alasql: Inserting from an array

Created on 15 Feb 2015  Â·  5Comments  Â·  Source: agershun/alasql

It took me ages to find this syntax:

    // Inserting an array (from a previous query) into Product table 
    //db.tables.Product.data =  data is elegant, but how to use it with varying table names ?
    myTable = "Product";
    db.exec('INSERT INTO ' + myTable + ' SELECT * FROM ?',[data]);  // That's THE syntax !! 
    //Guessed from https://github.com/agershun/alasql/wiki/Insert

(my 2 к. - sorry if it was obvious)

! Question

Most helpful comment

You can use three different methods to insert data from array into table:

    var data = [{a:1}, {a:2}];
    alasql('CREATE TABLE one');
    // Method 1
    alasql.tables.one.data = data;
    // Method 2
    alasql('SELECT * INTO one FROM ?',[data]);
    // Method 3
    alasql('INSERT INTO one SELECT * FROM ?',[data]);

BTW: If you download data from external file, you can use this syntax:

    alaslq('CREATE one; SELECT * INTO one FROM JSON("data.json")');

All 5 comments

You can use three different methods to insert data from array into table:

    var data = [{a:1}, {a:2}];
    alasql('CREATE TABLE one');
    // Method 1
    alasql.tables.one.data = data;
    // Method 2
    alasql('SELECT * INTO one FROM ?',[data]);
    // Method 3
    alasql('INSERT INTO one SELECT * FROM ?',[data]);

BTW: If you download data from external file, you can use this syntax:

    alaslq('CREATE one; SELECT * INTO one FROM JSON("data.json")');

Is it possible to insert data from JSON into a table which has already defined columns?

alasql('CREATE TABLE test (col1 INT, col2 TEXT');
var data = [{"col1" : 1, "col2" : "String1"}, {"col1" : 2, "col2" : "String2"}];
alasql.tables.test.data = data;

I'm trying it on a localStorage database and don't get the rows from JSON into the table.

You can try this:

alasql('SELECT * INTO mytable FROM ?',[data]);

Îòïðàâëåíî ñ iPhone

21 ìàðòà 2015 ã., â 14:41, joerch80 [email protected] íàïèñàë(à):

Is it possible to insert data from JSON into a table which has already defined columns?

alasql('CREATE TABLE test (row1 INT, row2 TEXT');
var data = [{"row1" : 1, "row2" : "String1"}, {"row1" : 2, "row2" : "String2"}];
alasql.tables.test.data = data;
I'm trying it on a localStorage database and don't get the rows from JSON into the table.

—
Reply to this email directly or view it on GitHub.

After rewriting the part in my project it now works as mentioned above. Thanks for your help!
Probably I had a problem in calling the statements because it was an async operation. I'm using "promises" now.

Please, write here if you find the source of the problem!

Was this page helpful?
0 / 5 - 0 ratings