个人日记本(SQLite实现增删改查)

本文介绍了如何使用SQLite数据库在Android应用中实现日记的增加、删除、修改和查询操作。通过实体类定义日记结构,创建数据库类,并提供布局文件和相关活动的详细说明。
摘要由CSDN通过智能技术生成
效果图:

当程序运行后,首先进入日记列表,显示如图:

长时间点击某个日记,将跳转至修改日记界面:

点击menu后,显示如图:

点击编辑新日志将跳转至添加新日记界面:

选中某个日记,点击删除日记,将弹出提示框:

点击“确定”,删除日记,点击“取消”,返回日记列表页面

具体实现方法:

实体类:

package cn.bzu.mydiary.model;

public class Diary {
	private int id;
	private String title;
	private String content;
	private String pubdate;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getPubdate() {
		return pubdate;
	}
	public void setPubdate(String pubdate) {
		this.pubdate = pubdate;
	}
	
	
	public Diary(String title, String content, String pubdate) {
		super();
		this.title = title;
		this.content = content;
		this.pubdate = pubdate;
	}
	public Diary(int id, String title, String content, String pubdate) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.pubdate = pubdate;
	}
	public Diary(int id, String title, String content) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;		
	}
	

}

创建数据库的类(数据库保存在DDMS/data/data/cn.bzu.mydiary/database/diary.db)

package cn.bzu.mydiary.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{
	private static String DATABASE_NAME="diary.db";
	private static int DATABASE_VERSION=1;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table tb_diary(_id integer primary key autoincrement,title verchar(20),content verchar(1000),pubdate)");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		
	}

}

数据库操作类

package cn.bzu.mydiary.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.bzu.mydiary.db.DBHelper;
import cn.bzu.mydiary.model.Diary;

public class DiaryService {
	private DBHelper dbHelper;
	SQLiteDatabase sqLiteDatabase = null;

	public DiaryService(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * 保存日记
	 * 
	 * @param diary
	 */

	public void save(Diary diary) {
		sqLiteDatabase = dbHelper.getWritableDatabase();
		String sql = "insert into tb_diary(title,content,pubdate) values (?,?,?)";
		sqLiteDatabase.execSQL(
				sql,
				new String[] { diary.getTitle(), diary.getContent(),
						diary.getPubdate() });
		sqLiteDatabase.close();

	}

	// 查询日志
	public List<Diary> getAllDiary() {
		Diary diary = null;
		List<Diary> diaryList = new ArrayList<Diary>();
		sqLiteDatabase = dbHelper.getReadableDatabase();
		// 得到游标,最多只有一条数据
		Cursor cursor = sqLiteDatabase.rawQuery("select * from tb_diary", null);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			String title = cursor.getString(cursor.getColumnIndex("title"));
			String content = cursor.getString(cursor.getColumnIndex("content"));
			String pubdate = cursor.getString(cursor.getColumnIndex("pubdate"));
			diary = new Diary(id, title, content, pubdate);
			diaryList.add(diary);
		}
		cursor.close();
		sqLiteDatabase.close();

		return diaryList;

	}

	/**
	 * 根据id删除日记
	 * 
	 * @param id
	 *            日记的id号
	 */
	public void delete(Integer id) {
		sqLiteDatabase = dbHelper.getReadableDatabase();
		sqLiteDatabase.execSQL("delete from tb_diary where _id=?",
				new Object[] { id });
		sqLiteDatabase.close();

	}

	/**
	 * 更新日记
	 * 
	 * @param diary
	 */
	public void update(Diary diary) {
		sqLiteDatabase = dbHelper.getReadableDatabase();
		sqLiteDatabase.execSQL(
				"update tb_diary set title=?,content=?,pubdate=? where _id=?",
				new Object[] { diary.getTitle(), diary.getContent(),
						diary.getPubdate(), diary.getId() });
		sqLiteDatabase.close();

	}

	// //获取记录总数
	// public long count(){
	// long count=0;
	// SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
	// Cursor cursor=sqLiteDatabase.rawQuery("select count(*) from tb_diary",
	// null);
	// cursor.moveToFirst();
	// count=cursor.getLong(0);
	// return count;
	// }

}

相应的布局文件:activity_add_diary.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title" />

    <EditText
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_co
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值