当前位置:   article > 正文

Android studio创建sqlite数据库以及数据库简易使用方法_androidstudio如何手动输入数据库

androidstudio如何手动输入数据库

1.创建一个类并继承 SQLiteOpenHelper

SQLiteOpenHelper是一个抽象类,并且是一个孤立的抽象类,通过创建一个子类继承SQLiteOpenHelper类,并实现其中的一些方法来对数据库进行操作。

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG="DatabaseHelper";
    private SQLiteDatabase sqLiteDatabase;

    /**
     *
     * @ context   上下文
     * @ name      数据库名称
     * @ factory   游标
     * @ version   版本号
     */

    public DatabaseHelper(Context context) {
        super(context, Database.DATABASE_NAME, null, Database.VERSION_CODE);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //OnCreate 方法;创建时回调
        Log.d(TAG,"创建数据库");
        //创建字段   这种方法是通过写好sql语句,然后交给execSQL()方法执行,来创建;
        //sql : create table table_name(id integer,GradeUnit integer,Unit integer,English varchar, Chinese varchar)
        //Database.TABLE_NAME = "vocabulary"  是在常量文件中写好的
        String sql ="create table "+Database.TABLE_NAME+" (id integer,GradeUnit integer,Unit integer,English varchar,Chinese varchar)";
        sqLiteDatabase.execSQL(sql);
    }

    /**
     *
     * @param sqLiteDatabase  资源器
     * @param i :oldversion  旧的版本号
     * @param i1:newversion  新的版本号
     */
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //OnUpdate方法 更新数据库时,回调
        Log.d(TAG,"更新数据库");

        //添加字段
        //alter table table_name add UNIT integer;
       String sql;

        //判断要采用的版本,进行修改
        switch (i){
            case 1:
                //版本一 则需要添加GradeUnit 和 Unit 字段
                //注意sql语句不要写错,注意sql语法
                sql = "alter table " + Database.TABLE_NAME + " add GradeUnit integer";
                sqLiteDatabase.execSQL(sql);
                sql = "alter table " + Database.TABLE_NAME + " add Unit integer";
                sqLiteDatabase.execSQL(sql);
                break;
            case 2:
                //版本2 添加Unit这个字段
                sql = "alter table " + Database.TABLE_NAME + " add Unit varchar(10)";
                sqLiteDatabase.execSQL(sql);
                break;
            case 3:

                break;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

对数据库的增删改查操作

创建一个Dao类,实现对数据库的增删改查

/**
 * @类名 : Dao
 * @作者 : SUNX
 * @描述 :数据库的增删改查
 */
public class Dao {

    private static final String TAG = "Dao" ;
    private final DatabaseHelper mhelper;

    public Dao(Context context){
        //创建数据库
        mhelper = new DatabaseHelper(context);
    }

    public void insert(int id,int GradeUnit,int Unit,String En,String Ch){
        SQLiteDatabase db = mhelper.getWritableDatabase();
        //添加数据
        //1.写好sql语句  insert into table_name (id,GradeUnit,Unit,English,Chinese)
        //2.将sql语句交给SQLiteDatabase.execSQL()执行
        //3.将SQLiteDatabase资源关闭
        //String sql = "insert into "+ Database.TABLE_NAME + " (id,GradeUnit,Unit,English,Chinese) values (?,?,?,?,?)";
        //db.execSQL(sql,new Object[]{id,GradeUnit,Unit,En,Ch});

        //API插入数据
        ContentValues values = new ContentValues();
        values.put("id",id);
        values.put("GradeUnit",GradeUnit);
        values.put("Unit",Unit);
        values.put("English",En);
        values.put("Chinese",Ch);
        db.insert(Database.TABLE_NAME,null,values);
        db.close();
    }

    public void delete(int id,int GradeUnit,int Unit,String En,String Ch){
        SQLiteDatabase db = mhelper.getWritableDatabase();
        //删除数据
        //1.写好sql语句 delete from table_name where conditions
        //2.将sql语句交给SQLiteDatabase.execSQL()执行
        //3.将SQLiteDatabase资源关闭
        String sql = "delete from " + Database.TABLE_NAME +" where English = '" +En + "'";
        db.execSQL(sql);
        db.close();
    }

    public void delete(int GradeUnit,int Unit,String English,String Chinese){
        SQLiteDatabase db = mhelper.getWritableDatabase();
        //删除数据
        //1.写好sql语句 delete from table_name where conditions
        //2.将sql语句交给SQLiteDatabase.execSQL()执行
        //3.将SQLiteDatabase资源关闭
        String sql = "delete from " + Database.TABLE_NAME +" where GradeUnit =" + GradeUnit;
        db.execSQL(sql);
        db.close();
    }


    public void update(int id,int GradeUnit,int Unit,String En,String Ch){
        SQLiteDatabase db = mhelper.getWritableDatabase();
        //更改数据
        //1.写好sql语句 update table_name set ***  where conditions
        //2.将sql语句交给SQLiteDatabase.execSQL()执行
        //3.将SQLiteDatabase资源关闭
        //String sql = "update " + Database.TABLE_NAME + " set Chinese = '"+ Ch+"' where English = '"+En+"'" ;
        //db.execSQL(sql);

        /**
         * SQLiteDatabase.update()
         * @param table : 表名
         * @param values : 修改的值  类型为 ContentValues 通过put方法加入
         * @param whereClause 修改的条件key  "English = ?"
         * @param whereArgs  修改该条件的条件值value String[] args = "ear"
         * whereClause + whereArgs 等同于 where English = 'ear'
         */
        ContentValues values = new ContentValues();
        values.put("Chinese",Ch);
        String condition ="English = ?";
        String[] args = {String.valueOf("ear")};
        db.update(Database.TABLE_NAME,values,condition,args);

        db.close();
    }

    public void query(int GradeUnit){
        SQLiteDatabase db = mhelper.getReadableDatabase();
        //查询数据
        //1.写好sql语句 select * from table_name where conditions
        //2.将sql语句交给SQLiteDatabase.execSQL()执行
        //3.将SQLiteDatabase资源关闭
        /*String sql = "select * from " + Database.TABLE_NAME + " where GradeUnit ="+ GradeUnit;
        Cursor cursor = db.rawQuery(sql,null);
        while (cursor.moveToNext()){
            int index = cursor.getColumnIndex("English");
            String name = cursor.getString(index);
            Log.d( TAG,"English ==" + name);
        }
        cursor.close();
        */

        /**
         * @param String[] columns 要提取的关键字键值key
         * @param String selection 查询条件的键值key
         * @param String[] selectionArgs 查询条件的value值
         * 相当于select String[] columns from table_name where String selection = "selectinoArgs"  
         */
        String[] columns = new String[]{"English"};
        String selection = "GradeUnit = ?";
        String[] selectionArgs =  {String.valueOf(33)};
        db.query(Database.TABLE_NAME,columns,selection,selectionArgs,null,null,null);

        db.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

总结

初次接触Android studio中数据库方面的事务,总结一些方法和例子。希望能给你们提供一些想法和思路。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/636275
推荐阅读
相关标签
  

闽ICP备14008679号