Tuesday, 4 April 2017

Copying data from one DB to another with node-sqlite - formatting the 'insert' statement

I'm writing a small utility to copy data from one sqlite database file to another. Both files have the same table structure - this is entirely about moving rows from one db to another.

My code right now:

let tables: Array<string> = [
        "OneTable", "AnotherTable", "DataStoredHere", "Video"
    ]

tables.forEach((table) => {
    console.log(`Copying ${table} table`);

    sourceDB.each(`select * from ${table}`, (error, row) => {
        console.log(row);
        destDB.run(`insert into ${table} values (?)`, ...row) // this is the problem
    })
})

row here is a js object, with all the keyed data from each table. I'm certain that there's a simple way to do this that doesn't involve escaping stringified data.



via NiloCK

No comments:

Post a Comment