-2

I'm working on a web-app project implementing sqlite. I've ran into an issue where I want to request information from a database, but only from rows with specific indeces. The issue is that the rowid's in the table are not 1,2,3,... .

Below I've posted:

  1. code responsible for creating/populating table
  2. code console.loging the rowid's
  3. the output from (2)

Am I doing something wrong in creating the table, causing this issue? If not, is there a way to reset the rowid's of the table so that they are 1,2,3,...?

// node.js file filling the database
...
    db.serialize(function () {
      db.run(
        "CREATE TABLE IF NOT EXISTS Movies(title TEXT UNIQUE, image TEXT, description TEXT, duration TEXT, release TEXT, age TEXT, schedule TEXT)"
      );

      var stmt = db.prepare(
        "INSERT OR REPLACE INTO Movies(title, image, description, duration, release, age, schedule) VALUES (?, ?, ?, ?, ?, ?, ?)"
      );
      const jsondata = JSON.stringify(m.schedule);
      stmt.run(
        m.title,
        m.image,
        m.description,
        m.duration,
        m.release,
        m.age,
        jsondata
      );
      stmt.finalize();
    });
...
// node.js file handling requests
...
  app.get('/moviesJson', (req,res) => {
    const sql = 'SELECT rowid FROM Movies;
    db.all(sql, (err, rows) => {
      if(err) throw err;
      console.log(rows);
      res.json(rows);       
    });
  });
...
// output
[
  { rowid: 568 }, { rowid: 576 },
  { rowid: 567 }, { rowid: 562 },
  { rowid: 571 }, { rowid: 472 },
  { rowid: 575 }, { rowid: 570 },
  { rowid: 561 }, { rowid: 279 },
  { rowid: 569 }, { rowid: 574 },
  { rowid: 284 }, { rowid: 565 },
  { rowid: 451 }, { rowid: 573 },
  { rowid: 564 }, { rowid: 572 },
  { rowid: 563 }, { rowid: 566 }
]
Jord van Eldik
  • 351
  • 2
  • 10
  • 1
    Check https://stackoverflow.com/questions/5586269/how-can-i-reset-a-autoincrement-sequence-number-in-sqlite – James Apr 12 '23 at 21:30

1 Answers1

0

The SqLite documentation says:

...The normal ROWID selection algorithm described above will generate monotonically increasing unique ROWIDs as long as you never use the maximum ROWID value and you never delete the entry in the table with the largest ROWID. If you ever delete rows or if you ever create a row with the maximum possible ROWID, then ROWIDs from previously deleted rows might be reused when creating new rows and newly created ROWIDs might not be in strictly ascending order.

If the rows are only to be ever inserted and never deleted, I suggest creating the table empty to reset it to 1; otherwise you can not rely on ROWIDs being always sequential.

tinazmu
  • 3,880
  • 2
  • 7
  • 20