티스토리 뷰

Study

Android(안드로이드) SQLite

메디츠 2023. 3. 22. 18:20
반응형

2010년 10월 기준.

private SQLiteDatabase db;

private final static String DB_NAME = "TapSonic.db";

private final static int DB_MODE = Context.MODE_PRIVATE;

private final static String T_NAME = "add";

 

public void openDB(Context c)

{

db = c.openOrCreateDatabase(DB_NAME, DB_MODE, null);

}

 

public void executeDB(String sql)

{

sql = "SELECT id, name, add FROM ";

sql += T_NAME;

sql += " WHERE id = 'medits'";

 

Log.e("executeDB", sql);

try {

Cursor cur = db.rawQuery(sql, null);

cur.moveToFirst();

if (cur.getCount() > 0) {

//tv.setText(cur.getString(1));

//tv.setText(cur.getString(cur.getColumnIndex("address")));

} else {

Log.e("executeDB", "Data is not exist");

}

} catch (SQLException e) {

Log.e("executeDB Error", e.toString());

}

}

 

public ArrayListgetListData(String sql) {

ArrayListarrayList;

arrayList = new ArrayList();

 

try {

Cursor c = db.rawQuery(sql, null);

c.moveToFirst();

if (c.getCount() > 0) {

int index;

String name;

 

for (int i=0;i

name = c.getColumnName(i);

index = c.getColumnIndex(name);

 

arrayList.add(Integer.toString(index)+","+name);

}

}

} catch (Exception e) {

}

 

return arrayList;

}

 

// 타입 처리

 

date('now') : 현재 입력

datetime('now')

android.util or android.text.util 을 참고

입력은 "yyyy-MM-dd HH:mm:ss" 포맷 문자열로 해야됨

 

String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));

DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

date = iso8601Format.parse(dateTime);

} catch (ParseException e) {

Log.e(TAG, "Parsing ISO8601 datetime failed", e);

}

long when = date.getTime();

int flags = 0;

flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;

flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;

flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;

flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;

String finalDateTime = android.text.format.DateUtils.formatDateTime(context,

when + TimeZone.getDefault().getOffset(when), flags);

 

 

 

SimpleDateFormat sdf =

new SimpleDateFormat(getResources().getString(R.string.DATE_FORMAT_ISO8601));

Date date = new Date(); //or new Date(long millis);

date.setTime(millis); //for now, use: System.currentTimeMillis()

int millis = date.getTime();

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); //or

MEDIUM..

formattedDate = df.format(date);

date = df.parse(formattedDate);

 

## Date 형 변환 순서 정리

TIMESTAMP -> Cursor.getInt()

-> Date.setTime(t*1000) -> DateFormat.format()

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함