How to popular an Sqlite database through a . sql file?

Asked

Viewed 123 times

1

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

Contents of the archive 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 popular an Sqlite database through a file .sql??

1 answer

0

I don’t understand of Typescript, but I believe it is a language that is converted to Javascript later (correct me if I’m wrong), so you can use native Apis like 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.