2015-04-28 77 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不能即时运行。