简体   繁体   中英

Load a database to Ormlite with sql queries

I already have a bunch of sql create table queries and injections written. How can I load these in to my android app using Ormlite? From what I understand the data base is created like this in the examples:

        @DatabaseField(generatedId = true)
        private Integer id;

        @DatabaseField
        private String description;
...
    TableUtils.createTable(connectionSource, Thing.class);

I would want to do this automagically the other way around with ex:

CREATE TABLE "History" ("_id" INTEGER PRIMARY KEY  NOT NULL , "date" DATETIME, "program_id" INTEGER, "FOREIGN KEY(program_id) REFERENCES Program (_id)" );

Best thing would be to read from a textfile added i resources.

As you can see Im a bit confused about this, can something like this be done? Should also mention that Im using foreign keys, is it supported?

Sounds to me that you need to match your objects to your create statements. For example, if you want to do the following create statement:

CREATE TABLE "History" ("_id" INTEGER PRIMARY KEY  NOT NULL ,
    "date" DATETIME, "program_id" INTEGER,
    "FOREIGN KEY(program_id) REFERENCES Program (_id)" );

Then you should should construct the following Java to correspond to it:

 public class History {
      // maybe generatedId = true
      @DatabaseField(columnName = "_id", nullable = false, id = true)
      public int id;
      @DatabaseField(/* not sure if you need to set a date type here */
      public Date date;
      @DatabaseField(foreign = true, columnName = "program_id")
      public Program program;
 }

 public class Program {
      // maybe generatedId = true
      @DatabaseField(columnName = "_id", nullable = false, id = true)
      public int id;
      ...
 }

You can do your own create and then tweak the corresponding Java classe so they are able to persist to the tables -- matching the columns with the fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM