0
<application
android:name=".AppClass"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data
android:name="AA_DB_NAME"
android:value="mydb.db" />
<meta-data
android:name="AA_DB_VERSION"
android:value="1" />
<activity
android:name=".ActivityMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
這裏AppClass:
public class AppClass extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(this);
context = getApplicationContext();
}
}
型號:
@Table(name = "Books")
public class Book extends Model {
@Column(name = "Author")
public String author;
@Column(name = "Title")
public String title;
public Book() {
super();
}
}
保存的書和檢索:
Book b = new Book();
b.title = "TheFaultInOurStar";
b.author = "JohnGreen";
b.save();
ArrayList<Book> books = new Select().all().from(Book.class).execute();
Log.e("Log", "SizeOfBooks:" + books.size());
但是,當運行應用程序,我得到這個錯誤:
Error inserting Title=TheFaultInOurStar Author=JohnGreen rolling Id=null
android.database.sqlite.SQLiteException: no such table: Books (code 1): , while compiling: INSERT INTO Books(Title,Author,Id) VALUES (?,?,?)
爲什麼Id字段添加到表模式? 我在清單中增加了AA_DB_VERSION,但仍然存在錯誤。 甚至在其他模擬器和手機上運行的應用程序再次出現該錯誤。
問題在哪裏?
仍然沒有工作,並得到這個錯誤:沒有這樣的表:書籍(代碼1):,而編譯時:SELECT ALL * FROM Books – javaddroid
我認爲你所有的代碼是正確的。你檢查了android:name =「.AppClass」是否正確地引用了你的Application類? – ikhsan