Insert Data from Memo into SQLite Database


#1

Hi boys,

I’m in trouble… when I compile my code, it goes to a function to insert Data from input/textarea to a SQLite database. It goes just before making the SQL query and then get out the function without announcing any of my Alert(JSON.stringify(error)); …

What do you think of that ?
There is my alert if you follow step by step the process to create a memo and save it.

  1. Here is the menu, so you press “Nouveau memo” (it means “new memo”)
  2. Then you arrive on the New Memo editor page. I complete the title input and the textarea with “Test”. I’m going to press the back-arrow at the top on the left

ALERTS RESULTS :
function goBack() : Back button press --> “Enter in goBack function if id == null…” --> “Going to INSERT query” --> function loadMemos : “Select result: {“rows”:{length":0},“rowsAffected”:0}

Without any errors…

Source code :

private goBack(showBadge: boolean = true)
  {
    if (this.memo.id == null) {

      alert("Enter in goBack function if id == null...")

      // Calling the existing database
      this.storage.create({
        name: "MemoPlus.db",
        location: "default"

      }).then((db: SQLiteObject) =>
      {
        alert("Going to INSERt query");
        db.executeSql('INSERT INTO PAGE (title, content) VALUES (?, ?)', [this.memoInfo.title, this.memoInfo.content]).then((data) => {

          alert("INSERTED error " + JSON.stringify(data));
          alert("Insert into page title content OK !");

          this.returnID(this.memoInfo.title);
        }, (error) => {

          alert("Report Error inserting table page title content : " + error);
          alert("ERROR: " + JSON.stringify(error.err));
        });
      }, (error) => {

          alert("ERROR: " + JSON.stringify(error.err));
          alert("DB error : " + error);
      });
    }
    else
    {
      alert("Memo already existing so memoupdate function")
      this.memoService.memoUpdate(this.memo);
    }

    // Redirect on HomePage
    this.navCtrl.setRoot(HomePage);

    // ShowBadge (toast) is a little pop-up during 3 seconds, to tell the user that the memo has been saved (optional)
    if (showBadge) {
      let toast = this.toastCtrl.create({
        message: 'Memo saved',
        duration: 3000
      });
      toast.present();
    }
  }

if needed :

public returnID(title: string ) : any 
  {
    alert("Enter into returnID function");
    this.storage.create({
      name: "MemoPlus.db",
      location: "default"
    }).then((db: SQLiteObject) => 
    {
      db.executeSql('SELECT id FROM page WHERE title = ?', title).then((data) => {
        let temp = JSON.stringify(data);
        alert("selected ID of the page where title = ? !!");
        return temp['id'];
      }, (error) => {
        console.log("ERROR: " + JSON.stringify(error.err));
        alert("Report Error select id FROm page where title = ?: " + error);
      });
    });
  }

and loadMemos :

public loadMemos()
  {
    // Calling the existing database (MemoPlus.db)
    this.storage.create
    ({
        name: "MemoPlus.db",
        location: "default"
    }).then((db: SQLiteObject) => 
    {
      // SQL Query to SELECT all the data (Memos) that the table "page" contains
      db.executeSql("SELECT * FROM page", []).then((data) => 
      {
          alert ("Select result: " + JSON.stringify(data));

      }, (error) => {
        // In case of an error while trying to SELECT the data from the database, return the error
        alert("Unable to execute SQL query: " + error);
        alert(JSON.stringify(error));
      });        
    }, (error) => {
      // In case of an error while trying to open the database, return the error
      alert("Unable to open database: " + error);
      alert("ici bug...: " + JSON.stringify(error));
    });
  }

tell me if you need more information.
Thanks in advance.


#2

Add noImplicitAny to the compiler options in your tsconfig.json. Modify your code so there are absolutely no instances of any and all functions have typed return values. If you don’t figure out what is going on during this process, at least at the end of it others will be able to understand what you are expecting to be happening.


#3

I found the problem.

I was using an entities folder to declare my Database’s tables but the HTML didn’t understand the variable needed, even if it was called in the linked typescript file.

It’s a bit strange, anyway, thanks for your answer and time :slight_smile:


#4
this.sqlite.create(this.options).then((db: SQLiteObject) => { 
      let data = [this.i_ID , this.i_name , this.amount];
      console.log(data);
      db.executeSql("INSERT INTO cart (item_No, item_Name,Qty) VALUES (? , ? , ?) ", data).then((data) => {
        console.log(JSON.stringify(data));
      })
      .catch(e => console.log(e));
});

This is my query. After execute it gives this result.

{"rows":{"length":0},"rowsAffected":1,"insertId":2}

But data is not inserted into database. What could be the reason?