从Android中的外部.sql文件导入Sqlite架构和sqlite数据

时间:2017-11-24 05:32:11

标签: android sqlite android-sqlite

要求:我不想使用任何java类创建数据库。我想拥有schema.sql(创建数据库和表)和seeddata.sqlite(将数据插入表)我的android应用程序的文件。当我的应用程序第一次运行它应该运行schema.sqlite然后seeddata.sql。
问题:我没有得到如何做到这一点。

1 个答案:

答案 0 :(得分:2)

如果我理解您的问题,以下内容可能符合您的要求: -

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("mydb"),null);
    // Loop through schema.sql (Noting that execSQL will only execute 1 SQL statment at a time)
    db.execSQL("CREATE TABLE IF NOT EXISTS tablea (_name TEXT, _anotherrow TEXT, _etc TEXT)");
    if (DatabaseUtils.queryNumEntries(db,"tablea") < 1) {
        // Loop through seeddata here (if actual SQL then use execSQL)
        ContentValues cv = new ContentValues();
        cv.put("_name", "This is the name.");
        cv.put("_anotherrow", "Someother data");
        cv.put("_etc", "and on and on.....");
        db.insert("tablea", null, cv);
    }
    Cursor csr = db.query("tablea",null,null,null,null,null,null);
    while (csr.moveToNext()) {
        Log.d("TABLEINFO","Row " + csr.getPosition());
        for(String s: csr.getColumnNames()) {
            Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
        }
    }

这将

  • 创建名为 mydb 的数据库(如果该数据库不存在),然后打开数据库。
  • 尝试创建名为 tablea 的表(如果它不存在。
  • 检查表格中有多少行,然后是否小于1
    • 插入一行
  • 将数据提取到光标名称 csr
  • 对于表格中的每一行,它都会
    • 在日志中写一行,详细说明行号(第一个为0),然后是每列
    • 在日志中写一行,详细说明列名称和该行的该列数据。

e.g。

11-24 17:06:04.601 3360-3360/? D/TABLEINFO: Row 0
11-24 17:06:04.601 3360-3360/? D/TABLEINFO:     Column=_name Data=This is the name.
11-24 17:06:04.601 3360-3360/? D/TABLEINFO:     Column=_anotherrow Data=Someother data
11-24 17:06:04.601 3360-3360/? D/TABLEINFO:     Column=_etc Data=and on and on.....

完整的工作解决方案

  • 1文件schema.sql,已放入assets文件夹(在本例中为名为ImportSchemaAndData D:\Android_Applications\ImportSchemaAndData\app\src\main\assets\schema.sql的项目)

: -

CREATE TABLE tablea (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
CREATE TABLE tableb (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
CREATE TABLE tablec (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
  • 2文件seeddata.sql(同样放入资产文件夹)

: -

INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")

注意您可能需要创建资产文件夹(参见上面的位置)

  • 3守则(在本案例中为MainActivity)

: -

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InputStream schema, seeddata;
        String lineofdata;
        SQLiteDatabase db;
        Cursor csr;

        try {
            schema = this.getAssets().open("schema.sql");
            seeddata = this.getAssets().open("seeddata.sql");
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return; //???? used return just for brevity
        }
        File dbpath = new File(this.getDatabasePath("mydb").getParent());
        if (!dbpath.exists()) {
            dbpath.mkdirs();

            db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("mydb"), null);
            BufferedReader br = new BufferedReader(new InputStreamReader(schema));
            try {
                while ((lineofdata = br.readLine()) != null) {
                    Log.d("ACTIONSQL", "Actioning " + lineofdata);
                    db.execSQL(lineofdata);
                }
                schema.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
                try {
                    schema.close();
                } catch (IOException ioe2) {
                    ioe2.printStackTrace();
                }
            }
            db.close();
            db = SQLiteDatabase.openDatabase(this.getDatabasePath("mydb").getPath(),null, Context.MODE_PRIVATE);
            lineofdata = "";
            br = new BufferedReader(new InputStreamReader(seeddata));
            try {
                while ((lineofdata = br.readLine()) != null) {
                    Log.d("ACTIONSQL", "Actioning " + lineofdata);
                    db.execSQL(lineofdata);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
                db.endTransaction();
            }
        }  else {
            db = SQLiteDatabase.openDatabase(this.getDatabasePath("mydb").getPath(),null,Context.MODE_PRIVATE);
        }

        csr = db.query("tablea",null,null,null,null,null,null);
        while (csr.moveToNext()) {
            Log.d("TABLEINFO","Row " + csr.getPosition());
            for(String s: csr.getColumnNames()) {
                Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
            }
        }
        csr = db.query("tableb",null,null,null,null,null,null);
        while (csr.moveToNext()) {
            Log.d("TABLEINFO","Row " + csr.getPosition());
            for(String s: csr.getColumnNames()) {
                Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
            }
        }
        csr = db.query("tablec",null,null,null,null,null,null);
        while (csr.moveToNext()) {
            Log.d("TABLEINFO","Row " + csr.getPosition());
            for(String s: csr.getColumnNames()) {
                Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
            }
        }
    }
}

初次运行的结果: -

11-24 19:20:02.182 4652-4652/? D/ACTIONSQL: Actioning CREATE TABLE tablea (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
11-24 19:20:02.187 4652-4652/? D/ACTIONSQL: Actioning CREATE TABLE tableb (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
11-24 19:20:02.191 4652-4652/? D/ACTIONSQL: Actioning CREATE TABLE tablec (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
11-24 19:20:02.197 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
11-24 19:20:02.201 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
11-24 19:20:02.204 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
11-24 19:20:02.206 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
11-24 19:20:02.209 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
11-24 19:20:02.213 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
11-24 19:20:02.216 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
11-24 19:20:02.219 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
11-24 19:20:02.222 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
11-24 19:20:02.224 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
11-24 19:20:02.227 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
11-24 19:20:02.231 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 0
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Fred.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Bert.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Tom.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=4
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Harry
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Harry has this data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=Harry has even more data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 0
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Fred.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Bert.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Tom.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_id Data=4
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_name Data=Harry
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Harry has this data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO:     Column=_etc Data=Harry has even more data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 0
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_id Data=1
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_name Data=Fred
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Fred
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Fred.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Row 1
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_id Data=2
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_name Data=Bert
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Bert
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Bert.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Row 2
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_id Data=3
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_name Data=Tom
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Data for Tom
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_etc Data=even more data for Tom.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Row 3
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_id Data=4
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_name Data=Harry
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_otherdata Data=Harry has this data.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO:     Column=_etc Data=Harry has even more data.

后续运行的结果: -

11-24 19:20:20.605 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 0
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Fred.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Bert.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Tom.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=4
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Harry
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Harry has this data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=Harry has even more data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 0
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Fred.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Bert.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Tom.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=4
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Harry
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Harry has this data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=Harry has even more data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 0
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Fred.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 1
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=2
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Bert
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Bert
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Bert.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 2
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=3
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Tom
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Data for Tom
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=even more data for Tom.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 3
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_id Data=4
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_name Data=Harry
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_otherdata Data=Harry has this data.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO:   Column=_etc Data=Harry has even more data.