`
mars5337
  • 浏览: 89341 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

android 数据库工具类MyDbHelper

阅读更多
package com.tdd.db.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDbHelper {
    public static final String KEY_TITLE = "title";    //数据表字段
    public static final String KEY_BODY = "body";    //数据表字段
    public static final String KEY_ROWID = "_id";    //数据表字段

    private DatabaseHelper mDbHelper;    //SQLiteOpenHelper实例对象
    private SQLiteDatabase mDb;    //数据库实例对象
   
    //数据表创建语句
    private static final String DATABASE_CREATE  = "create table notes (_id integer primary key autoincrement, "
                    + "title text not null, body text not null);";

    private static final String DATABASE_NAME = "data";    //数据库名
    private static final String DATABASE_TABLE = "notes";    //数据库表名
    private static final int DATABASE_VERSION = 2;    //数据库版本号

    private final Context mCtx;    //上下文实例

    private static class DatabaseHelper extends SQLiteOpenHelper {    //数据库辅助类
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be opened/created
     *
     * @param ctx the Context within which to work
     */
    public MyDbHelper(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new instance of the database.
* If it cannot be created, throw an exception to signal the failure
     *
     * @return this (self reference, allowing this to be chained in an initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public MyDbHelper open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
   
    public void close() {
        mDbHelper.close();
    }


    /**
     * Create a new note using the title and body provided. If the note is successfully created
* return the new rowId for that note, otherwise return a -1 to indicate failure.
     *
     * @param title the title of the note
     * @param body the body of the note
     * @return rowId or -1 if failed
     */
    public long createNote(String title, String body) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_BODY, body);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
     * Delete the note with the given rowId
     *
     * @param rowId id of note to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteNote(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all notes in the database
     *
     * @return Cursor over all notes
     */
    public Cursor fetchAllNotes() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,KEY_BODY}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     *
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchNote(long rowId) throws SQLException {

        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_TITLE,
KEY_BODY}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    /**
     * Update the note using the details provided. The note to be updated is specified using the rowId,
*and it is altered to use the title and body values passed in
     *
     * @param rowId id of note to update
     * @param title value to set note title to
     * @param body value to set note body to
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateNote(long rowId, String title, String body) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_BODY, body);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
分享到:
评论

相关推荐

    各种数据库工具类

    在IT行业中,数据库工具类是开发人员日常工作中不可或缺的一部分,它们极大地简化了数据操作和管理。这个主题"各种数据库工具类"涵盖了多个方面,包括但不限于数据库连接、查询、事务处理、数据迁移等。下面我们将...

    sqlite android数据库查看工具

    本文将详细介绍如何使用Android数据库查看工具来管理和分析SQLite数据库。 首先,我们需要了解Android数据库的基本操作。Android提供了SQLiteOpenHelper类,它是对SQLite数据库操作的基类,可以帮助我们创建、升级...

    JAVA 通过JDBC连接数据库工具类

    JDBC连接数据库工具类 JDBC 连接数据库 ORACLE SQLSERVER MYSQL ORACLE 已测 OK 欢迎大家反馈

    C# WPF 数据库工具类

    C# WPF sqlserver等增删查改数据库工具类

    sqlite数据库工具类

    在这个"sqlite数据库工具类"中,开发者已经基于Room技术封装了数据库操作,主要包括`DbManager`类和`DbHelper`类,这两个类是进行数据库管理和帮助操作的核心组件。 首先,我们来看`DbManager`类。这个类通常作为...

    DBUtil(ASP。NET数据库连接工具类)

    DBUtil 是一个在ASP.NET开发环境中常用的数据库连接工具类,它的设计目的是为了简化数据库操作,减少程序员编写重复的连接和断开数据库的代码,从而提高开发效率和代码的可维护性。通过使用DBUtil,开发者可以快速地...

    Android常用工具类集合

    - 数据存储工具类,如SharedPreferences或SQLite数据库操作,便于保存用户数据或应用配置。 3. **基础视图组件封装**: - 自定义View和Adapter的封装,可以简化UI开发,提供更丰富的交互效果。例如,自定义的滑动...

    android高德地图封装工具类

    android高德地图封装工具类地图定位,绘制路径等等android高德地图封装工具类地图定位,绘制路径等等android高德地图封装工具类地图定位,绘制路径等等android高德地图封装工具类地图定位,绘制路径等等android高德...

    C# ASP.NET 连接数据库工具类

    便捷的操作sqlserver数据库的数据库操作类,可以实现存储过程及语句的灵活调用。

    图片、文件上传、数据库连接工具类

    一个数据库连接工具类可以简化数据库操作,提供连接池管理,事务处理等功能。 - **JDBC API**:包括Connection、Statement、PreparedStatement和ResultSet等接口,分别用于建立数据库连接,执行SQL语句和处理查询...

    Android数据库Demo-GreenDaoTest.zip

    3. **创建Dao(Data Access Object)**:GreenDao通过`DaoGenerator`工具,根据定义的实体类生成对应的Dao接口,如`UserDao`。这些接口提供了增删查改的基本方法,如`insert()`, `delete()`, `query()`, `update()`...

    android查看数据库文件,导出数据库方便查看

    总之,查看和导出Android数据库文件,尤其是`external.db`,涉及到ADB操作、数据库管理工具的使用以及对SQLite数据库的理解。通过这些工具和技术,开发者可以方便地调试和分析应用的数据库内容,从而优化性能、修复...

    KingbaseDTS数据库迁移工具

    KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具KingbaseDTS数据库迁移工具...

    Android上传文件工具类

    Android上传文件工具类

    Android常用工具类

    以上只是Android开发中工具类的一部分,实际上,根据项目的不同需求,还可以创建更多针对性的工具类,如数据库操作工具类、颜色处理工具类等。通过合理地组织和利用这些工具类,可以使得Android应用的代码更加规范、...

    批量附加SQL数据库工具

    在IT领域,数据库管理是至关重要的任务之一,尤其是在企业级应用中。SQL(Structured Query Language)数据库因其高效、稳定和广泛支持的特点,被...了解并熟练掌握这类工具的使用,对提升数据库管理工作效率至关重要。

    u8批量附加数据库工具

    对于企业而言,这类工具的使用不仅可以优化数据库管理流程,还能减少人为错误,提升IT运维的规范化水平。同时,对于数据库管理员来说,它是一个不可或缺的辅助工具,能够有效提升他们的工作效率,使他们能将更多精力...

    查看android的数据库文件db工具

    本文将详细介绍如何使用一个名为SQLiteBrowser的工具来查看Android设备上的数据库文件(db)。 SQLiteBrowser是一款开源、免费的图形界面工具,它允许开发者直观地浏览、编辑和操作SQLite数据库。它支持创建、打开...

    数据库的查询、修改的工具类

    这是一个关于数据库的查询、修改、删除、添加的工具类 实用 方便

    WPF连接Access数据库工具类

    C#&WPF;连接Access,进行增、删、改、查等以及将数据库数据写入实体类等操作工具类。并细化工具类方法,主要针对于单个值、单条、多条数据查询,照片写入、查询等工具方法。

Global site tag (gtag.js) - Google Analytics