From 07fb02b92db62bede33a85389ec617a2764020bb Mon Sep 17 00:00:00 2001 From: zhangnaifu15 <1361423499@qq.com> Date: Tue, 8 May 2018 21:15:45 +0800 Subject: [PATCH] src\MyApplication\app\src\main\java\com\example\administrator\myapplication\db\DatabaseHelper.java --- .../myapplication/db/DatabaseHelper.java | 37 ++++ .../myapplication/db/NoteDatabase.java | 180 ++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/DatabaseHelper.java create mode 100644 src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/NoteDatabase.java diff --git a/src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/DatabaseHelper.java b/src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/DatabaseHelper.java new file mode 100644 index 0000000..00d5227 --- /dev/null +++ b/src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/DatabaseHelper.java @@ -0,0 +1,37 @@ +package com.example.administrator.myapplication.db; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class DatabaseHelper extends SQLiteOpenHelper { + + public static final String COOLNOTE_DATABASE_NAME = "coolnote"; + + public static final String NOTE_TABLE_NAME = "htq_Notebook"; + + public static final String CREATE_NOTE_TABLE = "create table " + + NOTE_TABLE_NAME + + " (_id integer primary key autoincrement, objectid text, iid integer," + + " time varchar(10), date varchar(10), content text, color integer)"; + + public static final String NEWS_LIST = "osc_news_list"; + + public static final String CREATE_NEWS_LIST_TABLE = "create table " + + NOTE_TABLE_NAME + "(" + "_id integer primary key autoincrement, " + + "news_id interger, title varchar(10), " + ")"; + + public DatabaseHelper(Context context) { + super(context, COOLNOTE_DATABASE_NAME, null, 1); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(CREATE_NOTE_TABLE); + // db.execSQL(CREATE_NEWS_LIST_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} + +} \ No newline at end of file diff --git a/src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/NoteDatabase.java b/src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/NoteDatabase.java new file mode 100644 index 0000000..5df308e --- /dev/null +++ b/src/MyApplication/app/src/main/java/com/example/administrator/myapplication/db/NoteDatabase.java @@ -0,0 +1,180 @@ +package com.example.administrator.myapplication.db; + +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + + +import com.example.administrator.myapplication.entity.NotebookData; +import com.example.administrator.myapplication.utils.AccountUtils; +import com.example.administrator.myapplication.utils.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +public class NoteDatabase { + private final DatabaseHelper dbHelper; + + public NoteDatabase(Context context) { + super(); + dbHelper = new DatabaseHelper(context); + } + + /** + * 增 + * + * @param data + */ + public void insert(NotebookData data) { + String sql = "insert into " + DatabaseHelper.NOTE_TABLE_NAME; + + sql += "(_id, objectid, iid, time, date, content, color) values(?, ?, ?, ?, ?, ?, ?)"; + + SQLiteDatabase sqlite = dbHelper.getWritableDatabase(); + sqlite.execSQL(sql, new String[] { data.getId() + "", + data.getIid() + "", data.getObjectId(), data.getUnixTime() + "", data.getDate(), + data.getContent(), data.getColor() + "" }); + sqlite.close(); + } + + /** + * 删 + * + * @param id + */ + public void delete(int id) { + SQLiteDatabase sqlite = dbHelper.getWritableDatabase(); + String sql = ("delete from " + DatabaseHelper.NOTE_TABLE_NAME + " where _id=?"); + sqlite.execSQL(sql, new Integer[] { id }); + sqlite.close(); + } + + /** + * 改 + * + * @param data + */ + public void update(NotebookData data) { + SQLiteDatabase sqlite = dbHelper.getWritableDatabase(); + String sql = ("update " + DatabaseHelper.NOTE_TABLE_NAME + " set iid=?, objectid=?, time=?, date=?, content=?, color=? where _id=?"); + sqlite.execSQL(sql, + new String[] { data.getIid() + "", data.getObjectId() + "", data.getUnixTime() + "", + data.getDate(), data.getContent(), + data.getColor() + "", data.getId() + "" }); + sqlite.close(); + } + + public List query() { + return query(" "); + } + + /** + * 查 + * + * @param where + * @return + */ + public List query(String where) { + SQLiteDatabase sqlite = dbHelper.getReadableDatabase(); + ArrayList data = null; + data = new ArrayList(); + Cursor cursor = sqlite.rawQuery("select * from " + + DatabaseHelper.NOTE_TABLE_NAME + where, null); + for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { + NotebookData notebookData = new NotebookData(); + notebookData.setId(cursor.getInt(0)); + notebookData.setObjectId(cursor.getString(1)); + notebookData.setIid(cursor.getInt(2)); + notebookData.setUnixTime(cursor.getString(3)); + notebookData.setDate(cursor.getString(4)); + notebookData.setContent(cursor.getString(5)); + notebookData.setColor(cursor.getInt(6)); + data.add(notebookData); + } + if (!cursor.isClosed()) { + cursor.close(); + } + sqlite.close(); + + return data; + } + + /** + * 重置 + * + * @param datas + */ + public void reset(List datas) { + if (datas != null) { + SQLiteDatabase sqlite = dbHelper.getWritableDatabase(); + // 删除全部 + sqlite.execSQL("delete from " + DatabaseHelper.NOTE_TABLE_NAME); + // 重新添加 + for (NotebookData data : datas) { + insert(data); + } + sqlite.close(); + } + } + + /** + * 保存一条数据到本地(若已存在则直接覆盖) + * + * @param data + */ + public void save(NotebookData data) { + List datas = query(" where _id=" + data.getId()); + if (datas != null && !datas.isEmpty()) { + update(data); + } else { + insert(data); + } + } + + // + // /** + // * 合并一条数据到本地(通过更新时间判断仅保留最新) + // * + // * @param data + // * @return 数据是否被合并了 + // */ + // public boolean merge(NotebookData data) { + // Cursor cursor = sqlite.rawQuery( + // "select * from " + DatabaseHelper.NOTE_TABLE_NAME + // + " where _id=" + data.getId(), null); + // NotebookData localData = new NotebookData(); + // // 本循环其实只执行一次 + // for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { + // localData.setId(cursor.getInt(0)); + // localData.setIid(cursor.getInt(1)); + // localData.setUnixTime(cursor.getString(2)); + // localData.setDate(cursor.getString(3)); + // localData.setContent(cursor.getString(4)); + // localData.setColor(cursor.getInt(5)); + // } + // // 是否需要合这条数据 + // boolean isMerge = localData.getUnixTime() < data.getUnixTime(); + // if (isMerge) { + // save(data); + // } + // return isMerge; + // } + + public void destroy() { + dbHelper.close(); + } + + public void insertIntroduce(Context context) + { + NotebookData editData=new NotebookData(); + if (editData.getId() == 0) { + editData.setId(-1 + * StringUtils.toInt( + StringUtils.getDataTime("dddHHmmss"), 0)); + } + editData.setUnixTime(StringUtils.getDataTime("yyyy-MM-dd HH:mm:ss")); + editData.setContent("欢迎使用嗖藏,赶快记下你此刻的灵感吧!"); + editData.setUserId(AccountUtils.getUserId(context)); + save(editData); + } +} \ No newline at end of file