Android SQL数据库的添加步骤:
模式:DAO设计模式
操作:SQLite增删改查
(1)weigthDaoSQLiteImp用于数据库的封装,代码为:
package com.work.weight.dao.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.work.weight.SysConst;
public class WeigthDaoSQLiteImp implements WeigthDao {
private static final String TAG = "WeigthDaoSQLiteImp";
private DBHelper mDBHelper;
public WeigthDaoSQLiteImp(Context ctx) {
//context = ctx;
mDBHelper = new DBHelper(ctx);
}
@Override
public void create(long date, String input, String output, String weight,
String amountExercise) {
SQLiteDatabase db = mDBHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SysConst.TABLE_FIELD_DATE, date);
values.put(SysConst.TABLE_FIELD_INPUT, input);
values.put(SysConst.TABLE_FIELD_OUTPUT, output);
values.put(SysConst.TABLE_FIELD_WEIGHT, weight);
values.put(SysConst.TABLE_FIELD_AMOUNTEXERCISE, amountExercise);
long rowId = db.insert(SysConst.TABLE_NAME, null, values);
}
@Override
public void delete(long date) {
SQLiteDatabase db = mDBHelper.getWritableDatabase();
String whereClause = SysConst.TABLE_FIELD_DATE + " = " + date;
long rowId = db.delete(SysConst.TABLE_NAME, whereClause, null);
}
@Override
public Cursor findAll() {
SQLiteDatabase db = mDBHelper.getReadableDatabase();
return db.query(SysConst.TABLE_NAME, new String[] {
SysConst.TABLE_FIELD_DATE, SysConst.TABLE_FIELD_INPUT,
SysConst.TABLE_FIELD_OUTPUT, SysConst.TABLE_FIELD_WEIGHT,
SysConst.TABLE_FIELD_AMOUNTEXERCISE }, null, null, null, null,
SysConst.TABLE_FIELD_DATE + " asc");
}
@Override
public Cursor findByPrimaryKey(long date) {
SQLiteDatabase db = mDBHelper.getReadableDatabase();
String whereClause = SysConst.TABLE_FIELD_DATE + " = " + date;
return db.query(SysConst.TABLE_NAME, new String[] {
SysConst.TABLE_FIELD_DATE, SysConst.TABLE_FIELD_INPUT,
SysConst.TABLE_FIELD_OUTPUT, SysConst.TABLE_FIELD_WEIGHT,
SysConst.TABLE_FIELD_AMOUNTEXERCISE }, whereClause, null, null,
null, SysConst.TABLE_FIELD_DATE + " asc");
}
@Override
public void modify(long date, String input, String output, String weight,
String amountExercise) {
SQLiteDatabase db = mDBHelper.getWritableDatabase();
String whereClause = SysConst.TABLE_FIELD_DATE + " = " + date;
ContentValues values = new ContentValues();
values.put(SysConst.TABLE_FIELD_INPUT, input);
values.put(SysConst.TABLE_FIELD_OUTPUT, output);
values.put(SysConst.TABLE_FIELD_WEIGHT, weight);
values.put(SysConst.TABLE_FIELD_AMOUNTEXERCISE, amountExercise);
long rowId = db.update(SysConst.TABLE_NAME, values, whereClause, null);
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, SysConst.DATABASE_NAME, null,
SysConst.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
StringBuffer sql = new StringBuffer();
sql.append("CREATE TABLE ");
sql.append(SysConst.TABLE_NAME);
sql.append(" (");
sql.append(SysConst.TABLE_FIELD_DATE);
sql.append(" NUMERIC(13) PRIMARY KEY ,");
sql.append(SysConst.TABLE_FIELD_INPUT);
sql.append(" Text,");
sql.append(SysConst.TABLE_FIELD_OUTPUT);
sql.append(" Text,");
sql.append(SysConst.TABLE_FIELD_WEIGHT);
sql.append(" Text ,");
sql.append(SysConst.TABLE_FIELD_AMOUNTEXERCISE);
sql.append(" Text");
sql.append(");");
Log.i(TAG, sql.toString());
db.execSQL(sql.toString());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SysConst.TABLE_NAME);
onCreate(db);
}
}
}
(2)weigthDao用于提供数据库封装的接口,代码为:
package com.work.weight.dao.db;
import android.database.Cursor;
public interface WeigthDao {
Cursor findByPrimaryKey(long date);
Cursor findAll();
void create(long date, String input, String output, String weight,
String amountExercise);
void delete(long date);
void modify(long date, String input, String output, String weight,
String amountExercise);
}
日期 |
|||
_id |
input |
output |
weight |
姓名 |
摄入 |
消耗 |
体重 |
package com.work.weight;
public interface SysConst {
public static final String DATABASE_NAME = "weight.db";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME = "wdate";
public static final String TABLE_FIELD_DATE = "_id";
public static final String TABLE_FIELD_INPUT = "input";
public static final String TABLE_FIELD_OUTPUT = "output";
public static final String TABLE_FIELD_WEIGHT = "weight";
public static final String TABLE_FIELD_AMOUNTEXERCISE = "amountExercise";
public static final String PREFS_CONF = "config.ini";
public static final String COLOR_KEY = "color";
public static final String DATE_KEY = "date";
}
public class MySimpleCursorAdapter extends CursorAdapter {
private Cursor cursor;
private String[] from;
private int[] to;
private int layout;
private LayoutInflater inflater;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, c);
inflater = LayoutInflater.from(context);
this.layout = layout;
this.cursor = c;
this.from = from;
this.to = to;
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
for (int i = 0; i < from.length; i++) {
TextView text = (TextView) v.findViewById(to[i]);
String s = cursor.getString(cursor.getColumnIndex(from[i]));
if (i == 0) {
Date date = new Date(new Long(s));
text.setText(df.format(date));
} else {
text.setText(s);
}
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(layout, parent, false);
return v;
}
}
private void findAll() {
//WeigthDao dao = DaoFactory.getWeigthDao(this, "weigthDao");
WeigthDao dao = new WeigthDaoSQLiteImp(this);
cursor = dao.findAll();
startManagingCursor(cursor);
sca = new MySimpleCursorAdapter(this, R.layout.my_listitem, cursor,
new String[] { SysConst.TABLE_FIELD_DATE,
SysConst.TABLE_FIELD_INPUT,
SysConst.TABLE_FIELD_OUTPUT,
SysConst.TABLE_FIELD_WEIGHT,
SysConst.TABLE_FIELD_AMOUNTEXERCISE }, new int[] {
R.id.date, R.id.input, R.id.output, R.id.weight,
R.id.amountExercise });
Log.v(TAG, sca.toString());
listView.setAdapter(sca);
}
********************************************************************
飞扬小米(记) 欢迎评论交流。