How to populate a SQLite database through a .sql file?

1

I have a .sql file and I need to run all the rows, insert all the records that are in it, into a table inside SQLite.

Content of file mydb.sql :

CREATE TABLE tblVeiculo (VeiculoId INT, Codigo [TEXT(9)], Fabricante [TEXT(255)], Modelo [TEXT(255)], AnoInicial INT, AnoFinal INT, Portas INT, Combustivel [VARCHAR(30)], NrMotorObstruido BOOLEAN);
INSERT INTO tblVeiculo (VeiculoId, Codigo, Fabricante, Modelo, AnoInicial, AnoFinal, Portas, Combustivel, NrMotorObstruido) VALUES (1, '001034066', 'AGRALE', 'MT 12.0 4X2 SB(E-TRONIC)(URB.) DIES 2P BASICO', 2005, 2013, 2, 'DIES', 1);
INSERT INTO tblVeiculo (VeiculoId, Codigo, Fabricante, Modelo, AnoInicial, AnoFinal, Portas, Combustivel, NrMotorObstruido) VALUES (2, '001034078', 'AGRALE', 'MT 12.0 4X2 LE(E-TRONIC)(RODOV.) DIES 1P BASICO', 2005, 2013, 1, 'DIES', 0);
INSERT INTO tblVeiculo (VeiculoId, Codigo, Fabricante, Modelo, AnoInicial, AnoFinal, Portas, Combustivel, NrMotorObstruido) VALUES (3, '001034080', 'AGRALE', 'MT 12.0 4X2 LE(E-TRONIC)(RODOV.C/AR) DIES 1P BASICO', 2005, 2013, 1, 'DIES', 0);
INSERT INTO tblVeiculo (VeiculoId, Codigo, Fabricante, Modelo, AnoInicial, AnoFinal, Portas, Combustivel, NrMotorObstruido) VALUES (4, '001034091', 'AGRALE', 'MT 12.0 4X2 LE(E-TRONIC)(URBANO) DIES 2P BASICO', 2005, 2013, 2, 'DIES', 0);

COMMIT TRANSACTION;
PRAGMA foreign_keys = on;

How can I populate an SQLite database through a .sql ??

    
asked by anonymous 21.02.2017 / 16:07

1 answer

0

I do not understand TypeScript , but I think it's a language that is converted to JavaScript later (correct me if I'm wrong), so you can use the native APIs as FileReader or XMLHttpRequest , if you are using cordova then you can do something like this:

function CarregarArquivo(arquivo, callback, errorCallback)
{
    var type = LocalFileSystem.PERSISTENT;
    var size = 5*1024*1024;

    window.requestFileSystem(type, size, successCallback, errorCallback);

    function successCallback(fs)
    {
        fs.root.getFile(arquivo, {}, function(fileEntry)
        {
            fileEntry.file(function(file)
            {
                var reader = new FileReader();

                reader.onloadend = function(e) {
                    callback(this.result);
                };

                reader.readAsText(file);

            }, errorCallback);
        }, errorCallback);
    }
}

CarregarArquivo('file.sql', function(fullquery) {
     //Aqui pode usar o split para separar por ';'
     var queries = fullquery.split(';');

     for (var i = 0, queries.length; i < j; i++) {
         //algo como executeQuery(queries[i].trim());
     }
}, function(erro) {
    alert(erro);
});

If it comes from a file selected via input you can use something like:

function executeQueries(fullquery) {
     //Aqui pode usar o split para separar por ';'
     var queries = fullquery.split(';');

     for (var i = 0, queries.length; i < j; i++) {
         //algo como executeQuery(queries[i].trim());
     }
}

//Selecione o elemento do <input type="file" id="sqlfile">
document.getElementById("sqlfile").addEventListener("change", function(e) {
     readFile(evt.target.files[0], executeQueries); //Mesma função
}, false);
    
21.02.2017 / 16:47