Android: Sugar ORM No Such Table Exception

六月ゝ 毕业季﹏ 提交于 2019-11-26 22:55:49

问题


I am getting the No Such table exception when i am Using Sugar ORM with GPU image Android Library. I am using Gradle and Android Studio. Once i remove GPU image this issue is solved. So i don't know whats causing this exception. Details about this exception are also being discussed in this git issue and it seems a lot of people are still facing it.

My crash log is posted below

> 10-09 11:30:21.511 4326-4831/com.example.app E/SQLiteLog: (10) Failed
> to do file read, got: 0, amt: 100, last Errno: 2 10-09 11:30:26.506
> 4326-4831/com.example.app E/SQLiteLog: (1) no such table: IMAGE 10-09
> 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime: FATAL
> EXCEPTION: AsyncTask #1 10-09 11:30:26.516 4326-4831/com.example.app
> E/AndroidRuntime: java.lang.RuntimeException: An error occured while
> executing doInBackground() 10-09 11:30:26.516
> 4326-4831/com.example.app E/AndroidRuntime:     at
> android.os.AsyncTask$3.done(AsyncTask.java:299) 10-09 11:30:26.516
> 4326-4831/com.example.app E/AndroidRuntime:     at
> java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> java.util.concurrent.FutureTask.setException(FutureTask.java:219)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> java.util.concurrent.FutureTask.run(FutureTask.java:239) 10-09
> 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 10-09
> 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> java.lang.Thread.run(Thread.java:838) 10-09 11:30:26.516
> 4326-4831/com.example.app E/AndroidRuntime:  Caused by:
> android.database.sqlite.SQLiteException: no such table: IMAGE (code
> 1): , while compiling: SELECT * FROM IMAGE 10-09 11:30:26.516
> 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
> Method) 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:
> at
> android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 10-09
> 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
> 10-09 11:30:26.516 4326-4831/com.example.app E/AndroidRuntime:     at
> android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)

回答1:


I had the same problem, it's related to Instant Run. Disable instant run, reinstall the application, and it works.

See this link for details on how to enable or disable Instant Run.




回答2:


If you are using Android studio 2.0

Having same issue!

So instant run is not compatible with Sugar ORM

  • open setting
  • now run your project

Enjoy!




回答3:


I was facing the same issue. Disabling "Instant run" didn't solve the problem. I didn't enable proguard too. Later I removed all meta data of sugarORM from android manifest (except query log) and that solved the problem. So remove the following 3 lines from android manifest:

    <meta-data android:name="DATABASE" android:value="edikodik.db" />
    <meta-data android:name="VERSION" android:value="2" />
    <meta-data android:name="QUERY_LOG" android:value="true" /> 

EDIT: As per comment of jrhamza, if above solution doesn't work, try disabling Instant Run.




回答4:


I am using version 1.5 and have tried many ways here but not work.

And I found the solution!

This will force to create tables when starting the app:

public class SugarOrmTestApplication extends SugarApp {
    @Override
    public void onCreate() {
        super.onCreate();
        SugarContext.init(getApplicationContext());

        // create table if not exists
        SchemaGenerator schemaGenerator = new SchemaGenerator(this);
        schemaGenerator.createDatabase(new SugarDb(this).getDB());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        SugarContext.terminate();
    }
}



回答5:


If you add new model(new table) in your code, you need change version of database in Manifest file. New value > Old value (update)

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



回答6:


I also faced this Problem. After hunted 2 days, I solved it by following tricks In android studio

Step 1. Write compile 'com.github.satyan:sugar:1.5' in dependencies in build.gradle file of app

Step 2. Write classpath 'com.android.tools.build:gradle:2.1.0' in dependencies of build.gradle file of project

Step 3. DB version should greater than 1 always as documented in sugarORM




回答7:


Disable your instant Run go to File > settings > Build, Execution, Deployment > Instant Run




回答8:


Resolved

I don't know but the SugarDb.onCreate is not called. So put this code in MainActivity.onCreate and the Sugar ORM will work

    SugarDb db = new SugarDb(this);
    db.onCreate(db.getDB());



回答9:


To create model tables, I use to execute this for all my models once my Application starts:

Book.findById(Book.class, (long) 1);
Car.findById(Car.class, (long) 1);

My Application extends SugarApp, so this forces Sugar to create all tables.

On the other hand, to update your database, you have to change your version to 2.

And finally, having an empty constructor is a must.

Hope this helps! ;)




回答10:


In my case, I removed the following line from AndroidManifest.xml and it works!




回答11:


I was also having this issue, my class only had 1 String property. I was using version 1.3. Just comment this code from AndroidManifest.xml file and you're good to go :)

<meta-data
 android:name="DOMAIN_PACKAGE_NAME"
 android:value="com.project.database.entities" />

Reference:

https://github.com/satyan/sugar/issues/75




回答12:


I had this problem. I executed all suggestions posted here. But neither worked. Then I applied all together, and finally got rid of that problem. What I did-

1. Disabled instant run. (as Parth Vora said.)
2. Then I added the following class-

   import android.app.Application;
   import android.content.res.Configuration;
   import com.orm.SugarApp;
   import com.orm.SugarContext;

   public class SugarOrmTestApplication extends SugarApp {

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
      }

      @Override
     public void onCreate() {
        super.onCreate();
        SugarContext.init(getApplicationContext());
        Book.findById(Book.class, (long) 1);
     }

     @Override
     public void onLowMemory() {
       super.onLowMemory();
     }

     @Override
     public void onTerminate() {
       super.onTerminate();
     }
 }

 3. Modified manifest file-
    <application
      //Added the following lines-
       android:name="SugarOrmTestApplication">
       <meta-data android:name="DATABASE" android:value="sugar_example_new.db" />
       <meta-data android:name="VERSION" android:value="2" />
       <meta-data android:name="QUERY_LOG" android:value="true" />
    </application>

And this is my model class-

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

   public Book(){
   }

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

Now my application works good.




回答13:


Did you try to change version to 2??? This could be different version, try other values as well.

Also, make sure you do have empty constructor on your SugarRecord classes.

Change de database name if you are using then is another approuche.

If you are creating the table without unistalling the app, please make the version number +1.




回答14:


I had the same problem and solved it not by disabling instant run but by changing my:

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

to

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

Since the lowest value should be 2




回答15:


As mentioned by others, Android Studio instant run might be the problem (known SugarORM issue), try to turn it off.

However, I've had the same problem, and that wasn't sufficient for me, the problem persisted. I fixed this by changing allowBackup="true" in my manifest to false, running the app, and remove > re-install the app afterwards.

Why I guess that's the problem: I found out my preferences were kept saved even after re-installation. If this also happens to the corrupt SQL table (due to instant run), then even re-installation wouldn't fix it. My problem was immediately fixed after changing allowBackup to false and re-installing. I'm not yet able to reproduce the error, so cannot reproduce the fix either.

Hope this addition might help anyone from headaches like me!




回答16:


When you're starting initial development and don't want to have to increase your Database version for changes made during quick iterations of your models (something I do a lot while I'm fleshing out unforeseen requirements), use this during your applications start-up:

SugarDb sugarDb = new SugarDb(getApplicationContext());
new File(sugarDb.getDB().getPath()).delete();
Model.findById(Model.class, (long) 1); // Perform this for each SugarRecord  model

// Insert test data:
Modelrecord = new Model("Model Title", "Model content", false);
record.save();

Be warned - this will remove all data from your database so persisted changes between debug sessions won't work, however it does allow you to release version 1 of your app version 1 of your database.

I do not recommend doing this after version 1 has been officially released, make sure you log ALL changes needed using the SugarORM Migration Method.




回答17:


Make sure the meta data package name points to where you've put your model entities not app package name




回答18:


For me the Issue was, In my app I had two different Database. so, I only changed the from version 1 to version 2. My first DB had version 1




回答19:


As stated in the README from the github repo, if you are using proguard, you should ensure that entities remain un-obfuscated so table and columns are named correctly.

To do so, add this line to proguard-rules.pro:

-keep class com.yourpackage.yourapp.domainclasspackage.** { *; }



回答20:


In my case it's problem of library Sugar. It has bad method to getting domain Classes (com.orm.util.ReflectionUtil#getDomainClasses). The method always returns no classes. So Sugar may not initialize database tables. I has downgraded to gradle 1.3.0 (that method works correctly) and problem is gone.




回答21:


Please check this property

    <`meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="trending.hashtags.pojo" />`

This should be exactly where your POJO class is not the package name only.



来源:https://stackoverflow.com/questions/33031570/android-sugar-orm-no-such-table-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!