我正在尝试提高Android数据库插入的速度。目前我所做的是生成一个字符串,例如:
INSERT INTO mytable (column1, column2) VALUES ('value1', 'value2')
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行时,出现了以下错误:
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行时一切正常。
我想这显而易见——我试图在单个execSQL
中传递太多变量。有人知道限制是多少,以便我可以将要插入的行拆分为适当的批次吗?