|
我的代码是$ p7 k) \" Z# J6 `: o1 G# N7 e
ContentValues values; ) z7 S9 t' h) }3 ]" I
values = new ContentValues();
5 o6 |) W, i' V" q; Z values.put(SQLHelper.EMPLOYEE_LPN, jsObj.getString("lpn"));- j6 X9 t1 l* ^3 i2 {' s3 c
db.update(SQLHelper.EMPLOYEE_TABLE, values,
8 e" ~5 z5 L" Y$ j* }" i "EMPLOYEE_LPN ='" + jsObj.getString("lpn") + "'",
4 p0 b* e( p& p null);
w* J, v" A9 m) F在Log Cat中显示警告2 D" b6 t. g J2 r
08-31 15:19:45.297: WARN/Database(2868): Reached MAX size for compiled-sql statement cache for database /data/data/org.sipdroid.sipua/databases/test.db; i.e.,
/ j7 {. T7 j! [( m9 aNO space for this sql statement in cache:
) r" B- z3 S z) T1 {: iSELECT EMPLOYEE_NAME FROM eyemployee WHERE EMPLOYEE_LPN ='1169162'. 4 r h0 ?8 P2 Q6 D
Please change your sql statements to use '?' for bindargs, instead of using actual values% T1 s3 |2 p6 L9 N
怎么解决呢?+ D& t. H- |" h! {
* E' W2 x- z! o# |- O解决方案:8 N( T3 f2 I) F; a
' y7 h; _9 K7 B; j7 o9 y" ]; Y' z' p% `: C8 V0 X' T, P
! c# }& |' ?7 w6 D 在这里查看示例8-3和8-4 。
5 ~5 E; C! G7 `2 Z7 \2 y示例8-3 使用更新方法8 B" m7 U& d, z& x$ V, ]/ Y$ {
/**+ f' {0 [, c3 Y3 H, X
* Update a job in the database.3 h8 K' x; ?' |6 I
* @param job_id The job id of the existing job
% K$ S g4 A( `) {1 | { * @param employer_id The employer offering the job: j8 R: o6 b# F3 h5 r& S
* @param title The job title; c/ G- Y+ b. V: x& ]
* @param description The job description: s' j% l5 I' C
*/* U7 B" E$ o% _/ @
public void editJob(long job_id, long employer_id, String title, String description) {
5 y: r9 h- c; W$ j; o1 V' ?( l k ContentValues map = new ContentValues();
' z5 ]! m& { i, h7 h map.put("employer_id", employer_id);; T2 l( }: k( X g
map.put("title", title);; t5 d4 @" f8 W( ]' ~8 S/ U! i0 D
map.put("description", description);
) S9 u0 T9 s' `+ C1 R String[] whereArgs = new String[]{Long.toString(job_id)};
K( F7 \- ?: K' m7 l, E! [ try{1 v, [' ]0 [, J+ J, S9 Z
getWritableDatabase().update("jobs", map, "_id=?", whereArgs);! m& n( S( G, _# J0 Y0 e. z( j; H$ `; t
} catch (SQLException e) {* f% C( Z: `6 ]. v
Log.e("Error writing new job", e.toString());
9 `$ C+ D5 O6 [, f# S' n9 m% J }
6 F* q4 O* C" p! W1 q! t* E6 ?# U}( l7 ?2 X# W: t, h- e! w
以下是示例8-3中的一些代码亮点:9 ?3 \5 O/ m3 W. F
例8-4显示了如何使用execSQL方法。# w; u9 R8 F: F; ~5 y
示例8-4 使用execSQL方法* y& a4 g2 x" s+ C7 a* T2 q
/**
\) i6 G5 z X+ T* x9 r" h0 _" V * Update a job in the database./ q/ w6 c, m) w
* @param job_id The job id of the existing job4 Z: f5 n, }/ K7 q8 g7 R" I
* @param employer_id The employer offering the job
' f, m: J4 v; Y' G6 q; c: d * @param title The job title& y, z* ?" G0 D' l' [2 l* q" N0 M$ q
* @param description The job description: ?8 R" ]$ w4 D5 H0 E$ [" w( p
*/
6 u, H! X2 y9 p: e4 Z e) Upublic void editJob(long job_id, long employer_id, String title, String description) {
* |- p) G, N- p4 E$ N" G( V String sql =
! m, m4 @3 A- n "UPDATE jobs " +
3 B3 x7 S7 {. Z$ J7 t, F9 x3 }. d# { "SET employer_id = ?, "+
0 P: V2 ^5 a/ @5 O3 ?; F " title = ?, "+6 h, R: ]% F0 i" O9 K, J3 y
" description = ? "+
% Z& h+ G( x& z/ i5 u "WHERE _id = ? ";/ l. F) W Q; W n6 p
Object[] bindArgs = new Object[]{employer_id, title, description, job_id};; l1 C. b7 }4 ^3 D
try{6 H% z! v$ ^6 @9 Y! U6 W
getWritableDatabase().execSQL(sql, bindArgs);+ p5 a; ~- N+ k; w( @) _
} catch (SQLException e) {# l0 C2 p0 }2 F4 g) N; n
Log.e("Error writing new job", e.toString());) ~1 F# J( t6 S0 R
}
7 w0 l+ H$ s$ d3 J1 y3 x5 _# _/ C}" t9 T' a$ X+ {1 N1 e7 a! B( r
% x! x: |% \7 `9 s8 p! D
该消息要求您使参数使用sql变量而不是sql文字。
% {5 z$ X2 p9 p# r" b( v$ e解析每个sql查询,生成计划,并将其存储在sql语句缓存中。0 s0 C1 ^4 B3 Y/ l+ Q
从缓存中获取具有相同文本的查询。9 _" f; N5 T# @; w7 k
--One query
+ E! T, L2 P$ q" p7 h& `' u+ n9 _! `SELECT * FROM Customers WHERE Id = @1 (@1 = 3); v7 ~. P# m, {& g2 ]
SELECT * FROM Customers WHERE Id = @1 (@1 = 4)+ `$ K; A, A/ C+ l; Y
SELECT * FROM Customers WHERE Id = @1 (@1 = 5)
7 B7 x A# T1 t; d5 W4 W8 m在缓存中找不到具有不同文本(包括文字)的查询,并已(无用地)将查询添加到了该查询中。
) h. Z( ~- z6 {" v4 m) v4 {& \ --Three Queries.+ ?9 m7 m% F3 ?( h, Q
SELECT * FROM Customers WHERE Id = 3
/ A8 r R5 J8 p1 K- \$ fSELECT * FROM Customers WHERE Id = 4- {+ ~& S9 p- n8 _4 f7 J
SELECT * FROM Customers WHERE Id = 5 |
|