Sugar orm 创建 table 问题

Sugar orm create table issue

首先,我到处寻找解决方案,但我还没有找到 table 创建时常见错误的解决方案:

Caused by: android.database.sqlite.SQLiteException: no such table: EVENT (code 1): , while compiling: INSERT OR REPLACE INTO EVENT

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company.something.something2">
        ...
        <meta-data android:name="DATABASE" android:value="sugarexample.db" />
        <meta-data android:name="VERSION" android:value="4" />
        <meta-data android:name="QUERY_LOG" android:value="true" />
        <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.company.something.something2.Entities" />

我的实体:

package com.company.something.something2.Entities;

import com.orm.SugarRecord;

public class Event extends SugarRecord {
    private String description;
    private String title;
    private String photo;
    public Event() {

    }

    //setters and getters
}

我的Activity(用于调试):

...
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SugarContext.init(getApplicationContext());
        com.company.something.something2.Entities.Event book =
                new com.company.something.something2.Entities.Event();
        book.setId((long) 1);
        book.setTitle("test");
        book.setDescription("de");
        book.setPhoto("someurl");
        book.save();

我尝试过的:

P.S: 我正在使用 SugarORM 1.5, Gradle 2.1.3

有没有人有什么想法?

您的 table 似乎未创建。 SugarOrm 需要一个 android Application class,它可以从中开始创建或管理其 table 模式和其他必要的东西。

根据您的代码,您在 activity 中定义了 SugarContext.init(getApplicationContext());。如果您没有将 class 扩展到 SugarApp class,这应该是您的 Application class 的一部分.

因此将此行移动到应用程序 class 的 onCreate
还要在 onTerminate()

中添加以下行
 SugarContext.terminate();  

因此您的应用程序 class 将如下所示:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        SugarContext.init(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        SugarContext.terminate();
    }
}

并在 Androidmanifest.xml 文件中声明此 class

<application
  android:name=".MyApplication"
  .. rest of the code