SQLite数据库浅谈

1.编写类继承SQLitHelper;

public class SQLiteHelper extends SQLiteOpenHelper {
}

2.实现其构造函数;

    public SQLiteHelper(Context context){
        this(context, TABLE_NAME, null, DB_VERSION);
    }
    
    public SQLiteHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, TABLE_NAME, factory, DB_VERSION);
    
    }

3.定义数据库的名称、表名、版本以及表的所有列明

    private static final String DB_NAME = "playerdatas";//数据库名称
    private static final String TABLE_NAME = "datas";//表名
    private static final int DB_VERSION = 1;//数据库版本
    //表的列名
    private static final String TABLE_ID = "_ID"; 
    private static final String TABLE_VIDEOID = "videoId";
    private static final String TABLE_URL = "url";
    private static final String TABLE_TITLE = "title";
    private static final String TABLE_CONTENT = "content";
    private static final String TABLE_ACTORS = "actors";
    private static final String TABLE_DIRECTOR = "director";
    private static final String TABLE_TYPE = "type";
    private static final String TABLE_ICON = "icon";

4.实现onCreate方法,编写SQL语句,创建表

    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
        StringBuilder builder = new StringBuilder();

        builder.append("create table if not exists ");
        builder.append(TABLE_NAME);
        builder.append("(");
        builder.append(TABLE_ID).append(" Integer primary key autoincrement, ");
        builder.append(TABLE_VIDEOID).append(" Integer, ");
        builder.append(TABLE_ICON).append(" Integer, ");
        builder.append(TABLE_URL).append(" varchar(50), ");
        builder.append(TABLE_TITLE).append(" varchar(30), ");
        builder.append(TABLE_CONTENT).append(" varchar(50) ");
        builder.append(" ) ");
        
        db.execSQL(builder.toString());
    }

5 实现onUpgrade方法,当版本升级时调用

    /**
     * 版本更新
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME );
        onCreate(db);

    }

6.编写查询、删除、更新等操作方法。

/**
     * 查询所有数据
     * @return
     */
    public Cursor query(){
        SQLiteDatabase db = getReadableDatabase();
        //获取Corsor
        Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);
        return c;
        
    }
    /**
     * 根据id查找数据
     * @param id
     * @return
     */
    public Cursor queryById(int videoId){
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query(TABLE_NAME, new String[]{TABLE_VIDEOID}, "videoId=?", 
                new String[]{String.valueOf(videoId)}, null, null, null);
        return c;
    }
    
    /**
     * 根据唯一标识_ID  来删除数据  
     * @param id
     */
    public void delete(int id){
        SQLiteDatabase db = getWritableDatabase();
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>"+db);
        db.delete(TABLE_NAME, "_ID=?", new String[]{String.valueOf(id)});

    }
    /**
     * 插入方法
     * @param values插入值
     */
    public void insert(ContentValues values){
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

7.关闭数据库的方法

    /**
     * 关闭数据库
     */
    public void close(){
        if(db != null){
            db.close();
        }
    }
原文地址:https://www.cnblogs.com/wei1228565493/p/4424308.html