2

I just tried to use ActiveAndroid. Following their tutorial from here, I set everything. I added the manifest entry:

application android:name="com.activeandroid.app.Application" ...

meta-data android:name="AA_DB_NAME" android:value="MyDb.db"
meta-data android:name="AA_DB_VERSION" android:value="1"

and created my model class:

package com.mycomp.Model;

import org.joda.time.LocalDate;
import org.odata4j.core.Guid;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

@Table(name = "Table1")
public class Table1 extends Model
{
  @Column(name = "Custom_ID")
  public Guid Custom_ID;
  @Column(name = "Name")
  public String Name;
  @Column(name = "Usr_type")
  public int Usr_type;
  @Column(name = "BDate")
  public LocalDate BDate;

  public MyEnum UserGroup;

  public Table1()
  {
    super();
  }

  public Table1(String name, LocalDate date, Guid custom_id,
      MyEnum userGroup)
  {
    super();
    Name = name;
    BDate = date;
    Custom_ID = custom_id;
    UserGroup = userGroup;
  }

}

After that, I used the save method (it allegedly creates the underlying database structure):

Table1 t1=new Table1();
t1.Name="Test";
t1.UserGroup = MyEnum.G1;
t1.save();

It threw an exception: table Table1 has no column named Name

I checked it with SQLite Browser. The table exists but contains only one field: Id. What else should I do to create the other fields?

Nestor
  • 8,194
  • 7
  • 77
  • 156
  • I'm confused: I added a Test class with a simple String property. I created an instance, and called _save_. It threw an exception: **no such table: Test** I believed, the model makes the underlying system to create it. Can you tell me, how ActiveAndroid works? Isn't a table creation funcion missing? – Nestor Feb 23 '13 at 20:03

3 Answers3

10

Well, I believe, it should be important to share in their tutorial that ActiveAndroid creates the database structure on the first run. If you change your model, it doesn't sync it. The solution: delete the db file from your phone/emulator and on the next run, it works.

Nestor
  • 8,194
  • 7
  • 77
  • 156
8

There is another solution: change the AA_DB_VERSION metadata value and place the modification scripts to the assets/migrations folder so ActiveAndroid will use the upgrade function. It comes handy when there are already data in the database.

Source: https://github.com/pardom/ActiveAndroid/wiki/Schema-migrations

Nestor
  • 8,194
  • 7
  • 77
  • 156
  • Thanks for the answer. But when I am trying this I get the below exception. Any idea why? `android.database.sqlite.SQLiteException: not an error (code 0)` – Hannan Shaik Apr 28 '14 at 07:31
  • 1
    No idea, but if you have the source code, you can debug where the exception was thrown. I also suggest to write in the issue tracker of the project: https://github.com/pardom/ActiveAndroid/issues – Nestor Apr 28 '14 at 07:35
  • The error was thrown from My Application Class where I was initializing my ActiveAndroid. Thanks for the help I shall open up an issue at github. – Hannan Shaik Apr 28 '14 at 07:52
  • I have three versions in play store. 1st version dont have a column in a Table. 2nd version has the column, whiles users tries to update to the third version the migration scripts reads to add a duplicate column for version 2 user thus crashes, how can i solve it – George Thomas Jul 17 '15 at 05:44
  • The migration scripts should only contain changes between versions. So if script #2 adds the column, script #3 must not add it. – Nestor Jul 17 '15 at 06:17
1

It could be that you made a change to the class after creating the database hence activeandroid doesn't recognize the table column. You could either uninstall the apk or you could clear the cached data for the app.