38

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)
Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
  • I am also getting no such table exception after adding two more parameters in the table. –  Jun 21 '18 at 14:21

21 Answers21

95

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.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
Michele
  • 2,336
  • 1
  • 16
  • 10
  • 1
    This problem was occuring before the instant run option was available so thankyou for your reply but it seemed to be another issue. – Sheraz Ahmad Khilji Apr 07 '16 at 14:39
  • FYI, Instant Run enabled can be an issue with Sugar ORM. I had the same issue so I made sure to follow @NewUser answer but the issue persisted. Once I disabled Instant Run, my problems went away. So, if you're still having issues, make sure if you're running Android Studio 2.0 with Instant Run to disable it. – John Murphy Apr 10 '16 at 17:02
  • 7
    I have to disable and reinstall the app. – Beto Caldas Apr 11 '16 at 13:54
  • 1
    Exactly, thanks, sugar doesn't work with android studio 2.0 if there is instant run enabled. – Emanuele Pavanello Apr 12 '16 at 22:23
  • I wasn't able to disable Instant Run unless I first change the gradle build to `classpath 'com.android.tools.build:gradle:2.1.0'`. – Liron May 05 '16 at 19:04
  • 3
    Disable Instant run AND reinstall the app - disabling the feature alone is not enough. – javaxian Jul 22 '16 at 10:25
  • I must add that you may re-enable Instant Run after the first install of the app with it disabled. – galaxigirl Oct 25 '17 at 14:46
  • Great point @galaxigirl ! If you want/need instant run, after the initial launch you should be able to re-enable. Just remember that if you re-install (remove, install) the app, the tables may not exist and therefore you have to disable and re-install again. – BK- Apr 12 '18 at 18:38
67

If you are using Android studio 2.0

Having same issue!

So instant run is not compatible with Sugar ORM

  • open setting
  • enter image description here
  • now run your project

Enjoy!

Parth Vora
  • 1,718
  • 13
  • 9
31

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.

farhad rubel
  • 2,334
  • 2
  • 22
  • 29
  • May be your problem is different – farhad rubel Feb 09 '17 at 08:40
  • 1
    @ farhad rubel Solved the issue by disabling instant run option in android studio – jrh Feb 09 '17 at 08:52
  • 1
    I'm not 100% sure, what's the reason. But I guess, for the first time when you run sugarORM, it tries to upgrade to your desired version from previous version. But as there is no previous version, it crashes. – farhad rubel Feb 20 '17 at 04:36
  • This solved my issue temporarily, however, when I re-added those 3 lines again (because I need them), the problem returned, even when instant run was turned off. I found our allowBackup="true" in android manifest should be set to false. Read my answer on this page for more info. – P Kuijpers Jun 14 '17 at 21:40
  • I had the same issue. Both of these solutions dosn't work. I just reduce the number of caracteres of my db name : "mensurations.db" to "app.db". Maybe there is a limitation ... – Elloco Aug 21 '17 at 06:14
19

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();
    }
}
YK Leung
  • 191
  • 2
  • 5
15

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"/>
hoangdv
  • 15,138
  • 4
  • 27
  • 48
10

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

Sachin Tyagi
  • 1,257
  • 15
  • 21
  • 1
    This tip didn't resolve he issue. I also didn't notice anywhere that VERSION has to greater than 1. – javaxian Jul 22 '16 at 10:19
8

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

7

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());
MarceloSouza
  • 429
  • 5
  • 10
  • Yeah, it worked. Strange, I can't find it anywhere mentioned in official docs http://satyan.github.io/sugar/getting-started.html – Mudassir Nov 20 '17 at 04:27
6

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! ;)

NewUser
  • 156
  • 1
  • 9
  • This results in the same error for me (table not found), but now due to this line rather than the original save() where it was being fired - i.e. the query failing is now a `SELECT` instead of a `INSERT or REPLACE`. I am running an Application Test on an Emulator. Any ideas? :( – Angad Mar 02 '16 at 17:47
6

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

enter image description here

W.Dan
  • 86
  • 1
  • 3
  • Finally, I find out what makes wrong! I set the wrong value of "DOMAIN_PACKAGE_NAME", it suppose to be the value of your model class's package name! – W.Dan Jul 17 '17 at 09:27
  • It works fine. But I didn't removed line. I just changed the value to correct package name of my project. – Umesh Suryawanshi Aug 20 '18 at 10:20
5

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

Hussain Mansoor
  • 2,934
  • 2
  • 27
  • 40
4

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.

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
2

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.

rdenadai
  • 433
  • 2
  • 10
2

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

gurehbgui
  • 14,236
  • 32
  • 106
  • 178
2

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!

P Kuijpers
  • 1,593
  • 15
  • 27
  • this worked for me. I had to override but this is a Life saver! tools:replace="android:icon,android:allowBackup" android:allowBackup="false" – ssk Mar 03 '19 at 07:56
1

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.

Tom 'Blue' Piddock
  • 2,131
  • 1
  • 21
  • 36
1

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

1

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

0

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.** { *; }
Francesco Papagno
  • 617
  • 10
  • 29
  • the problem was that it was working fine before even though i didn't update the proguard file with my package name but once i added a lot of libraries and 65k method limit was reached. it started acting in a weird way. But i will try your solution and see if that works. – Sheraz Ahmad Khilji Apr 04 '16 at 08:56
  • I had this same problem a few days ago, and I solved it by disabling Instant run on Android Studio 2.0. But I doubt that this is your case, because of the date of your question. Anyway in the end, I switched Sugar with DBFlow for performance reasons. – Francesco Papagno Apr 04 '16 at 12:40
0

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.

atott
  • 880
  • 10
  • 15
0

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.

Rishabh Dugar
  • 606
  • 5
  • 16