-1

Below the code for generating and establishing to-one relationship in greendao

private static void addPersonInfo(Schema schema) {
        Entity person=schema.addEntity("Person");
        person.addIdProperty().primaryKey().autoincrement();
        person.addStringProperty("personName");

        Entity order=schema.addEntity("Order");
        order.addLongProperty("orderID").primaryKey().autoincrement();
        order.addStringProperty("orderName");
        Property orderProperty=person.addLongProperty("orderID").getProperty();
        person.addToOne(order,orderProperty);   
    }

Above code generates corresponding Person.java,PersonDao.java,Order.java,OrderDao.java, DaoSession.java and DaoMaster.java files Succesfully.

Now in my MainActivity's onCreate() method I am simply trying to insert a record in both the table but i don't know how can i do that. I tried like this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"RelationDemo.db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        Person person=new Person();
        Order order=new Order();
        OrderDao orderDao=daoSession.getOrderDao();
        order.setOrderID(Long.parseLong("111"));
        order.setOrderName("person1 order");
        orderDao.insert(order);
        PersonDao personDao=daoSession.getPersonDao();
        person.setPersonName("person1");
        person.setOrderID(Long.parseLong("111"));
        person.setOrder(order);
        personDao.insert(person);

}

but this gives Exception: Caused by: android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): , while compiling: INSERT INTO ORDER ('ORDER_ID','ORDER_NAME') VALUES (?,?)

Please help me out. thanks

Baqir
  • 717
  • 1
  • 7
  • 20

1 Answers1

0

It seems that you're doing it the right way, at least speaking about the greendao code.

But it appears to be an SQLite issue. I think it might be the table name, since "ORDER" it's a SQLite reserved word used for the "ORDER BY" clausule. You could quote the keyword, as it's told in the link, but I think it should be easy to use a diferent name for the table.

Jofre Mateu
  • 2,390
  • 15
  • 26