Android实现风蓝记事本(2):数据库设计

Android实现风蓝记事本(2):数据库设计:

要实现记事本记事能力,得设计一个数据库,要有一张表来保存记事记录。

package com.example.notes;

        import java.sql.SQLData;

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

public class DatabaseHelper extends SQLiteOpenHelper{

    private String tableName="notes";//表名
    private Context mContext=null;
    private String sql="create table if not exists "+tableName+
            "( id Integer primary key autoincrement , "+
            "n_title varchar(30) NOT NULL,"+//标题
            "n_content text NOT NULL ,"+//内容
            "n_time varchar(30))";//时间
    public DatabaseHelper(Context context, String name, CursorFactory factory,
                          int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        //创建表
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

}


把对数据表的增删改查操作封装在一个类:




package com.example.notes;

        import android.content.ContentValues;
        import android.content.Context;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.database.sqlite.SQLiteException;
        import android.util.Log;

public class DatabaseManage {
    private Context mContext = null;

    private SQLiteDatabase mSQLiteDatabase = null;// 用于操作数据库的对象
    private DatabaseHelper dh = null;// 用于创建数据库的对象

    private String dbName = "notes.db";
    private int dbVersion = 1;

    public DatabaseManage(Context context) {
        mContext = context;
    }

    public DatabaseManage() {
        // TODO Auto-generated constructor stub
    }

    /**
     * 打开数据库
     */
    public void open() {

        try {
            dh = new DatabaseHelper(mContext, dbName, null, dbVersion);
            if (dh == null) {
                // Log.v("msg", "is null");
                return;
            }
            mSQLiteDatabase = dh.getWritableDatabase();
            // dh.onOpen(mSQLiteDatabase);

        } catch (SQLiteException se) {
            se.printStackTrace();
        }
    }

    /**
     * 关闭数据库
     */
    public void close() {

        mSQLiteDatabase.close();
        dh.close();

    }

    // 获取列表
    public Cursor selectAll(boolean sort_desc) {
        Cursor cursor = null;
        String sql = null;
        try {
            sql = "select * from notes order by n_time "
                    + (sort_desc != true ? "" : "desc");// 倒序
            cursor = mSQLiteDatabase.rawQuery(sql, null);
        } catch (Exception ex) {
            ex.printStackTrace();
            cursor = null;
        }
        return cursor;
    }

    public Cursor selectById(int id) {

        // String result[] = {};
        Cursor cursor = null;
        try {
            String sql = "select * from notes where id='" + id + "'";
            cursor = mSQLiteDatabase.rawQuery(sql, null);
        } catch (Exception ex) {
            ex.printStackTrace();
            cursor = null;
        }

        return cursor;
    }

    // 根据内容查找
    public Cursor selectWord(String word) {
        Cursor cursor = null;
        System.out.println("---data----word" + word);
        try {
            String sql = " select * from notes where n_title like '" + "%"
                    + word + "%' or n_content like '" + "%" + word
                    + "%' order by n_time desc";
            cursor = mSQLiteDatabase.rawQuery(sql, null);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            cursor = null;
        }
        return cursor;
    }

    // 插入数据
    public long insert(String title, String content, String time) {

        long flag = -1;
        try {
            ContentValues cv = new ContentValues();
            cv.put("n_title", title);
            cv.put("n_content", content);
            cv.put("n_time", time);
            System.out.println("----insert"+time);
            flag = mSQLiteDatabase.insert("notes", null, cv);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return flag;

    }

    // 删除数据
    public int delete(long id) {
        int affect = 0;
        try {
            affect = mSQLiteDatabase.delete("notes", "id=?", new String[] { id
                    + "" });
        } catch (Exception ex) {
            ex.printStackTrace();
            affect = -1;
        }

        return affect;
    }

    // 修改数据
    public int update(int id, String title, String content, String time) {
        int affect = 0;
        try {
            ContentValues cv = new ContentValues();

            cv.put("n_title", title);
            cv.put("n_content", content);
            cv.put("n_time", time);
            String w[] = { id + "" };
            affect = mSQLiteDatabase.update("notes", cv, "id=?", w);
        } catch (Exception ex) {
            ex.printStackTrace();
            affect = -1;
        }
        return affect;
    }

}


我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。


猜你喜欢

转载自blog.csdn.net/weixin_39220472/article/details/80383049