0 1 0

安卓实训开发-笔记本之数据封装传输和数据库执行

ZAESKY
2020-1-27 3670

当然做完一大堆功能还是离不开数据库的支持,还需要将操作数据封装调用数据库代码才能将数据储存。

封装数据代码

package com.example.notebook;
import java.io.Serializable;
import android.R.integer;
public class Notelist implements Serializable  {
        private int id;
        private String titleString,eventString,dateString;
        public int getId() {
                 return id;
        }
        public void setId(int id) {
                 this.id = id;
        }
        public String getTitleString() {
                 return titleString;
        }
        public void setTitleString(String titleString) {
                 this.titleString = titleString;
        }
        public String getEventString() {
                 return eventString;
        }
        public void setEventString(String eventString) {
                 this.eventString = eventString;
        }
        public String getDateString() {
                 return dateString;
        }
        public void setDateString(String dateString) {
                 this.dateString = dateString;
        }
        public Notelist(int id, String titleString, String eventString, String dateString) {
                 super();
                 this.id = id;
                 this.titleString = titleString;
                 this.eventString = eventString;
                 this.dateString = dateString;
        }
        public Notelist() {
                 // TODO Auto-generated constructor stub
        }
}


调用的数据库代码

package com.example.notebook;
import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/*----------------------------------数据库语句------------------------------------*/
public class MydatabaseHelper extends SQLiteOpenHelper {
        //1.定义数据库名称
        private static String DATABASENAME="personal.db";
        //2.定义数据库版本
        private static int DATABASEVERSION=1;
        public MydatabaseHelper(Context context) {
                 super(context, DATABASENAME, null, DATABASEVERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase arg0) {
                 // TODO Auto-generated method stub
                 //1.构建创建用户注册信息数据表语句
                 String sqlString="CREATE TABLE register ("+ 
                             "id   INTEGER        PRIMARY KEY AUTOINCREMENT,"+
                             "name VARCHAR( 8 ),"+
                             "pwd  VARCHAR( 14 ) "+
                             ");";
                 //2.执行构造好的语句
                 arg0.execSQL(sqlString);
                 //创建笔记事件记录的表
                 sqlString="CREATE TABLE event ( "+
                                  "id     INTEGER                PRIMARY KEY AUTOINCREMENT,"+
                                  "title  TEXT( 30 ),"+
                                  "events TEXT( 255 ),"+
                                  "date   DATE "+
                                  ");";
                 arg0.execSQL(sqlString);
                 //构建笔记事件备份的表
                 sqlString="CREATE TABLE eventbackup ( "+
                                  "title  TEXT( 30 ),"+
                                  "events TEXT( 255 ),"+
                                  "date   DATE "+
                                  ");";
                 arg0.execSQL(sqlString);
        }
        //当用户通过注册页面注册,通过此方法将注册信息插入表中。
        public void insert(String nameString,String pwdString) {
                 SQLiteDatabase db=super.getWritableDatabase();
                 String sqlString="insert into register(name,pwd) values('"+nameString+"','"+pwdString+"')";
                 db.execSQL(sqlString);
                 db.close();
        }
        //当用户添加了记事之后,通过此方法插入到表中。
        public void eventsinsert(String titleString,String evntsString,String timeString) {
                 SQLiteDatabase db=super.getWritableDatabase();
                 String sqlString="insert into event(title,events,date) values('"+titleString+"','"+evntsString+"','"+timeString+"')";
                 db.execSQL(sqlString);
                 db.close();
        }
        //通过游标方法查找用户注册信息
        public Cursor findCousor(){
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="select name,pwd from register";
                 Cursor resultCursor=db.rawQuery(sqlString, null);
                 return resultCursor;
        }
        //通过游标方法查找事件记录信息
        public Cursor findnoteCursor(){
                 SQLiteDatabase db=super.getReadableDatabase();
                 String sqlString="select id,title,events,date from event";
                 Cursor resultCursor = db.rawQuery(sqlString, null);
                 return resultCursor;
        }
        //当用户修改了记事之后通过此方法修改数据库里面的记录
        public void eventsupdate(String idString,String titleString,String eventsString,String dateString) {
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="update event set title='"+titleString+"',events='"+eventsString+"',date='"+dateString+"' where id="+idString;
                 db.execSQL(sqlString);
                 db.close();
        }
        //当用户删除记事之后通过此方法删除数据库里面的记事
        public void eventdelete(int id) {
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="delete from event where id="+id;
                 db.execSQL(sqlString);
                 db.close();
        }
        //查找事件记录
        public Cursor getAllnote() {
                 SQLiteDatabase db=super.getReadableDatabase();
                 String sqlString="select id,title,events,date from event";
                 Cursor resultCursor = db.rawQuery(sqlString, null);
                 return resultCursor;
        }
        //数据库备份
        public void backup() {
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="insert into  eventbackup select title,events,date from  event";
                 db.execSQL(sqlString);
                 db.close();
        }
        //删除原备份
        public void backupdelete() {
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="delete from eventbackup";
                 db.execSQL(sqlString);
                 db.close();
        }
        //数据库还原
        public void recover() {
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="insert into  event(title,events,date) select title,events,date from  eventbackup";
                 db.execSQL(sqlString);
                 db.close();
        }
        //删除原数据
        public void recoverdelete() {
                 SQLiteDatabase db=getWritableDatabase();
                 String sqlString="delete from event";
                 db.execSQL(sqlString);
                 db.close();
        }
        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                 // TODO Auto-generated method stub
        }
}


最新回复 (0)

    暂无评论

请先登录后发表评论!

返回
请先登录后发表评论!