У меня есть два аннотированных класса модели ActiveAndroid, Term и Course (отношение 1 ко многим).
Я могу создавать объекты Term и Course и вызывать .save () для них без каких-либо ошибок.
Однако, когда я пытаюсь запросить объекты вне доступа к их членам напрямую, я получаю сообщение об ошибке:
no such table: Terms (code 1 SQLITE_ERROR):, while compiling: SELECT * FROM Terms ORDER BY RANDOM()
или подобное, если я пытаюсь запросить курсы.
Насколько я знаю, я не менял свою схему.
То, что я пробовал: 1. Удаление и переустановка приложения в эмуляторе.2. Изменение AA_DB_VERSION в манифесте Android.
Манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gilbertdev.wgu.c196.abm1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="AA_MODELS"
android:value="com.gilbertdev.wgu.c196.abm1.Term, com.gilbertdev.wgu.c196.abm1.Course" />
<meta-data android:name="AA_DB_VERSION" android:value="10" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Зависимости:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('libs/activeandroid-3.0.jar')
}
Term.java
package com.gilbertdev.wgu.c196.abm1;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import java.util.Date;
@Table(name = "Terms")
public class Term extends Model {
@Column(name = "title")
public String title;
@Column(name = "startdate")
public Date startdate;
@Column(name = "enddate")
public Date enddate;
//@Column(name = "course")
//public Course course;
public Term () {
super();
}
}
Курс.Java
package com.gilbertdev.wgu.c196.abm1;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import java.util.Date;
@Table(name = "Courses")
public class Course extends Model {
@Column(name = "title")
public String title;
@Column(name = "startdate")
public Date startdate;
@Column(name = "enddate")
public Date enddate;
@Column(name = "status")
public String status;
@Column(name = "term")
public Term term;
public Course() {
super();
}
}
MainActivity.java
package com.gilbertdev.wgu.c196.abm1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.activeandroid.ActiveAndroid;
import com.activeandroid.Model;
import com.activeandroid.query.Select;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView testView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActiveAndroid.initialize(this);
setContentView(R.layout.activity_main);
testView = (TextView) findViewById(R.id.testtext);
testView.append("\nGilbert\n");
Term firstterm = new Term();
firstterm.title = "First term";
firstterm.startdate = new Date(1991, 2, 20);
firstterm.enddate = new Date(2100, 2, 20);
firstterm.save();
Course firstcourse = new Course();
firstcourse.title = "First course";
firstcourse.startdate = new Date(2018, 1, 13);
firstcourse.enddate = new Date(2018, 1, 20);
firstcourse.status = "In Progress";
firstcourse.term = firstterm;
firstcourse.save();
testView.append(firstterm.title + "\n");
testView.append(firstterm.startdate + "\n");
testView.append(firstcourse.term.title + "\n");
try {
List<Course> courses = new Select()
.from(Course.class)
.where("Term = ?", firstterm.getId())
.orderBy("title ASC")
.execute();
for (Course course : courses) {
testView.append(course.status);
}
// Term test = new Select().from(Term.class).orderBy("RANDOM()").executeSingle();
// testView.append(test.title);
} catch (Exception e) {
testView.append(e.getMessage());
}
}
}