在资产文件夹中加载大于1 mb的数据库sqlite

时间:2015-03-11 08:32:03

标签: android sqlite assets

如何在资产文件夹中加载大于1 MB的数据库?我有一个数据库文件,我将其复制到资产文件夹然后通过代码复制到数据目录,但因为它大于1 MB,所以会出错!

1 个答案:

答案 0 :(得分:0)

SQLiteAssetHelper库使这项任务非常简单 jar文件和文档位于https://github.com/jgilfelt/android-sqlite-asset-helper

如文档中所述:

  1. android-sqlite-asset-helper.jar添加到项目libs目录。
  2. 使用assets/databases/database_name.zip
  3. 等zip文件压缩数据库
  4. 创建一个类:
  5. 示例:

    public class MyDatabase extends SQLiteAssetHelper {
         private static final String DATABASE_NAME = "database_name";
         private static final int DATABASE_VERSION = 1;    
         public MyDatabase(Context context) {
             super(context, DATABASE_NAME, null, DATABASE_VERSION);  
         }  
     }
    

    修改

    public Cursor getEmployees() {
    
            SQLiteDatabase db = getReadableDatabase();
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    
            String [] sqlSelect = {"0 _id", "FullName"}; 
            String sqlTables = "Employees";
    
            qb.setTables(sqlTables);
            Cursor c = qb.query(db, sqlSelect, null, null,
                    null, null, null);
    
            c.moveToFirst();
            return c;
    
        }
    
        public int getUpgradeVersion() {
    
            SQLiteDatabase db = getReadableDatabase();
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    
            String [] sqlSelect = {"MAX (version)"};
            String sqlTables = "upgrades";
    
            qb.setTables(sqlTables);
            Cursor c = qb.query(db, sqlSelect, null, null,
                    null, null, null);
    
            int v = 0;
            c.moveToFirst();
            if (!c.isAfterLast()) {
                v = c.getInt(0);
            }
            c.close();
            return v;
        }