Androidã¢ããªã±ã¼ã·ã§ã³ã§ã®ãã¼ã¿ãã¼ã¹åºç¤
ãååã§ã¬ã¤ã¢ã¦ããä½æã§ãã¾ããã®ã§ãä»åã¯ãã¼ã¿ãã¼ã¹ãä½æãã¦ã¢ããªã±ã¼ã·ã§ã³ã«çµã¿è¾¼ãã§ããã¾ãããã
対象èªè
ãæ¬é£è¼ã¯Javaããã³Eclipseã®åºæ¬ãç解ãã¦ããæ¹ã対象ã¨ãã¦ãã¾ãã
ååã¾ã§ã®è¨äº
- 第1åãAndroidã®æ¦è¦ã¨éçºç°å¢ã®æ§ç¯
- 第2åãæä½éããã ãç¥ã£ã¦ããã°OKï¼ Androidã¢ããªéçºã®åºç¤ç¥è
- 第3åãã¬ã¤ã¢ã¦ãã®ä½ææé ããã¹ã¿ã¼ããï¼
Androidã§å©ç¨ããSQLiteã®ã¯ã©ã¹
ãAndroidã§ã¯SQLiteããµãã¼ãããã¦ãããä¸è¨ã®2ã¤ã®ã¯ã©ã¹ã使ãã ãã§ãç°¡åã«å©ç¨ã§ãã¾ãã
SQLiteOpenHelper
ããã¼ã¿ãã¼ã¹ã®çæãã¹ãã¼ãã®ãã¼ã¸ã§ã³ç®¡çãè¡ãããã®æ½è±¡ã¯ã©ã¹ã§ãããã¡ãã®ã¯ã©ã¹ãç¶æ¿ãã¦ãã¼ã¿ãã¼ã¹ã®çæãã¢ããã°ã¬ã¼ãå¦çãå®è£ ãã¾ãã
SQLiteDatabase
ãinsertãupdateãªã©ãã¼ã¿ãã¼ã¹ã«å¯¾ããå種æä½ç¨ã®ã¡ã½ãããæä¾ããã¯ã©ã¹ã§ãã
ãåååæ§ãä»åã®ä½æ¥ãè¡ã£ãç¶æ ã®ããã¸ã§ã¯ãããã¡ããããã¦ã³ãã¼ãã§ãã¾ãã®ã§ãè¿·ã£ãå ´åã¯åèã«ãã¦ãã ããã
ãSQLiteã¯ãã¼ã¿ã®ä¿åã«åä¸ãã¡ã¤ã«ã®ã¿ã使ç¨ãã軽éã®RDBMSã§ããã¢ããªã±ã¼ã·ã§ã³ã«å«ãå½¢ã§å©ç¨ãããå ´åãå¤ããiPhoneã§ãSQLiteãå©ç¨ããã¦ãã¾ãããã¼ã¿åãã·ã³ãã«ã§ä¸è¨ã®5種é¡ã®ã¿ã«å¯¾å¿ãã¦ãã¾ãã
- INTEGERã»ã»ã»ç¬¦å·ä»æ´æ°
- REALã»ã»ã»æµ®åå°æ°ç¹
- TEXTã»ã»ã»ããã¹ã
- BLOBã»ã»ã»ãã¤ããªãã¼ã¿
- NULLã»ã»ã»NULL
äºåæºå
ããã¼ã¿ãã¼ã¹ã®ä½æã«å ¥ãåã«ãã½ã¼ã¹ã®ãã¾ãã¾ãªæã«SQLãã«ã©ã åãåã¾ã£ã¦ãã¾ãã®ãé²ãããã1ã¬ã³ã¼ãåã®ãã¼ã¿ãä¿æããã¯ã©ã¹ãæºåãã¾ãããããªãããã¼ã¿ãã¼ã¹é¢é£ã®ã½ã¼ã¹ã¯ãsample.bizcard.dbãããã±ã¼ã¸ã«ä½æãã¦ãã¾ãã
package sample.bizcard.db; import java.io.Serializable; /** * 1ã¬ã³ã¼ãã®ãã¼ã¿ãä¿æãããªãã¸ã§ã¯ã * Intentã«è©°ãã¦ããåãããã®ã§Serializableãimplementsãã */ @SuppressWarnings("serial") public class BizCard implements Serializable{ // ãã¼ãã«å public static final String TABLE_NAME = "biz_card"; // ã«ã©ã å public static final String COLUMN_ID = "_id"; public static final String COLUMN_PERSON_NAME = "person_name"; public static final String COLUMN_COMPANY_NAME = "company_name"; public static final String COLUMN_ORGANIZATION_NAME = "organization_name"; public static final String COLUMN_POSITION_NAME = "position_name"; public static final String COLUMN_ZIP_CODE = "zip_code"; public static final String COLUMN_ADDRESS = "address"; public static final String COLUMN_TEL1 = "tel1"; public static final String COLUMN_TEL2 = "tel2"; public static final String COLUMN_MAIL = "mail"; // ãããã㣠private Long rowid = null; private String personName = null; private String organizationName = null; private String companyName = null; private String positionName = null; private String zipCode = null; private String address = null; private String tel1 = null; private String tel2 = null; private String mail = null; ï¼ ï¼ä¸ç¥(getter/setter) ï¼ /** * ListView表示ã®éã«å©ç¨ããã®ã§ã¦ã¼ã¶å+ä¼ç¤¾åãè¿ã */ @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append( getPersonName()); if( getCompanyName() != null){ builder.append(":"); builder.append(getCompanyName()); } return builder.toString(); } }