1 import java.util.ArrayList; 2 import java.util.HashMap; 3 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.database.sqlite.SQLiteOpenHelper; 8 9 public class MyDBHelper extends SQLiteOpenHelper {10 private final static String DB_NAME = "alarm_db";11 private final static int DATABASE_VESION = 1;12 13 public String ALARM_ID = "_id";14 public String ALARM_TIME = "alarmtime";15 public String ALARM_REPEAT = "alarmrepeat";16 public String ALARM_ISOPEN = "alarmisopen";17 18 public ArrayList list;19 public int[] id ;20 public String[] alarm_time;21 public String[] alarm_repeat;22 public String[] alarm_isopen;23 24 public MyDBHelper(Context context) {25 super(context, DB_NAME, null, DATABASE_VESION);26 }27 28 @Override29 public void onCreate(SQLiteDatabase db) {30 String str = "create table " +DB_NAME + "(" +ALARM_ID +31 " integer primary key autoincrement,"+32 ALARM_TIME +" text,"+ALARM_REPEAT+" text,"+ ALARM_ISOPEN+" text)";33 System.out.println(str.toString());34 db.execSQL(str);35 }36 37 @Override38 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {39 String sql = "drop table if exists " + DB_NAME;40 System.out.println(sql.toString());41 db.execSQL(sql);42 }43 44 public ArrayList getAllInfo(SQLiteDatabase sqliteDB){45 list = new ArrayList();46 Cursor cursor = sqliteDB.rawQuery("select * from alarm_db", null);47 while(cursor.moveToNext()){48 HashMapitem = new HashMap ();49 item.put("_id","日期提醒"+String.valueOf(cursor.getInt(cursor.getColumnIndex("_id"))));50 item.put("alarmtime", cursor.getString(cursor.getColumnIndex("alarmtime")));51 item.put("alarmrepeat",cursor.getString(cursor.getColumnIndex("alarmrepeat")));52 item.put("alarmisopen",cursor.getString(cursor.getColumnIndex("alarmisopen")));53 item.put("image", R.drawable.alarm_dialog);54 list.add(item);55 } 56 return list;57 }58 59 }
1 private MyDBHelper dbHelper; 2 private SQLiteDatabase db; 3 4 dbHelper = new MyDBHelper(MainActivity.this); 5 db = dbHelper.getReadableDatabase(); 6 7 ContentValues values = new ContentValues(); 8 values.put(dbHelper.ALARM_TIME,tv_alarm_time.getText().toString()); 9 values.put(dbHelper.ALARM_REPEAT,tv_alarm_repeat.getText().toString());10 values.put(dbHelper.ALARM_ISOPEN,"isopen");11 db.insert("alarm_db", null, values);