1

我有很大的 XML 文件(75MB)。当我把它放在 res/xml 文件夹中时,Eclipse 冻结。可能和我的电脑配置有关,我不知道(2.0GHz CPU,2GB RAM)。现在我要将 XML 划分为 1-2MB 的部分并将其转换为 sqlite db。这样做值得吗?或者有没有更好的方法来做到这一点?转换后我可以将 sqlite 数据库放在包(.apk)上吗?我需要你的建议。

4

1 回答 1

1

这是您如何将 sqlite 数据库放入您的 apk 中的方法,或者如果它尚未退出,则实际为您的应用程序加载它。您创建或添加相关部分到您的 DBHelper/DBContractor。您必须将您的 sqlite 数据库放在项目的 /res/assets 文件夹中。这就是它要读取它的地方。

    public class DbHelper extends SQLiteOpenHelper {

public static String DATABASE_PATH;
public static final String DATABASE_NAME = "yourdatabasename.db";
private static final int DATABASE_VERSION = 1;
private Context context;
private SQLiteDatabase db;

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
    String packageName = context.getPackageName();
    DATABASE_PATH = String.format("//data//data//%s//databases//",
            packageName);
    openDataBase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    TDAdb.onCreate(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    TDAdb.onUpgrade(db, oldVersion, newVersion);
}

// Performing a database existence check
private boolean checkDataBase() {
    SQLiteDatabase checkDb = null;
    try {
        String path = DATABASE_PATH + DATABASE_NAME;
        checkDb = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLException e) {
        Log.e(this.getClass().toString(), "Error while checking db");
    }

    if (checkDb != null) {
        checkDb.close();
    }
    return checkDb != null;
}

// Method for copying the database
private void copyDataBase() throws IOException {

    InputStream externalDbStream = context.getAssets().open(DATABASE_NAME);

    String outFileName = DATABASE_PATH + DATABASE_NAME;

    OutputStream localDbStream = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = externalDbStream.read(buffer)) > 0) {
        localDbStream.write(buffer, 0, bytesRead);
    }

    localDbStream.close();
    externalDbStream.close();
}

public void createDataBase() {

    boolean dbExist = checkDataBase();
    if (!dbExist) {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            Log.e(this.getClass().toString(), "Copying error");
            throw new Error("Error copying database!");
        }
    } else {
        //Log.i(this.getClass().toString(), "Database already exists");
    }
}

public SQLiteDatabase openDataBase() throws SQLException {
    String path = DATABASE_PATH + DATABASE_NAME;

    if (db == null) {
        createDataBase();
        db = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    return db;
}

}

希望这可以帮助。

这是 TDAdb 的源代码,它基本上是一个 java 类,用作我的内容提供者与手机上其他应用程序公开可用的内容之间的合同。它部分是为了安全性和一致性,例如提供列名、原始查询、表名等。如果需要,您可以在应用程序中将列名和表名指定为字符串。

package com.birdsall.tda;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TDAdb {

public static final String LOG_TAG = "TDAdb";

// tables
public static final String CHAPTER_TABLE = "chapters";
public static final String RULE_TABLE = "rules";
public static final String INDEXHEADER_TABLE = "indexheader";
public static final String KEYDESCRIPTORS_TABLE = "keydescriptors";
public static final String RULEREFERENCES_TABLE = "rulereferences";
public static final String SEARCHREQUEST_TABLE = "searchrequest";
public static String SQLITE_TABLE = "chapters";

// Chapter Table Columns
public static final String KEY_ROWID = "_id";
public static final String COL_CHAPTER = "chapter";
public static final String COL_CHAPTERTITLE = "chaptertitle";

// Searchrequest Columns
public static final String COL_ID = "id";

// Rule Table Columns
public static final String COL_RULE = "rule";
// public static final String COL_CHAPTER = "chapter";
public static final String COL_KEYDESCRIPTOR = "keydescriptor";
public static final String COL_RULETITLE = "ruletitle";
public static final String COL_DESCR = "descr";
public static final String COL_DESCRIPTION = "description"; // LongText
public static final String COL_LABEL = "label";
public static final String COL_USERLABEL = "userlabel";
public static final String COL_LABELID = "labelid"; // Integer
public static final String COL_USERLABELID = "userlabelid"; // Integer
public static final String COL_TDTIP = "tdtip";
public static final String COL_DEFUNCT = "defunct";
public static final String COL_USCFREVISION = "uscfrevision";
public static final String COL_HIGHLIGHT = "highlight"; // LongText
public static final String COL_PAGENO = "pageno";
public static final String COL_CHANGEDATE = "changedate"; // Integer
public static final String COL_REFINC = "refinc";
public static final String COL_RULEINC = "ruleinc";

// keydescriptors table columns
public static final String COL_KEYDESC = "keydesc";
public static final String COL_IMAGEREFERENCE = "imagereference";

// indexheader table columns
public static final String COL_INDEX_HEADER = "index_header";
public static final String COL_INDEXID = "indexid";
public static final String COL_INDEXLEXICON = "indexlexicon";

// rulereferences table columns
public static final String COL_RULEREFERENCE = "rulereference";
public static final String COL_REF_ORDER = "ref_order";

// columns defined in RAW Queries via AS
public static final String COLRAW_XREFRULE = "xrefrule";
public static final String COL_PROJ_THECOUNT = "count(*) as thecount";
public static final String COLTHECOUNT = "thecount";

public static final String[] PROJECTION_CHAPTER = { KEY_ROWID, COL_CHAPTER,
        COL_CHAPTERTITLE, };

public static final String[] PROJECTION_RULES = { KEY_ROWID, COL_CHAPTER,
        COL_RULE, COL_RULETITLE, COL_KEYDESCRIPTOR, };

public static final String[] PROJECTION_RULES_RULE = { COL_RULE, COL_RULETITLE, COL_KEYDESCRIPTOR, };

public static final String[] PROJECTION_RULEREFERENCE = { KEY_ROWID,
        COL_RULEREFERENCE, COL_REF_ORDER, };

public static final String[] PROJECTION_SEARCHREQUEST = { KEY_ROWID,
        COL_ID, };

/* ************** MAKE SURE THERE'S and _id *********************** */
public static final String RAWQUERY_RULESDESCRIBED = "select rules._id, rule, chapter, ruletitle, rules.keydescriptor, keydesc FROM rules LEFT OUTER JOIN keydescriptors ON rules.keydescriptor = keydescriptors.keydescriptor WHERE chapter = ?  and rules.keydescriptor = '2' group by 1, 2, 3, 4, 5 order by 1, 4";
public static final String RAWQUERY_RULESCROSSREF = "SELECT rules._id, rules.rule, rules.ruletitle,  rulereferences.rule AS xrefrule, rulereferences.ref_order FROM rulereferences INNER JOIN rules ON rulereferences.rulereference = rules.rule WHERE (((rules.keydescriptor)='2') AND ((rulereferences.rule)= ? )) ORDER BY rulereferences.ref_order, rules.rule";
public static final String RAWQUERY_RULESCROSSREFDETAIL = "SELECT rules._id, rules.rule, rules.ruletitle, rules.keydescriptor, keydescriptors.keydesc FROM rules INNER JOIN keydescriptors ON rules.keydescriptor = keydescriptors.keydescriptor WHERE (((rules.rule)= ? )) GROUP BY rules.rule, rules.ruletitle, rules.keydescriptor, keydescriptors.keydesc ORDER BY rules.rule, Min(rules.keydescriptor)";
public static final String RAWQUERY_INDEX = "SELECT DISTINCT _id, indexlexicon FROM indexheader GROUP BY 2 ORDER BY 2";
public static final String RAWQUERY_INDEXCHILD = "SELECT indexheader._id, indexheader.index_header, indexer.rule, indexheader.indexid, rules.pageno, rules.uscfrevision, rules.defunct FROM (indexheader LEFT JOIN indexer ON indexheader.indexid = indexer.indexid) LEFT JOIN rules ON indexer.rule = rules.rule WHERE (((indexheader.indexlexicon)= ? )) GROUP BY indexheader._id, indexheader.index_header, indexer.rule, indexheader.indexid, rules.pageno, rules.uscfrevision, rules.defunct HAVING (((rules.pageno) Is Not Null)) ORDER BY indexheader.index_header, indexheader.indexid";
public static final String RAWQUERY_RULEDETAIL = "SELECT rules._id, rules.rule, rules.ruletitle, rules.descr, rules.description, rules.keydescriptor, rules.tdtip, rules.uscfrevision, rules.defunct, rules.highlight, rules.label, rules.userlabel, rules.changedate,  rules.chapter, rules.labelid, rules.userlabelid, rules.tdtip, rules.pageno, rules.refinc, rules.ruleinc, keydesc FROM rules LEFT OUTER JOIN keydescriptors ON rules.keydescriptor = keydescriptors.keydescriptor GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 HAVING (((rules.rule)= ? )) ORDER BY 1, 5";
public static final String RAWQUERY_CLUBDETAIL = "SELECT rules._id, rules.rule, rules.ruletitle, rules.descr, rules.description, rules.keydescriptor, rules.tdtip, rules.uscfrevision, rules.defunct, rules.highlight, rules.label, rules.userlabel, rules.changedate,  rules.chapter, rules.labelid, rules.userlabelid, rules.tdtip, rules.pageno, rules.refinc, rules.ruleinc FROM rules  WHERE rules.keydescriptor = '1' AND (((rules.rule)= ? )) ORDER BY 1, 5";
public static final String RAWQUERY_RULEDETAIL_KEY = "SELECT rules._id, rules.rule, rules.ruletitle, rules.descr, rules.description, rules.keydescriptor, rules.tdtip, rules.uscfrevision, rules.defunct, rules.highlight, rules.label, rules.userlabel, rules.changedate,  rules.chapter, rules.labelid, rules.userlabelid, rules.tdtip, rules.pageno, rules.refinc, rules.ruleinc, keydesc FROM rules LEFT OUTER JOIN keydescriptors ON rules.keydescriptor = keydescriptors.keydescriptor WHERE rules._id = ? GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20  ORDER BY 1, 5";
public static final String RAWQUERY_LABEL = "select distinct _id, label, count(*) as thecount from rules group by 2 order by label";
public static final String RAWQUERY_LABELDETAIL = "SELECT rules._id, rules.rule, rules.ruletitle, keydescriptors.keydesc FROM rules INNER JOIN keydescriptors ON rules.keydescriptor = keydescriptors.keydescriptor WHERE (((rules.label)= ? )) GROUP BY rules.rule, rules.ruletitle, keydescriptors.keydesc ORDER BY rules.rule, Min(rules.keydescriptor)";
public static final String RAWQUERY_USERLABEL = "select distinct _id, userlabel, count(*) as thecount from rules group by 2 order by userlabel";
public static final String RAWQUERY_USERLABELDETAIL = "SELECT rules._id, rules.rule, rules.ruletitle, keydescriptors.keydesc FROM rules INNER JOIN keydescriptors ON rules.keydescriptor = keydescriptors.keydescriptor WHERE (((rules.userlabel)= ? )) GROUP BY rules.rule, rules.ruletitle, keydescriptors.keydesc ORDER BY rules.rule, Min(rules.keydescriptor)";
public static final String RAWQUERY_ALLTDTIPS = "select distinct _id, rule, ruletitle from rules where keydescriptor = '3' order by rule";
public static final String RAWQUERY_ALLCLUB = "select distinct _id, rule, ruletitle from rules where keydescriptor = '1' order by rule";
public static final String RAWQUERY_RULECLASS = "select distinct _id, rule, ruletitle from rules where keydescriptor = ? order by rule";
public static final String RAWQUERY_RULECLASS_USERLABEL = "select distinct _id, rule, ruletitle from rules where userlabel > ' ' order by rule";
public static final String RAWQUERY_RULECLASS_HIGHLIGHT = "select distinct _id, rule, ruletitle from rules where highlight > ' ' order by rule";

public static void onCreate(SQLiteDatabase db) {

    // Log.i(LOG_TAG, "onCreate");
}

public static void onUpgrade(SQLiteDatabase db, int oldVersion,
        int newVersion) {
    Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");

    onCreate(db);
}
}
于 2013-07-30T18:01:44.980 回答