안드로이드 SQLite 데이터 추가 방법(INSERT문)
테이블에 데이터를 추가하는 스크립트는 다음과 같다. 샘플스크립트 여러개 올려두니 참고하자.
데이터를 추가하기 위해서는 INSERT문을 사용하게된다.
public static void insertTable(SQLiteDatabase database, int gubunType, String sDate){
if(database != null){
try {
String currentDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(currentDateString));
//Log.e("hour", ""+calendar.get(Calendar.HOUR));
//Log.e("minutes", ""+calendar.get(Calendar.MINUTE));
//Log.e("seconds", ""+calendar.get(Calendar.SECOND));
int hour = calendar.get(Calendar.HOUR);
if(hour < 1) hour = 12;
sDate = sDate + " " + DateUtil.getTime(hour) + ":" + DateUtil.getTime(calendar.get(Calendar.MINUTE)) + ":" + DateUtil.getTime(calendar.get(Calendar.SECOND));
ULog.d("TAG", "=========== insertTable: 저장날짜 : " + sDate + " gubunType = " + gubunType );
// DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// Date dt = df1.parse(sDate);
//
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String eventDate = sdf.format(dt);
// ULog.d(DataBaseUtil.class.getSimpleName(), "=========== eventDate: " + eventDate + " gubunType:" + gubunType);
database.execSQL("INSERT INTO TB_FINEDUST_RECORD(_event_type, _event_date ) VALUES"
+ "("
+ gubunType + ","
+ "'" + sDate + "'"
+ ")");
}catch(Exception e) {
e.printStackTrace();
}
}
}
샘플2
public static void insertMyNumbers(SQLiteDatabase database, String _event_date, int _my_number_1, int _my_number_2, int _my_number_3, int _my_number_4, int _my_number_5, int _my_number_6, int _reg_type){
//_reg_type 1 : 직접등록, _reg_type 2 : 일반번호 등록, _reg_type 3: 고급번호 등록
if(database != null){
try {
database.execSQL("INSERT INTO TB_LOTTO_MY_NUMBER(_event_date, _my_number_1, _my_number_2, _my_number_3, _my_number_4, _my_number_5, _my_number_6, _is_delete , _reg_type ) VALUES"
+ "("
+ "'" + _event_date + "',"
+ _my_number_1 + ","
+ _my_number_2 + ","
+ _my_number_3 + ","
+ _my_number_4 + ","
+ _my_number_5 + ","
+ _my_number_6 + ","
+ 1 + ","
+ _reg_type
+ ")");
}catch(Exception e) {
e.printStackTrace();
}
}
}
샘플3
public static void insertLottoWinerNumbers(SQLiteDatabase database, int _event_round, String _event_date, String _winner_numbers, int _bonus_number){
if(database != null){
try {
database.execSQL("INSERT INTO TB_LOTTO_WINNER_NUMBER(_event_round, _event_date, _winner_numbers, _bonus_number ) VALUES"
+ "("
+ _event_round + ","
+ "'" + _event_date + "',"
+ "'" + _winner_numbers + "',"
+ _bonus_number
+ ")");
}catch(Exception e) {
e.printStackTrace();
}
}
}
데이터베이스 초기화 방법은 아래 링크를 참고
1. 안드로이드 SQLite 사용을 위한 SQLiteOpenHelper 커스터마이징
2. 안드로이드 SQLite 테이블 생성 방법(CREATE TABLE)