2015-04-28 33 views
1

我有我使用SQLiteOpenHelper創建的現有數據庫,但現在我想移動到Sugar Orm並嘗試在現有數據庫中添加新表。我遵循Sugar orm頁面(Sugar Orm Configuration)的配置頁面上提到的所有要點。在現有的數據庫sqlite糖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; 
} 

Model類

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) 

我任何建議因爲它看起來像我做的一切權利。由於

+0

你能告訴我們堆棧跟蹤嗎? –

+0

已更新堆棧跟蹤 – user3290805

+0

您是否也瀏覽了[數據庫遷移說明](http://satyan.github.io/sugar/migration.html)?你必須提高版本,並提供一個升級腳本來創建新的「Now」表格 - 這不會自動發生。 –

回答

0

INSERT INTO聲明是無效的:

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

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

我猜SugarRecord conains一個id是強制性的。

+0

那麼它應該是在構造函數中的問題,而不是沒有發現表,其實我檢查它是相等的數量?在插入到,我已經更新了問題 – user3290805

+0

由於某種原因表不存在。它不能從你的代碼樣本中推斷出原因。 –

+0

是的,那是什麼問題,但這正是我在我的代碼..任何提示? – user3290805

5

同樣發生在我身上,並將溶液最多隻能在清單數據庫版本:

來源:

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

要:

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

問候。

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

得到了同樣的問題。檢查一個條目是否爲「空」。

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

0

嘗試禁用即時運行,糖orm不能即時運行。