sqlite3: [uncaught application error]: TypeError - Invalid CString pointer, not valid UTF-8

Getting this error all of a sudden.

I wonder if maybe some non-utf8 strings got into my db? I donj’t know how to fix.

Here’s the query:

  .get('/recent', async (context) => {
    const links = await context.state.db
      .prepare(
        'SELECT url, title, excerpt, created_at, count FROM urls ORDER BY created_at DESC LIMIT 50'
      )
      .all();

    context.response.body = links;
  })

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Most upvoted comments

When passing strings to SQLite, we first encode it to a buffer and pass that to bind function. It is possible that since we do not retain any reference to that buffer after calling bind function, it’s getting freed by GC once in a while and when the query is actually run, the data then read from that buffer is corrupted. To overcome that, I have added some measure to retain references to such things so GC cannot free them. @npinochet Would you mind trying the module from https://raw.githubusercontent.com/denodrivers/sqlite3/cstr-fix/mod.ts to see if that fixes the corruption issue? This branch contains that new bind reference code.

Oh if those are unix timestamps - they need to be retrieved as 64 bit integers, while we get 32 bit by default because 64 bit integers make up for BigInts and causes pretty significant performance hit. Try setting db.int64 = true