Sugar ORM和Android

时间:2017-05-04 14:56:26

标签: android sqlite orm sugarorm

在遵循文档后,我已成功添加了jar并实现了示例代码:

public class Book extends SugarRecord{
   String title;
   String edition;

   public Book(){

   }
   public Book(String title, String edition){
       this.title = title;
       this.edition = edition;
   }
}

我的主要活动是:

public class MainActivity extends Activity implements View.OnClickListener {
  Button btnAdd;
  EditText txtedition, txttitle;



  @Override
  protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      btnAdd = (Button) findViewById(R.id.btnadd);
      btnAdd.setOnClickListener(this);



      txtedition = (EditText) findViewById(R.id.txtedition);
      txttitle = (EditText) findViewById(R.id.txttitle);

  }

  @Override
  public void onClick(View v) {

      String edition = txtedition.getText().toString();
      String title = txttitle.getText().toString();
      switch (v.getId()) {
          case R.id.btnadd:
              Book book = new Book(title, edition);
              book.save();
              break;

      }
  }
}

我的应用程序已成功构建,但是当我按下添加按钮时,我得到以下异常:

  

致命的例外:主要                                                                        处理:com.example.agds.demo,PID:7939                                                                        android.database.sqlite.SQLiteException:没有这样的表:BOOK(代码1):,同时编译:INSERT OR REPLACE INTO BOOK(TITLE,ID,EDITION)VALUES(?,?,?)                                                                        ################################################## ###############                                                                        错误代码:1(SQLITE_ERROR)                                                                        引起:SQL(查询)错误或缺少数据库。                                                                           (没有这样的表:BOOK(代码1):,编译时:INSERT OR REPLACE INTO BOOK(TITLE,ID,EDITION)VALUES(?,?,?))                                                                        ################################################## ###############                                                                            在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)                                                                            在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)                                                                            在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)                                                                            在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                            在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:59)                                                                            在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)                                                                            在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)                                                                            在com.orm.SugarRecord.save(SugarRecord.java:266)                                                                            在com.orm.SugarRecord.save(SugarRecord.java:360)                                                                            在com.example.agds.demo.MainActivity.onClick(MainActivity.java:49)                                                                            在android.view.View.performClick(View.java:5702)                                                                            在android.widget.TextView.performClick(TextView.java:10888)                                                                            在android.view.View $ PerformClick.run(View.java:22541)                                                                            在android.os.Handler.handleCallback(Handler.java:739)                                                                            在android.os.Handler.dispatchMessage(Handler.java:95)                                                                            在android.os.Looper.loop(Looper.java:158)                                                                            在android.app.ActivityThread.main(ActivityThread.java:7229)                                                                            at java.lang.reflect.Method.invoke(Native Method)                                                                            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您是否已将< AndroidManifest中的元数据添加到<应用/>

<meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example.yourmodels" />

另外,请将其放在<aplication>

android:name="com.orm.SugarApp"

这是库中的Application类。

https://github.com/chennaione/sugar/blob/master/library/src/main/java/com/orm/SugarApp.java

或者,如果您已经拥有自己的Application类,则可以扩展它。

public class YourApp extends SugarApp {
}

答案 1 :(得分:1)

请注意,如果您在Android Studio中启用了“即时运行”,那么Sugar将无法工作(至少它不会创建新表格)。