左晓为主开发手持机充值管理机
zuoxiao
2025-04-11 040f1aba13b179ff318366680a6346af7fd97795
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
package com.dayu.general.dao
 
import android.content.Context
import android.os.Environment
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import java.io.File
 
class BaseDaoSingleton {
 
    companion object {
        var baseDao: AppDataBase? = null
        var AsynchBaseDao: AppDataBase? = null
        @JvmField
        val SqlitePath: String =
            Environment.getExternalStorageDirectory().absolutePath + File.separator + ".dayu" + File.separator + "data" + File.separator
 
        // 数据库迁移策略:从版本3迁移到版本4
        private val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 这里不需要实际的数据库结构修改,因为我们只是更新了Room版本
                // 如果需要修改数据库结构,可以在这里添加 ALTER TABLE 语句
            }
        }
 
        fun getInstance(context: Context): AppDataBase {
            if (baseDao == null) {
                baseDao = Room.databaseBuilder<AppDataBase>(
                    context,
                    AppDataBase::class.java,
                    SqlitePath + "ConfigurationData_generalV1"
                ).allowMainThreadQueries().build()
            }
            return baseDao as AppDataBase
        }
        fun getAsynchInstance(context: Context): AppDataBase {
            if (AsynchBaseDao == null) {
                synchronized(this) {
                    if (AsynchBaseDao == null) {
                        AsynchBaseDao = Room.databaseBuilder(
                            context.applicationContext,
                            AppDataBase::class.java,
                            "GeneralLibrary.db"
                        )
                        .addMigrations(MIGRATION_3_4) // 添加迁移策略
                        .build()
                    }
                }
            }
            return AsynchBaseDao as AppDataBase
        }
 
        // 销毁数据库实例
        fun destroyInstance() {
            AsynchBaseDao = null
        }
    }
 
 
 
 
 
//    companion object {
//        fun getAsynchInstance(baseActivity: BaseActivity): AppDataBase? {
//           return getAsynchInstance(baseActivity)
//        }
//    }
}