可以在单个execSQL查询中指定的SQL变量有什么限制 [英] What is the limit of SQL variables one can specify in a single execSQL query

查看:156
本文介绍了可以在单个execSQL查询中指定的SQL变量有什么限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高我的android数据库插入的速度.我目前正在做的是生成一个像这样的字符串:

I am trying to improve the speed of my android database inserts. What I am currently doing is generate a string like:

SELECT ? as title, ? as musician_id, ? as album_id, ? as genre
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?

然后使用

SQLiteDatabase database = //initialized in some way
String insertQuery; // the string of the query above
String [] parameters; // the parameters to use in the insertion.
database.execSQL(insertQuery.toString(), parameters);

当我尝试插入大约2000行时出现以下错误:

I am getting the following error when I try to insert about 2000 rows:

Caused by: android.database.sqlite.SQLiteException: too many SQL variables (code 1): , while compiling: INSERT INTO songs (title, musician_id, album_id, genre)
SELECT ? as title, ? as musician_id, ? as album_id, ? as genre
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?

当我尝试插入约200行时,一切正常.

When I try to insert about 200 rows everything works fine.

我想这很明显-我试图在一个execSQL中传递太多变量.有人知道限制是什么,以便我可以按适当的批次拆分插入的行吗?

I suppose it is obvious - I am trying to pass in too many variables in a single execSQL. Does anyone know what is the limit so that I can split the rows I insert in appropriate batches?

推荐答案

该限制在中硬编码sqlite3.c 并设置为999.不幸的是,可以更改它,但只能在编译时更改.以下是相关片段:

The limit is hardcoded in sqlite3.c and is set to 999. Unfortunately it can be changed but only at compile time. Here are the relevant snippets:

/*
** The maximum value of a ?nnn wildcard that the parser will accept.
*/
#ifndef SQLITE_MAX_VARIABLE_NUMBER
# define SQLITE_MAX_VARIABLE_NUMBER 999
#endif


/*
** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
** than 32767 we have to make it 32-bit.  16-bit is preferred because
** it uses less memory in the Expr object, which is a big memory user
** in systems with lots of prepared statements.  And few applications
** need more than about 10 or 20 variables.  But some extreme users want
** to have prepared statements with over 32767 variables, and for them
** the option is available (at compile-time).
*/
#if SQLITE_MAX_VARIABLE_NUMBER<=32767
typedef i16 ynVar;
#else
typedef int ynVar;
#endif

这篇关于可以在单个execSQL查询中指定的SQL变量有什么限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆