ActiveAndroid icon indicating copy to clipboard operation
ActiveAndroid copied to clipboard

Accessing prepopulated database

Open jkicha opened this issue 12 years ago • 7 comments

How to access the pre populated db from the assets folder?

jkicha avatar Apr 18 '14 16:04 jkicha

You search this link? https://github.com/pardom/ActiveAndroid/wiki/Pre-populated-databases

Other useful link: http://guides.thecodepath.com/android/ActiveAndroid-Guide Hope it helps

sabadow avatar Apr 21 '14 10:04 sabadow

Is it possible to use a database with "id" as primary key instead of "Id" Is there probably a way to define the Id column via Annotation ?

0xCCD avatar May 17 '14 19:05 0xCCD

Indeed, there is:

@Table(name = "attachments", id = "_id")
public class Attachment extends BaseModel {

Replace _id with whatever you would like to name the primary key column.

joshuapinter avatar May 17 '14 20:05 joshuapinter

Many thanks for the fast answer, i've found it already 1 hour ago after checking your code :)

0xCCD avatar May 17 '14 21:05 0xCCD

Awesome. Can we can close this Issue, than?

joshuapinter avatar May 17 '14 21:05 joshuapinter

While this is still open, I was wondering, how can I validate that my pre-populated Database is setup correctly? I am creating it an putting it into the assets directory of my project but when I push to the phone, there is no content. I'm using

CREATE TABLE Lists (_id INTEGER PRIMARY KEY, name VARCHAR);
CREATE TABLE Words (_id INTEGER PRIMARY KEY, encoding VARCHAR, meaning VARCHAR);
CREATE TABLE ListWords (_id INTEGER PRIMARY KEY, list INTEGER, word INTEGER, FOREIGN KEY(list) REFERENCES list(_id), FOREIGN KEY(word) REFERENCES words(_id));

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO "android_metadata" VALUES ('en_US');

INSERT INTO Lists (_id, name) VALUES (1, "Review List"), (2, "Test");
INSERT INTO Words (encoding, meaning) VALUES ("1A06180616", "Test Word 1"), ("1C080D04150C", "Test Word 2");
INSERT INTO ListWords (list, word) VALUES (2, 1), (2, 2);

To set up the Database on my computer and populate some test values. The models I'm trying to copy are:

@Table(name="ListWords", id= BaseColumns._ID)
public class ListWords extends Model {
    @Column(name="list", onDelete = Column.ForeignKeyAction.NO_ACTION)
    protected List list;
    @Column(name="word", onDelete = Column.ForeignKeyAction.NO_ACTION)
    protected Word word;
}


@Table(name="Words", id= BaseColumns._ID)
public class Word extends Model {
    @Column(name = "encoding")
    protected String encoding = null;
    @Column(name="meaning")
    protected String meaning;
}


@Table(name="Lists", id= BaseColumns._ID)
public class List extends Model {
    @Column(name="name")
    protected String name;
}

Is there something I'm missing? or something which is automatically set up?

Thanks for any help!

EDIT: End the Code formatting before final comments.

laochiv avatar Jun 01 '14 05:06 laochiv

Actually, I'm sorry guys, It's because I'm a moron. I had the db file named something different than the database on the device. I'll leave both of these up here just in case it helps someone else.

laochiv avatar Jun 01 '14 05:06 laochiv