[安卓] 数据库的Android仲 - P4更新数据库
本教程的主要目的是在当今所有的Android数据库 – 更新数据库如何申请不丢失, 当你想更改数据库无故障.
联赛历史后进行应用程序的注意事项, 你想让你的音符最后修改时间. 因此,我们把执行单干.

步 1: 阶级结构的变化注意
每一个音符应该有更多的时间进行最后的修正, 所以我们需要的属性添加到类注上次更改.
package cachhoc.net.tut.demodatabase;
public class Note {
private long id;
private String title;
private String content;
private String lastModified;
/**
* here is getter and setter of id, title, content.
*/
public String getLastModified() {
return lastModified;
}
public Note setLastModified(String lastModified) {
this.lastModified = lastModified;
return this;
}
}
我们只需要时间显示最后一次修改,但不必计算它应该与使用的字符串不使用类型日期 (日期类型,将更难处理).
步 2: 更新DatabaseHelper
这是最重要的一步. 我们反过来改变DatabaseHelper下面的部分:
更改命令创建表
首先,我们需要添加一个不变的是新列的标题. 然后将其添加到命令创建表的注意事项
/**
* table note contain id, title, content
*/
public static final String TABLE_NOTE = "tb_note";
public static final String KEY_ID_NOTE = "id";
public static final String KEY_TITLE_NOTE = "title";
public static final String KEY_CONTENT_NOTE = "content";
public static final String KEY_LAST_MODIFIED_NOTE = "last_modified";
/**
* string for create table note
*/
public static final String CREATE_TABLE_NOTE =
"CREATE TABLE " + TABLE_NOTE + "(" +
KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
"," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
"," + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'" +
")";
这确保了新手机也将安装在表列LAST_MODIFIED笔记.
更新的数据库版本
你还记得我们有一天 1 变量是DATA_VERSION, 它是版本数据库. 现在,越来越多你给它更大的比以前的版本 (最后一次是 1, 小时应增加到 2).
/** * value for update database */ public static final int DATA_VERSION = 2;
执行数据库更新
正如我所提到的前一天onUpgrade方法将被调用的时候,我们更新数据库版本. 只是DATA_VERSION变化,这种方法将被调用. 我们重写这个方法如下::
/**
* call when change DATA_VERSION
* help we update database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// update database for database version < 2
if (oldVersion < 2) {
db.execSQL("ALTER TABLE " + TABLE_NOTE + " ADD COLUMN " + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'");
}
}
你有没有注意到以下几点:
- KHI更新应用程序, 系统会自动保存的旧版本的数据库版本在上述变量oldVersion. NEWVERSION是新DATA_VERSION.
-
你们有些人经常清理表和回调函数在这的onCreate, 然而,这样将导致整个旧数据. 这是不愿意这样做, 如果他们避免任何忌.
-
使用oldVersion他的病情 < 2 执行编辑表. 这使您可以编辑阶段数据库. 例如,下一次DATA_VERSION = 3 那么if语句添加(oldVersion < 3) 与实施. 它可以帮助你记住阶段更新数据库,也可以使应用程序更新间隔 (例如,从 1.0 不涨 2.0 但最多 3.0 对) 没有更新错误.
-
此字段LAST_MODIFIED自己默认为空字符串, 帮助记录一直没空, 避免错误检索.
更新相关的检索方法
在本节, 我们需要修改 2 方法注意到并从数据库中采取ContentValues笔记能投入,并采取了足够的信息.
/**
* convert note to contentvalues
* don't put id of note because
* when insert id will auto create
* when update we don't update id
*/
private ContentValues noteToValues(Note note) {
ContentValues values = new ContentValues();
values.put(KEY_TITLE_NOTE, note.getTitle());
values.put(KEY_CONTENT_NOTE, note.getContent());
values.put(KEY_LAST_MODIFIED_NOTE, note.getLastModified());
return values;
}
/**
* convert cursor to note
*/
private Note cursorToNote(Cursor cursor) {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID_NOTE)))
.setTitle(cursor.getString(cursor.getColumnIndex(KEY_TITLE_NOTE)))
.setContent(cursor.getString(cursor.getColumnIndex(KEY_CONTENT_NOTE)))
.setLastModified(cursor.getString(cursor.getColumnIndex(KEY_LAST_MODIFIED_NOTE)));
return note;
}
步 3: 更新,其中新数据创建
听着似乎有点混乱,但它是简单, 在类中的正确的方法NoteAcitivty保存,以节省时间更新笔记.
/**
* get title and content and update last modified time of note, if they empty then finish
* if they not empty then check note is null?
* if note is null (create new note), we will create note and insert into database
* if note not null then we update note
* after save we finish activity
*/
private void save() {
String title = editTitle.getText().toString().trim();
String content = editContent.getText().toString().trim();
String notify = null;
if (TextUtils.isEmpty(title) && TextUtils.isEmpty(content)) {
notify = "note empty, don't save!";
} else {
// get curren time for last modified
SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currenTime = formatTime.format(cal.getTime());
// new note
if (note == null) {
Note note = new Note();
note.setTitle(title).setContent(content).setLastModified(currenTime);
if (db.insertNote(note) > 0) {
notify = "add success!";
} else {
notify = "add fail!";
}
} else { // update note
note.setTitle(title).setContent(content).setLastModified(currenTime);
if (db.updateNote(note)) {
notify = "update success!";
} else {
notify = "update fail!";
}
}
}
Toast.makeText(context, notify, Toast.LENGTH_SHORT).show();
finish();
}
步 4: 更新接口
在数据库文件中进行编辑后,, 我们回到编辑界面相应.
每个项目现在将显示更多 1 学校是在底部左侧LAST_MODIFIED.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
card_view:cardCornerRadius="1dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/backgroud_item_note"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="1"
android:text="@string/title_note"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="1"
android:text="@string/content"
android:textSize="13sp" />
<TextView
android:id="@+id/tv_last_modified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:textSize="13sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
最后,相应的修改遣散适配器. 我们只需要修复 2 将类 RecyclerViewHolder 在ItemNoteAdapter和方法 onBindViewHolder
/**
* ViewHolder for item view of list
*/
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements
OnClickListener {
public LinearLayout container;
public TextView tvTitle;
public TextView tvContent;
public TextView tvLastModified; //add
public RecyclerViewHolder(View itemView) {
super(itemView);
container = (LinearLayout) itemView.findViewById(R.id.item_container);
tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
tvContent = (TextView) itemView.findViewById(R.id.tv_content);
tvLastModified = (TextView) itemView.findViewById(R.id.tv_last_modified);
container.setOnClickListener(this);
}
// click item then display note
@Override
public void onClick(View v) {
MainActivity.showNote(context, list.get(getPosition()).getId());
}
}
/**
* set data for item
*/
@Override
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) {
Note note = list.get(position);
viewHolder.tvTitle.setText(note.getTitle());
viewHolder.tvContent.setText(note.getContent());
viewHolder.tvLastModified.setText(note.getLastModified());
}
紧固件是最重要的职位已完成. 接下来的部分,我将指导您如何将数据备份到存储卡.
在本教程的帖子 数据库的Android仲 由 nguyenvanquan7826



你自己问, 为什么我这样做,那么小时分享出来. 它只是 “2017-03-31 00:56:07”. 营业时间 00
您的评论日期时间码格式见星光.