Skip to content

SQLite Query Returns Empty Results When pageid is numpy.int32 #7

@SH-1216

Description

@SH-1216

When the pageid parameter in the _get_page_info method is of type numpy.int32, the SQLite query returns no results (len(results) == 0), even when matching records exist in the database.

Steps to Reproduce:

1.Create a SQLite database with a pages table:

import sqlite3
conn = sqlite3.connect("test.db")
c = conn.cursor()
c.execute("CREATE TABLE pages (pageid INTEGER)")
c.execute("INSERT INTO pages VALUES (1)")
conn.commit()

2.Query the table using a numpy.int32 value:

import numpy as np
pageid = np.int32(1)
c.execute("SELECT * FROM pages WHERE pageid = ?", [pageid])
results = c.fetchall()
print(len(results))  # Outputs 0

3.Compare with a Python int:

pageid = 1
c.execute("SELECT * FROM pages WHERE pageid = ?", [pageid])
results = c.fetchall()
print(len(results))  # Outputs 1

Expected Behavior:
The query should return matching records (e.g., len(results) == 1) regardless of whether pageid is a Python int or numpy.int32.

Actual Behavior:
When pageid is numpy.int32, the query returns an empty result set (len(results) == 0).

Environment:

Python: 3.12.4
NumPy: 1.26.4
SQLite: 3.45.3

Possible Cause:
The sqlite3 module may not properly bind numpy.int32 values to the query parameter, causing a type mismatch with the database column.

Suggested Fix:
Convert pageid to a Python int before executing the query:

def _get_page_info(self, pageid):
    pageid = int(pageid)  # Convert to Python int
    columns = self._get_pages_columns()
    conn = sqlite3.connect(self.db_path)
    # same as before
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions