|
我的代码是7 m3 h+ E" o* s, o& l# E
ContentValues values; # L% @9 t. S8 `* H- ]" d3 D
values = new ContentValues();3 h2 ]" B6 i/ _+ f" `/ m0 _5 z
values.put(SQLHelper.EMPLOYEE_LPN, jsObj.getString("lpn"));6 l7 ]8 S/ A& b) `2 g' H' W
db.update(SQLHelper.EMPLOYEE_TABLE, values,
, x7 Y9 A7 a# b) s "EMPLOYEE_LPN ='" + jsObj.getString("lpn") + "'",6 Z$ ~5 k' P0 n4 k
null);
/ x" u6 Y, }2 M. v! ]在Log Cat中显示警告
0 d; T1 ^+ n. b( I08-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.,
3 ^& s) Q2 ?# s2 o7 gNO space for this sql statement in cache:
; e' l1 K1 _; h* `) ESELECT EMPLOYEE_NAME FROM eyemployee WHERE EMPLOYEE_LPN ='1169162'. g* t6 \) }" e3 r* X
Please change your sql statements to use '?' for bindargs, instead of using actual values3 E/ E& K, O- U- s4 J
怎么解决呢?
3 {* r5 k2 H, l6 j ) o' Z3 Y2 k" R* `: z& e) U
解决方案:4 \% i6 e Q' G) [1 T" B7 F
5 `8 q# _9 u) Z* q
3 f& F8 o2 i- W9 `" `+ N
5 @# U- ]: q% {$ ^( R 在这里查看示例8-3和8-4 。3 Y8 {6 M. F% C1 k# c7 k
示例8-3 使用更新方法: J# @- l6 u* ^3 S9 G+ S
/**
+ `( }$ ?5 c! y2 c" T * Update a job in the database.
8 E3 z9 H- t2 f3 r" m * @param job_id The job id of the existing job4 R* W3 ?# C" N- _ x8 _9 d x$ A5 ]
* @param employer_id The employer offering the job
6 o3 x# s( a7 w& }, N+ X& @# ] * @param title The job title
+ O5 u- v6 w1 U+ d1 H% W, r * @param description The job description1 \' t% H2 r1 \) ^$ K
*/1 R R& c' U( u
public void editJob(long job_id, long employer_id, String title, String description) {/ b3 [* S# F2 E5 p U
ContentValues map = new ContentValues();' K# q. ^5 U, C) |: `0 b# g
map.put("employer_id", employer_id);5 J9 D- J7 ^3 Y$ H& W
map.put("title", title);6 G# g5 l. U+ c3 C
map.put("description", description);
1 P# Q8 b# \9 g9 E; _ String[] whereArgs = new String[]{Long.toString(job_id)}; {. c% \1 f/ G
try{
1 t# K9 [, x' I) n% L8 J getWritableDatabase().update("jobs", map, "_id=?", whereArgs);- u* c5 y0 X9 ~* D
} catch (SQLException e) {$ G, S4 L4 @+ D) I4 n5 T- B& ^- X
Log.e("Error writing new job", e.toString());8 e0 A- }3 V" n1 g2 q1 \) w
}
" b D* i9 D7 u}" G) f9 _9 |/ C% f% m. b
以下是示例8-3中的一些代码亮点:
/ {! v+ i" o: [8 W例8-4显示了如何使用execSQL方法。7 t% }$ ~( y0 C6 `0 x+ Y' P- s
示例8-4 使用execSQL方法3 u# Y7 D) \5 ?0 [3 i
/**
" F8 Z1 w0 J8 O7 E * Update a job in the database.
2 `5 U) \( G- f$ s8 M, I * @param job_id The job id of the existing job
, t' A& p" [( s * @param employer_id The employer offering the job
! \7 j; p3 S, x5 `& R4 Q' h, T4 d * @param title The job title, N; c7 W( o( g7 Y& e
* @param description The job description
+ j% L, d$ F6 I9 Y% J */& p; D( B4 _. f3 Q1 I
public void editJob(long job_id, long employer_id, String title, String description) {5 r4 y: l3 A9 H* Z
String sql =
( G8 ^0 p9 W3 U "UPDATE jobs " +
7 u( X3 ?0 d4 g# V5 p "SET employer_id = ?, "+
5 j4 b% k' o2 \ " title = ?, "+' m2 {! b1 _! L3 W
" description = ? "+
; E. Q2 Z4 c2 t# j "WHERE _id = ? ";
9 |" e" C! r$ I x Object[] bindArgs = new Object[]{employer_id, title, description, job_id};
4 Q2 c, y% x0 V: N! F% o try{
G0 @) K1 F8 R' l" J: r& f3 _. y getWritableDatabase().execSQL(sql, bindArgs);
, o2 S8 R) y3 W7 z& e, w } catch (SQLException e) {
: x# G9 w( J4 i' g7 v) ^: k5 b Log.e("Error writing new job", e.toString());' t: d# `% O# n' F! [$ u
}
, Z- H+ `, R) W' @0 i; J/ b}
! T* O. w4 V) t" [! \9 X' L% `; f: \, H& x
9 @; X6 f2 G& d该消息要求您使参数使用sql变量而不是sql文字。
5 [1 _" Z. ^1 [) i解析每个sql查询,生成计划,并将其存储在sql语句缓存中。& R0 Z! F/ ^9 S7 n! G
从缓存中获取具有相同文本的查询。 k$ h; P; j$ ?+ {8 ?1 p, ?5 S
--One query. H0 N* N/ ~; c8 ? f
SELECT * FROM Customers WHERE Id = @1 (@1 = 3)
" s2 f* v* M5 K+ a$ H" hSELECT * FROM Customers WHERE Id = @1 (@1 = 4)5 p! Y8 j4 F1 N2 w. x; o
SELECT * FROM Customers WHERE Id = @1 (@1 = 5)
; A J- L2 K; p3 r' [在缓存中找不到具有不同文本(包括文字)的查询,并已(无用地)将查询添加到了该查询中。$ ~7 N' q- E) \
--Three Queries.8 m5 |8 S& Z; u0 |2 ?
SELECT * FROM Customers WHERE Id = 3
4 y0 e1 t. X* G+ cSELECT * FROM Customers WHERE Id = 4$ A' Y+ p/ Y/ T8 f% |
SELECT * FROM Customers WHERE Id = 5 |
|