question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

peewee.OperationalError: too many SQL variables PLUS

See original GitHub issue

Hi there:

I’m using peewee to insert files into SQLite, I meet this error: peewee.OperationalError: too many SQL variables . I tried Google, some said it’s because the SQLITE_MAX_VARIABLE_NUMBER.

So i searched and using this code to get the SQLITE_MAX_VARIABLE_NUMBER.

def max_sql_variables():
	"""Get the maximum number of arguments allowed in a query by the current
	sqlite3 implementation. Based on `this question
	`_

	Returns
	-------
	int
		inferred SQLITE_MAX_VARIABLE_NUMBER
	"""
	import sqlite3
	db = sqlite3.connect(':memory:')
	cur = db.cursor()
	cur.execute('CREATE TABLE t (test)')
	low, high = 0, 100000
	while (high - 1) > low: 
		guess = (high + low) // 2
		query = 'INSERT INTO t VALUES ' + ','.join(['(?)' for _ in range(guess)])
		args = [str(i) for i in range(guess)]
		try:
			cur.execute(query, args)
		except sqlite3.OperationalError as e:
			if "too many SQL variables" in str(e):
				high = guess
			else:
				raise
		else:
			low = guess
	cur.close()
	db.close()
	return low

SQLITE_MAX_VARIABLE_NUMBER = max_sql_variables()

And i get the value is 999. Then I tried the following steps:

... code omit...
for _fileList in _list:
	print "Insert %s Records" % len( _fileList )
	FileLite.fromFileList( _fileList )

(1) I split my file list to 400, But it still said peewee.OperationalError: too many SQL variables (2) I split my files list to 100,it still said peewee.OperationalError: too many SQL variables (3) I split my files list into 80, It’s OK! (4) I increase the value to 84, It seems OK and insert some data, but when the loops goes on, the peewee.OperationalError: too many SQL variables throws, HOW COULD THIS HAPPEN?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
vadimkantorovcommented, Feb 12, 2019

It would be great if peewee could automatically batch up inserts upon the insert_many call (peewee is in a better position to count accurately the number of used SQL variables)

0reactions
coleifercommented, Jun 26, 2018

As far as I can tell this is not a Peewee issue, but running into limits imposed by SQLite.

Read more comments on GitHub >

github_iconTop Results From Across the Web

peewee.OperationalError: too many SQL variables on upsert ...
After some investigation, the problem appears to be related with the maximum number of parameters that a sql query can have: SQLITE_MAX_VARIABLE_NUMBER.
Read more >
How to increase SQLITE_MAX_VARIABLE_NUMBER at ...
I'm using Python and the Peewee module to manage a Sqlite3 database. ... questions/35616602/peewee-operationalerror-too-many-sql-variables ...
Read more >
peewee.OperationalError: too many SQL variables - 华为云社区
使用peewee+sqlite批量插入数据报错peewee.OperationalError: too many SQL variables ...
Read more >
Is there a way to create a parameterized query or stored ...
OperationalError "too many SQL variables" when selecting rows from given range · How to solve ... It shouldn't make too much of a...
Read more >
herman - PyPI
a little fork of peewee. ... peewee.OperationalError: no such column: t2.name ... DataError: Too many instances matching query exist: SQL: SELECT p1.id, p1....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found