13-4-4 android的SQLite功能应用
AndroidSQL数据库的添加步骤:
模式:DAO设计模式
操作:SQLite增删改查
1.建立SQLite数据信息重载增删改查功能
(1)weigthDaoSQLiteImp用于数据库的封装,代码为:
packagecom.work.weight.dao.db;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
importandroid.util.Log;
importcom.work.weight.SysConst;
publicclassWeigthDaoSQLiteImpimplementsWeigthDao{
privatestaticfinalStringTAG="WeigthDaoSQLiteImp";
privateDBHelpermDBHelper;
publicWeigthDaoSQLiteImp(Contextctx){
//context=ctx;
mDBHelper=newDBHelper(ctx);
}
@Override
publicvoidcreate(longdate,Stringinput,Stringoutput,Stringweight,
StringamountExercise){
SQLiteDatabasedb=mDBHelper.getWritableDatabase();
ContentValuesvalues=newContentValues();
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);
longrowId=db.insert(SysConst.TABLE_NAME,null,values);
}
@Override
publicvoiddelete(longdate){
SQLiteDatabasedb=mDBHelper.getWritableDatabase();
StringwhereClause=SysConst.TABLE_FIELD_DATE+"="+date;
longrowId=db.delete(SysConst.TABLE_NAME,whereClause,null);
}
@Override
publicCursorfindAll(){
SQLiteDatabasedb=mDBHelper.getReadableDatabase();
returndb.query(SysConst.TABLE_NAME,newString[]{
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
publicCursorfindByPrimaryKey(longdate){
SQLiteDatabasedb=mDBHelper.getReadableDatabase();
StringwhereClause=SysConst.TABLE_FIELD_DATE+"="+date;
returndb.query(SysConst.TABLE_NAME,newString[]{
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
publicvoidmodify(longdate,Stringinput,Stringoutput,Stringweight,
StringamountExercise){
SQLiteDatabasedb=mDBHelper.getWritableDatabase();
StringwhereClause=SysConst.TABLE_FIELD_DATE+"="+date;
ContentValuesvalues=newContentValues();
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);
longrowId=db.update(SysConst.TABLE_NAME,values,whereClause,null);
}
classDBHelperextendsSQLiteOpenHelper{
publicDBHelper(Contextcontext){
super(context,SysConst.DATABASE_NAME,null,
SysConst.DATABASE_VERSION);
}
@Override
publicvoidonCreate(SQLiteDatabasedb){
try{
StringBuffersql=newStringBuffer();
sql.append("CREATETABLE");
sql.append(SysConst.TABLE_NAME);
sql.append("(");
sql.append(SysConst.TABLE_FIELD_DATE);
sql.append("NUMERIC(13)PRIMARYKEY,");
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(Exceptione){
Log.e(TAG,e.getMessage());
}
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
db.execSQL("DROPTABLEIFEXISTS"+SysConst.TABLE_NAME);
onCreate(db);
}
}
}
(2)weigthDao用于提供数据库封装的接口,代码为:
packagecom.work.weight.dao.db;
importandroid.database.Cursor;
publicinterfaceWeigthDao{
CursorfindByPrimaryKey(longdate);
CursorfindAll();
voidcreate(longdate,Stringinput,Stringoutput,Stringweight,
StringamountExercise);
voiddelete(longdate);
voidmodify(longdate,Stringinput,Stringoutput,Stringweight,
StringamountExercise);
}
2.创建对应参数信息表
日期 | |||
_id | input | output | weight |
姓名 | 摄入 | 消耗 | 体重 |
packagecom.work.weight;
publicinterfaceSysConst{
publicstaticfinalStringDATABASE_NAME="weight.db";
publicstaticfinalintDATABASE_VERSION=2;
publicstaticfinalStringTABLE_NAME="wdate";
publicstaticfinalStringTABLE_FIELD_DATE="_id";
publicstaticfinalStringTABLE_FIELD_INPUT="input";
publicstaticfinalStringTABLE_FIELD_OUTPUT="output";
publicstaticfinalStringTABLE_FIELD_WEIGHT="weight";
publicstaticfinalStringTABLE_FIELD_AMOUNTEXERCISE="amountExercise";
publicstaticfinalStringPREFS_CONF="config.ini";
publicstaticfinalStringCOLOR_KEY="color";
publicstaticfinalStringDATE_KEY="date";
}
3.建立数据与listview连接的adapter(数据源)
publicclassMySimpleCursorAdapterextendsCursorAdapter{
privateCursorcursor;
privateString[]from;
privateint[]to;
privateintlayout;
privateLayoutInflaterinflater;
publicMySimpleCursorAdapter(Contextcontext,intlayout,Cursorc,
String[]from,int[]to){
super(context,c);
inflater=LayoutInflater.from(context);
this.layout=layout;
this.cursor=c;
this.from=from;
this.to=to;
}
@Override
publicvoidbindView(Viewv,Contextcontext,Cursorcursor){
for(inti=0;i<from.length;i++){
TextViewtext=(TextView)v.findViewById(to[i]);
Strings=cursor.getString(cursor.getColumnIndex(from[i]));
if(i==0){
Datedate=newDate(newLong(s));
text.setText(df.format(date));
}else{
text.setText(s);
}
}
}
@Override
publicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent){
Viewv=inflater.inflate(layout,parent,false);
returnv;
}
}
4.显示SQLite中的数据
privatevoidfindAll(){
//WeigthDaodao=DaoFactory.getWeigthDao(this,"weigthDao");
WeigthDaodao=newWeigthDaoSQLiteImp(this);
cursor=dao.findAll();
startManagingCursor(cursor);
sca=newMySimpleCursorAdapter(this,R.layout.my_listitem,cursor,
newString[]{SysConst.TABLE_FIELD_DATE,
SysConst.TABLE_FIELD_INPUT,
SysConst.TABLE_FIELD_OUTPUT,
SysConst.TABLE_FIELD_WEIGHT,
SysConst.TABLE_FIELD_AMOUNTEXERCISE},newint[]{
R.id.date,R.id.input,R.id.output,R.id.weight,
R.id.amountExercise});
Log.v(TAG,sca.toString());
listView.setAdapter(sca);
}
********************************************************************
飞扬小米(记)欢迎评论交流。
更多相关文章
- Android(安卓)保存数据到文件
- android sqlite 一次创建多个表
- android 操作sqllite 数据库
- Android(安卓)sqlite 数据库在java代码中的增删改查
- android访问mysql增删改查_Android(安卓)Studio连接数据库实现增
- Android(安卓)Intents and Intent Filters
- Android腾讯微薄客户端开发十三:提及篇(与我有关的微博)
- android后台上传数据demo
- mybatisplus的坑 insert标签insert into select无参数问题的解决