簡體   English   中英

在現有數據庫sqlite sugar orm中創建表

[英]creating tables in existing database sqlite sugar orm

我有使用SQLiteOpenHelper創建的現有數據庫,但是現在我想移至Sugar Orm並嘗試在現有數據庫中添加新表。 我遵循糖orm頁面( 糖Orm配置 )的配置頁面上提到的所有要點。

這是我的配置,

AndroidManifest.xml

<application
    android:name=".application"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="DATABASE" android:value="database.db" />
    <meta-data android:name="VERSION" android:value="5" />
    <meta-data android:name="QUERY_LOG" android:value="true" />
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example.in.smart" />

application.java(應用程序類)

public class application extends com.orm.SugarApp
 {
private static Context instance;
 public static Context get() {
    return application.instance;
}

模型類

public class Now extends SugarRecord<Now> {
public String type = null;
public String name = null;
public String address = null;
public Date created;

public Now(){}

public Now(String type, String name,String address,Date created){
    super();
    this.type = type;
    this.name = name;
    this.address = address;
    this.created = created;
}

}

@Override public void onCreate()
{
    super.onCreate();
    instance = getApplicationContext();
    //ACRA.init(this);
}

}

添加數據過程

   Now now = new Now(entry.getKey(), entry.getValue().get(i).name, entry.getValue().get(i).address, calendar.getTime());
  now.save(); // throws exception here

但我得到這個例外

E/SQLiteLog﹕ (1) no such table: NOW
 E/SQLiteDatabase﹕ Error inserting ADDRESS=abc TYPE=Now NAME=tyy CREATED=1430229379156
android.database.sqlite.SQLiteException: no such table: NOW (code 1): , while compiling: INSERT INTO NOW(ADDRESS,TYPE,NAME,CREATED) VALUES (?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at com.orm.SugarRecord.save(SugarRecord.java:126)
        at com.orm.SugarRecord.save(SugarRecord.java:45)

任何建議對我來說都好像我做對了所有事情。 謝謝

同樣的事情發生在我身上,解決方案僅是清單中的數據庫版本:

從:

<meta-data
            android:name="VERSION"
            android:value="2" />

至:

<meta-data
            android:name="VERSION"
            android:value="3" />

問候。

INSERT INTO語句無效:

INSERT INTO NOW(ADDRESS,TYPE,NAME,CREATED) VALUES (?,?,?,?,?)

您嘗試插入5個值而不是4。(請注意5個問號)。

我猜SugarRecord包含一個必填的ID。

 Now now = new Now(entry.getKey(), entry.getValue().get(i).name, entry.getValue().get(i).address, calendar.getTime()); 

有同樣的問題。 檢查您的條目之一是否為“ null”。

Sugar ORM不支持空值。 請也增加數據庫的版本。

嘗試禁用即時運行,sugar orm不適用於即時運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM