ããå
³äº SQLite çä¼åï¼é¦å
æ¯è½ç¨SQLè¯å¥çï¼å°±ä¸è¦åç¬æä½,
Cursor å°±æ´æ¯è½ä¸ç¨å°±ä¸ç¨ãæ¯å¦ææ¹ç
DELETE/UPDATE ï¼å°æ¡ä»¶ç»è£
å°
SQL è¯å¥ï¼ä¼æ¯ä½¿ç¨ CURSOR
ä¸æ¡æ¡çæ¥åå æçè¦é«å¾å¤(
è¥å¹²å¹´åå°±æ¾ä½¿ç¨åå¨è¿ç¨ä»£æ¿åç¬æä½ï¼å°ä¸æ¬¡æ¹é计ç®æ¶é´ä»ä¸æä¸ç¼©å°äºä¸å°æ¶ä»¥å
)ãå
¶æ¬¡æ¯å¯¹æä½çä¼åï¼å¯¹äº INSERT/UPDATE
æä½è¾å¤æ¶ä½¿ç¨äºå¡ï¼å¦æSELECTæä½è¾å¤æ¶ï¼ä½¿ç¨ç´¢å¼ã
ããç»åç°å¨çå·¥ä½ï¼åç°é对æä½çä¼åï¼ä¸é¢ è¿ç¯æç« å¯ä»¥ç¿»è¯åºæ¥å½æ¡£ã以ä¸ä¸ºæ£æ:
ããSQLite æä¸ä¸ªç®æ´çSQLæ¥å£ï¼ä¸ä»¥ä½å
åå ç¨è称ãç°å¦ä»ï¼
SQLite å·²ç»å¨ Android å
iOS å¼åä¸å¾å°å¹¿æ³çåºç¨ãæ¬æ主è¦è®¨è®ºå¨ Android
åºç¨å¦ä½ä¼å SQLite çæ§è½åèµæºå ç¨ã
ãã1, 使ç¨äºå¡( Transaction )
ããå¨é»è®¤æ
åµä¸æ¯ä¸ä¸ªSQLè¯å¥é½è¢«å
ä¸ä¸ªå
¨æ°çäºå¡å
ï¼æ¯å¦æ§è¡ä¸ä¸ªå¦INSERTè¿æ ·åºæ¬çæ°æ®åºæä½ï¼å°±ä¼æ¾å°ä¸ä¸ªæ°å建çäºå¡ä¸æ§è¡ãä¸æ¬¡åªéè¦æä½ä¸æ¬¡æ°æ®åºæä½æ¶ï¼è®©SQLiteèªå·±æ¥è¿è¡äºå¡ç®¡çå½ç¶æ¯ææºçãä½å¦æä¸æ¬¡æ大éçæä½è¦åæ¶ï¼æ¯å¦å¾ªç¯è°ç¨INSERTæ·»å æ¶ï¼è¿æ ·å°±æ¾å¾å¼éè¿å¤§äºï¼å 为æ¯ä¸ç¬æä½é½è¦éæ°æå¼ãåå
¥ï¼æååå
³éjournalæ件,
è¿ä¸ªæ件æ¯ä¸´æ¶ç¨æ¥ä¿åæ°æ®æä½çä¸é´ç»æï¼è¯¦ç»å
容çè¿é( åè )ã
ããå¦ææç¡®å°å¨ä¸ç³»åSQLè¯å¥åå以 BEGIN TRANSACTION å
END TRANSACTION
è¿æ ·æ¾ç¤ºå°ä½¿ç¨äºå¡å°±å¯ä»¥é¿å
ä¸é¢çæ
åµã对äºé£äºä¸ä¼æ¹åæ°æ®çæä½ï¼è¿æ ·çæ¹å¼ä¹åæ ·å¯ä»¥æé(好似æ°æ®åºæä½ä¸åç¬çæä½æçå°è¿ä½äºæ¹æ¬¡æä½ï¼å¦æç¨SQLè¯å¥å¯ä»¥æå®çäºï¼å°±ä¸å¯ä½¿ç¨Cursorè¿è¡æä½)ã
ãã注æ:é¤äºåèµ·äºå¡å¤ï¼ä½ å¿
é¡»è¿è¦è´è´£å¯¹äºå¡çæ交ååæ»æä½ã
ããå¨Androidåºç¨å¼åä¸å¯ä»¥ä½¿ç¨ç±»ä¼¼å¦ä¸çæ¹å¼ä½¿ç¨ BEGIN TRANSACTION
å END TRANSACTION :
ããdb.beginTransaction();
try{
for(int i =0; i< LENGTH ; i++,sequenceNum++)
{
// execute SQL
}
db.setTransactionSuccessful();// marks a commit
}
finally{
db.endTransaction();
}
ãã2. 使ç¨ç´¢å¼
ããå¦æ没æå¨æ°æ®åºä½¿ç¨ç´¢å¼ï¼å½ä½ å¨ä¸ä¸ªæ²¡ææåºçæ°æ®è¡¨ä¸ä½¿ç¨æ å°æ¥è¯¢(projection
query)æç´¢æ¶ï¼æ å¯é¿å
çè¦æ§è¡ä¸ä¸ªå
¨åºåæ¥æ¾ãè¿ç§æ
åµé常并ä¸æ¯ä»ä¹é®é¢ï¼æ¯ç§æ°æ®åºï¼å
æ¬SQLiteé½ä¼ä¸ºæ°æ®éæ§è¡ç´¢å¼æ¥éä½æ¥æ¾æ¶é´ã
ããç´¢å¼ç»´æ¤çä¸ä¸ªè¡¨ä¸æä¸åææå åç顺åºï¼è¿æ ·å°±å¯ä»¥å¿«éå®ä½å°ä¸ç»å¼ï¼èä¸ç¨æ«éå
¨è¡¨ãææçç´¢å¼ä¿¡æ¯ä¼è¢«ä¿åå¨ä¸ä¸ªç¬ç«çç´¢å¼è¡¨ä¸ï¼æ以ä¼äº§çé¢å¤ç空é´å ç¨ï¼ä¸è¿ç»å¯¹ç©è¶
æå¼ï¼ç¹å«æ¯å½ä½ ä¼å¨æ°æ®åºä¸è¿è¡å¤§éç读åæç´¢æä½æ¶ã
ããSQLiteä¼èªå¨ä¸ºæ¯ä¸ä¸ªUNIQUEæ ä½å建索å¼ï¼å
æ¬ä¸»é®(Primary Key)æ ä½ï¼å¦å¤ä¹å¯ä»¥éè¿CREATE
INDEXè¿è¡æ¾ç¤ºå°å建ã
ãã注:å¦æä½ çæ¥è¯¢å¤ªå¤æèæ æ³ä½¿ç¨æå建çç´¢å¼ï¼é£ä½ å°±è¦å¥½å¥½æ³æ³ä½ æ°æ®åºçç»æäºã
ãã3. å¨Whereåæ¯ä¸ä½¿ç¨éå®ç¬¦
ããå¦æ以å串æ¼æ¥åºSQLè¯å¥çWhereï¼è«ä¸å¦ä½¿ç¨SQLiteçqueryæä½å¸¦ä¸'?'æ¥ç¼è¯æ¥è¯¢ã以ä¸æ¯å®ç好å¤:
ããa. æå©äºSQLiteç¼åè¿äºæ¥è¯¢ã
ããb. å¯ä»¥é¿å
è¾¾å°SQLiteç¼åçä¸éã使ç¨å串æ¼æ¥Whereçæ¥è¯¢ï¼æ¯ä¸ä¸ªé½è¢«è§ä¸ºä¸åçæ¥è¯¢ï¼è¿å°±å®¹æè¾¾å°ç¼åçä¸éã
ããc. å¯ä»¥é¿å
éæ³çSQL注å
¥ã
ãã转载
温馨提示:内容为网友见解,仅供参考