Andriod数据库操作记录之创建与插入

 

获取外部存储(MTP看到的手机自身的存储,非T卡存储)路径

    public static File getExternalPrivateStorageFile(Context context, String fileName) {
        return new File(context.getExternalFilesDir(null), fileName);
    }

 

打开数据库

    private void openDatabase(@NonNull Context context) {
        if (null == sqlite) {
            String filename = "xxx.db";
            File f = Util.getExternalPrivateStorageFile(context, filename);
            sqlite = SQLiteDatabase.openOrCreateDatabase(f, null);
            sqlite.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME
                    + " ("
                    + "id integer primary key autoincrement,"
                    + KEY_DATE + " TEXT NOT NULL, "
                    + KEY_LOCATION + " TEXT NOT NULL, "
                    + KEY_DETAIL + " TEXT NOT NULL);");
        }

 

向数据库内插入记录

    public void insertToDatabase(@NonNull BillBean bill)  {
        if (null == sqlite) {
            return;
        }

        sqlite.execSQL("INSERT INTO "+TABLE_NAME
                +"("
                +KEY_AMOUNT
                +","+KEY_DATE
                +","+KEY_LOCATION
                +","+KEY_DETAIL
                +") VALUES ("
                + bill.getAmount()
                + ", \"" + getFormattedDateString(bill.getDate()) + "\""
                +",\"" + bill.getLocation()+ "\""
                +",\"" + bill.getDetail()+ "\""
                +");");
    }

 

posted @ 2020-02-17 15:55  -ssdq-  阅读(131)  评论(0编辑  收藏  举报