Why aren’t you entering the data into sqlite and how to insert images?

Asked

Viewed 63 times

0

I’m making a system, I followed all the steps to insert the data into the table using sqlite, but when I go to the Application in the part of inspecting the browser element the database remains empty. I would also like to know what to do in the code for an image to be inserted...

JS with sqlite:

    var db = openDatabase("Meubanco", "3.0", "Mybase", 6000);
    db.transaction(function(criar){
    criar.executeSql("CREATE TABLE granjas (codigo PRIMARY KEY, nome TEXT, email TEXT, cnpj TEXT)")
});

    function cadastrar(){
    var codigo = document.getElementById("codigo").value;
    var nome = document.getElementById("nome").value;
    var email = document.getElementById("email").value;
    var cnpj = document.getElementById("cnpj").value;

    db.transaction(function(armazenar){
    armazenar.executeSql("INSERT INTO granjas (codigo, nome, email, cnpj) VALUES (?,?,[email protected],?)", [codigo, nome, email, cnpj]);
});
    alert("Granja " + document.getElementById("nome").value + " Cadastrada!");
};

HTML

<form onsubmit="cadastrar();">
  <div class="form-group">
    <label>Código</label>
    <input type="text" class="form-control" id="codigo" placeholder="codigo">
  </div>
  <div class="form-group">
    <label>Nome</label>
    <input type="text" class="form-control" id="nome" placeholder="nome">
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Endereço de email</label>
    <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="email">
  </div>
  <div class="form-group">
    <label>CNPJ</label>
    <input type="text" class="form-control" id="cnpj" placeholder="cnpj">
  </div>
  <form>
  <div class="form-group">
    <label for="exampleFormControlFile1">Insira uma imagem da granja<small id="emailHelp" class="form-text text-muted">Ação Opcional.</small></label>
    <input type="file" class="form-control-file" id="imagem_da_empresa"/>
  </div>
  </form>
  <button type="button" class="btn btn-primary" onclick="cadastrar();">Cadastrar</button>

I think it’s pretty clear what I’m trying to do... Does anyone know where the mistake is?

  • is missing the data type of the "code" field in your command to create the table

  • True! but inserted here and after reloading the page continues without saving the changes in the bank.

1 answer

0

If you put the column codigo only PRIMARY KEY must enter a value in the insert. If you do not want to enter a value in the column just put the AUTOINCREMENT column

CREATE TABLE granjas (codigo INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, email TEXT, cnpj TEXT );

  • I edited here. The browser keeps saying that the table is empty after entering the data :/

Browser other questions tagged

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