为什么在外部存储上创建此数据库?

时间:2014-08-27 07:30:38

标签: android sql sqlite

我正在处理一些遗留代码。

我注意到如果我卸载了应用程序,数据库就不会被删除(当我选择时我会得到“旧记录”),

因此我不得不假设数据库是在外部存储上创建的,

但我不明白这是怎么发生的,因为没有给出数据库的路径。

请你帮我理解发生的事情???

这是助手:

private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
    public CustomSQLiteOpenHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String s;
        try {
            Toast.makeText(context, "1", 2000).show();
            InputStream in = context.getResources().openRawResource(
                    R.raw.sql);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(in, null);
            NodeList statements = doc.getElementsByTagName("statement");
            for (int i = 0; i < statements.getLength(); i++) {
                s = statements.item(i).getChildNodes().item(0)
                        .getNodeValue();
                // Log.d("SQL:", s);
                db.execSQL(s);
            }
        } catch (Throwable t) {
            Toast.makeText(context, t.toString(), 50000).show();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS jobs");
        onCreate(db);
    }

}

这是R.raw.sql (执行的SQL语句):

<sql>

<statement>
CREATE TABLE IF NOT EXISTS [jobs] (
  [jobNo] TEXT NOT NULL UNIQUE COLLATE NOCASE, 
  [test_id] NUMERIC NOT NULL,
  [totalqty] NUMERIC NOT NULL, 
  [testedqty] NUMERIC NOT NULL DEFAULT 0,
  [passedqty] NUMERIC NOT NULL DEFAULT 0,
  [created] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')), 
  [LastUpdated] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')),
  [LastReportedRecord] NUMERIC NOT NULL DEFAULT 0,
  [ReportNumber] NUMERIC NOT NULL DEFAULT 0,
  [active] NUMERIC NOT NULL DEFAULT 0
  );
</statement>

<statement>  
CREATE TRIGGER job_lastupdate
AFTER UPDATE ON jobs FOR EACH ROW
BEGIN
UPDATE jobs
SET LastUpdated = (datetime('now','localtime'))
WHERE rowid = old.rowid;
END;
</statement>

<statement>
CREATE TABLE IF NOT EXISTS [recipients] (
  [email] TEXT NOT NULL UNIQUE COLLATE NOCASE 
  );
</statement>


<statement>
CREATE TABLE IF NOT EXISTS [test] (
  [desc] TEXT NOT NULL COLLATE NOCASE, 
  [type] INTEGER NOT NULL, 
  [created] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime'))
  );
</statement>

<statement>INSERT INTO test (desc, type) VALUES('Open Test', 1);</statement>
<statement>INSERT INTO test (desc, type) VALUES('Closed Test', 2);</statement>

<statement>
CREATE TABLE IF NOT EXISTS [testlimit] (
  [desc] TEXT NOT NULL COLLATE NOCASE, 
  [test_id] INTEGER NOT NULL,
  [seqNo] INTEGER NOT NULL,
  [s0lower] NUMERIC NOT NULL, 
  [s1lower] NUMERIC NOT NULL, 
  [s2lower] NUMERIC NOT NULL,  
  [s0upper] NUMERIC NOT NULL, 
  [s1upper] NUMERIC NOT NULL, 
  [s2upper] NUMERIC NOT NULL, 
  [stability] NUMERIC NOT NULL, 
  [created] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')),
  [modified] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime'))
  );
</statement>

<statement>  
CREATE TRIGGER testlimit_lastupdate
AFTER UPDATE ON testlimit FOR EACH ROW
BEGIN
UPDATE testlimit
SET modified = (datetime('now','localtime'))
WHERE rowid = old.rowid;
END;
</statement>

<statement>INSERT INTO testlimit (desc, test_id, seqNo, s0lower, s1lower, s2lower,     s0upper, s1upper, s2upper, stability) VALUES('Rest', 1, 1, 0, 0, 0, 25, 25, 25, 100);    </statement>
<statement>INSERT INTO testlimit (desc, test_id, seqNo, s0lower, s1lower, s2lower,     s0upper, s1upper, s2upper, stability) VALUES('Low', 1, 2, 800, 900, 900, 1000, 1100, 1100,     50);</statement>
<statement>INSERT INTO testlimit (desc, test_id, seqNo, s0lower, s1lower, s2lower,     s0upper, s1upper, s2upper, stability) VALUES('High', 1, 3, 3600, 2900, 2900, 3699, 3100,     3100, 50);</statement>

<statement>INSERT INTO testlimit (desc, test_id, seqNo, s0lower, s1lower, s2lower,         s0upper, s1upper, s2upper, stability) VALUES('Rest', 2, 1, 0, 0, 0, 4095, 4095, 4095, 4095);    </statement>
<statement>INSERT INTO testlimit (desc, test_id, seqNo, s0lower, s1lower, s2lower,     s0upper, s1upper, s2upper, stability) VALUES('50g', 2, 2, 500, 800, 800, 1800, 3000, 3000,     100);</statement>

<statement>
CREATE TABLE IF NOT EXISTS [deviceRecords] (
  [devid] TEXT UNIQUE NOT NULL COLLATE NOCASE,
  [serial] TEXT UNIQUE COLLATE NOCASE,
  [version] TEXT NOT NULL COLLATE NOCASE,
  [created] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime'))
);       
</statement>

<statement>
CREATE TABLE IF NOT EXISTS [testRecords] (
  [job_id] INT NOT NULL, 
  [dev_id] NUMERIC NOT NULL, 
  [zero0min] NUMERIC NOT NULL,
  [zero0max] NUMERIC NOT NULL,
  [zero0avg] NUMERIC NOT NULL,
  [zero1min] NUMERIC NOT NULL,
  [zero1max] NUMERIC NOT NULL,
  [zero1avg] NUMERIC NOT NULL, 
  [zero2min] NUMERIC NOT NULL,
  [zero2max] NUMERIC NOT NULL,
  [zero2avg] NUMERIC NOT NULL, 
  [weight0min] NUMERIC NOT NULL,
  [weight0max] NUMERIC NOT NULL,
  [weight0avg] NUMERIC NOT NULL,
  [weight1min] NUMERIC NOT NULL,
  [weight1max] NUMERIC NOT NULL,
  [weight1avg] NUMERIC NOT NULL, 
  [weight2min] NUMERIC NOT NULL,
  [weight2max] NUMERIC NOT NULL,
  [weight2avg] NUMERIC NOT NULL, 
  [test_id] INT NOT NULL, 
  [result] TEXT NOT NULL COLLATE NOCASE, 
  [operator] TEXT COLLATE NOCASE, 
  [created] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime'))
  );
</statement>

</sql> 

1 个答案:

答案 0 :(得分:1)

如果将其保存到externalstorage,请在Android / data //下为您提供包名称的路径。但最好的方法是将任何数据库文件保存在私有应用程序存储下。

以上两种情况在卸载应用程序时这些文件夹被删除