Bor*_*jev 21 sqlite android android-sqlite
我正在努力提高我的android数据库插入速度.我目前正在做的是生成一个字符串,如:
SELECT ? as title, ? as musician_id, ? as album_id, ? as genre
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
UNION SELECT ?, ?, ?, ?
Run Code Online (Sandbox Code Playgroud)
然后执行它
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);
Run Code Online (Sandbox Code Playgroud)
当我尝试插入大约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 ?, ?, ?, ?
Run Code Online (Sandbox Code Playgroud)
当我尝试插入大约200行时,一切正常.
我想这很明显 - 我试图在一个单独传递太多变量execSQL.有谁知道限制是什么,以便我可以分割我在适当的批次中插入的行?
eba*_*hea 29
该限制在sqlite3.c中进行了硬编码,并设置为999.不幸的是,它只能在编译时更改.以下是相关摘要:
/*
** 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
Run Code Online (Sandbox Code Playgroud)
Sim*_*iak 14
我正在努力提高我的android数据库插入速度.我目前正在做的是生成一个字符串,如:
你有没有想过使用TRANSACTION?我建议你用它代替你的方法.我认为使用UNION条款根本不是"胜利",并且有更好,更主要的方法来实现它.
db.beginTransaction();
try {
for (int i = 0 ; i < length ; i++ ) { // or another kind of loop etc.
// make insert actions
}
db.setTransactionSuccessful(); // now commit changes
}
finally {
db.endTransaction();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8014 次 |
| 最近记录: |