是否會因為編寫時候
沒有自動產生出可以使用的方法列表
Intelli-sense
•Windows -> Preferences -> Java -> Editor -> Content Assist
•Auto Activation -> Auto Activation Trigger for Java
•Change "." to ".(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Eclipse-自動添加
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 取得所有資料
*
* @return
*/
public List<Map<String, Object>> getAllData() {
List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
// 查詢條件
String strSelect = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(strSelect, null);
if (cursor.moveToFirst()) {
do {
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put(KEY_ID, cursor.getString(0));
mapData.put(KEY_DATE, cursor.getString(1));
mapData.put(KEY_NUMBER, cursor.getString(2));
mapData.put(KEY_NOTE, cursor.getString(3));
listData.add(mapData);
} while (cursor.moveToNext());
}
return listData;
}
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 取得當日資料
*
* @return
*/
public Map<String, Object> getCurrentData(@NonNull Date mDate) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Map<String, Object> mapData = new HashMap<String, Object>();
// 查詢條件
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE " + KEY_DATE + " = ? ";
String[] strVal = new String[]{sdf.format(mDate)};
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, strVal);
if (cursor.moveToFirst()) {
do {
mapData.put(KEY_ID, cursor.getString(0));
mapData.put(KEY_DATE, cursor.getString(1));
mapData.put(KEY_NUMBER, cursor.getString(2));
mapData.put(KEY_NOTE, cursor.getString(3));
} while (cursor.moveToNext());
}
return mapData;
}
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 刪除資料列
*/
public int deleteData(@NonNull Date mData) {
// DELETE FROM Table where 條件....
String deleteVal = KEY_DATE + " = ?";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String[] strVal = new String[]{sdf.format(mData)};
// 如果你有多條件
/*
String strSQL = KEY_DATE + " = ? AND " + KEY_NOTE + " = ?";
// 第一問號,第二問號...以此類推
String[] strings = new String[]{sdf.format(mDate), "Apple"};
*/
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_CONTACTS, deleteVal, strVal);
}
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
.
.
.
public int deleteData(@NonNull Date mDate, int number, String note) {
String strSQL = KEY_DATE + " = '2018-01-05'";
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_CONTACTS, strSQL, null);
}
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 更新資料
*
* @param mDate
* @param number
* @param note
*/
public int updateData(@NonNull Date mDate, int number, String note) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ContentValues values = new ContentValues();
values.put(KEY_NUMBER, number);
values.put(KEY_NOTE, note);
String strSQL = KEY_DATE + " = ?";
String[] strings = new String[]{sdf.format(mDate)};
// 如果你有多條件
/*
String strSQL = KEY_DATE + " = ? AND " + KEY_NOTE + " = ?";
// 第一問號,第二問號...以此類推
String[] strings = new String[]{sdf.format(mDate), "Apple"};
*/
SQLiteDatabase db = this.getWritableDatabase();
return db.update(TABLE_CONTACTS, values, strSQL, strings);
}
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
.
.
.
public int updateData(@NonNull Date mDate, int number, String note) {
String strSQL = KEY_DATE + " = '2018-01-05'";
SQLiteDatabase db = this.getWritableDatabase();
return db.update(TABLE_CONTACTS, values, strSQL, null);
}
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 添加單筆資料
*
* @param mDate
* @param number
* @param note
* @return
*/
public boolean addData(@NonNull Date mDate, int number,
String note) {
ContentValues values = new ContentValues();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
values.put(KEY_DATE, sdf.format(mDate));
values.put(KEY_NUMBER, number);
values.put(KEY_NOTE, note);
// Inserting Row
SQLiteDatabase db = this.getWritableDatabase();
return db.insert(TABLE_CONTACTS, null, values) == -1 ? false : true;
}
public void ...(){
db.insert(TableName, ColumnName, null);
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHandler extends SQLiteOpenHelper {
// 必要
public MyDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 如果資料庫不存在
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 資料庫有任何參數異動
}
}
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "counterManager";
public MyDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_ID = "id";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
@Override
public void onCreate(SQLiteDatabase db) {
// 依照各自需求建立對應的資料表
// 建立資料庫
String createDB = "create table " + TABLE_CONTACTS + "(" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_DATE + " DATE UNIQUE NOT NULL," +
KEY_NUMBER + " int," +
KEY_NOTE + " char(255)" +
");";
db.execSQL(createDB);
}
private static final String TABLE_CONTACTS = "counter";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 如果資料庫有更新,則移除存在的
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}