SQLite执行报错:sqlite3.IntegrityError数据类型不匹配问题求助
datatype mismatch Error in Your Student Table Insert Hey there, let's break down why you're hitting that sqlite3.IntegrityError: datatype mismatch error and how to fix it quickly.
The Root Cause
Looking at your table definition, you set studentid as an INTEGER PRIMARY KEY. In SQLite, this column has strict type rules: it must hold integer values—not strings.
But in your INSERT statement, you’re passing "a" (a string) for the studentid field. That’s a direct conflict with the column’s data type requirement, which triggers the integrity error.
Two Simple Fixes
Pick the approach that fits your use case:
Let SQLite auto-generate the student ID (Recommended)
SinceINTEGER PRIMARY KEYin SQLite automatically acts as an auto-incrementing column (it’s tied to the table’s internal ROWID), you don’t need to manually specify a value for it. Just omit thestudentidfield in your INSERT:cursor.execute(''' INSERT INTO students(forename,surname,currentgrade,targetgrade) VALUES ("b","1","c","d"); ''')SQLite will handle assigning a unique, incrementing integer to each new student automatically.
Manually specify an integer student ID
If you need to setstudentidyourself, make sure you pass an integer (not a string). For example:cursor.execute(''' INSERT INTO students(studentid,forename,surname,currentgrade,targetgrade) VALUES (1,"b","1","c","d"); ''')Here, we replaced
"a"with1(an integer), which matches the column’s data type rule perfectly.
Quick Side Note
Even though SQLite is generally flexible with data types, INTEGER PRIMARY KEY columns are an exception—they enforce strict integer typing because they’re linked to the table’s underlying ROWID system. This ensures the column stays a reliable, unique identifier.
内容的提问来源于stack exchange,提问作者Jack Burgess

