gpt4 book ai didi

java - 在现有数据库中创建表 sqlite sugar orm

转载 作者:行者123 更新时间:2023-11-29 07:41:18 25 4
gpt4 key购买 nike

我现有的数据库是使用 SQLiteOpenHelper 创建的,但现在我想转移到 Sugar Orm 并尝试在现有数据库中添加新表。我遵循 sugar orm 页面 ( Sugar Orm Configuration ) 配置页面上提到的所有要点。

这是我的配置,

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)

任何建议对我来说似乎我做的一切都是正确的。谢谢

最佳答案

我也遇到了同样的情况,解决方案只是升级 list 中的数据库版本:

来自:

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

收件人:

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

问候。

关于java - 在现有数据库中创建表 sqlite sugar orm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com