Android: Sugar ORM No Such Table Exception
Asked Answered
C

21

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)
Cottonmouth answered 9/10, 2015 at 6:43 Comment(1)
I am also getting no such table exception after adding two more parameters in the table.Osteoma
L
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.

Limousine answered 7/4, 2016 at 14:4 Comment(8)
This problem was occuring before the instant run option was available so thankyou for your reply but it seemed to be another issue.Cottonmouth
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.Ellaelladine
I have to disable and reinstall the app.Objective
Exactly, thanks, sugar doesn't work with android studio 2.0 if there is instant run enabled.Royalty
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'.Pop
Disable Instant run AND reinstall the app - disabling the feature alone is not enough.Cyrillic
I must add that you may re-enable Instant Run after the first install of the app with it disabled.Sachasachem
Great point @Sachasachem ! 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.Amputate
N
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!

Nevanevada answered 13/4, 2016 at 12:28 Comment(4)
Я голову сломал почему у меня ничего не работает! Спасибо тебе огромное! // I broke the head why nothing works for me! Thanks to you huge!Baggage
For me I had to disable Instant Run and uninstall the app.Seaway
I must add that you may re-enable Instant Run after the first install of the app with it disabled.Sachasachem
Perfect SolutionBhagavadgita
F
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.

Fantinlatour answered 26/5, 2016 at 15:42 Comment(5)
May be your problem is differentFantinlatour
@ farhad rubel Solved the issue by disabling instant run option in android studioPeugia
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.Fantinlatour
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.Treasure
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 ...Scutiform
W
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();
    }
}
Whitesell answered 7/11, 2016 at 4:20 Comment(4)
This worked for me, but no need to add SugarContext.terminate(), it is being called through SugarApp superNurmi
It works. Turns out neither instant run or my manifest was the culpritJackquelin
So much, Dude !Complot
It worked didn't try it with the enabled instant run,But if it works those two lines you found are so precious. :) thank you.Wraith
O
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"/>
Ogre answered 9/10, 2015 at 6:50 Comment(1)
This is not the issue. its not creating table for some reason.Cottonmouth
D
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

Disbelieve answered 7/6, 2016 at 11:8 Comment(1)
This tip didn't resolve he issue. I also didn't notice anywhere that VERSION has to greater than 1.Cyrillic
O
8

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

Osteoma answered 16/4, 2018 at 8:1 Comment(0)
E
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());
Eyepiece answered 4/7, 2017 at 11:18 Comment(1)
Yeah, it worked. Strange, I can't find it anywhere mentioned in official docs satyan.github.io/sugar/getting-started.htmlPi
H
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! ;)

Hysterectomize answered 12/10, 2015 at 14:23 Comment(1)
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? :(Simard
H
6

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

enter image description here

Halliehallman answered 17/7, 2017 at 9:14 Comment(2)
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!Halliehallman
It works fine. But I didn't removed line. I just changed the value to correct package name of my project.Estovers
O
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

Olivenite answered 3/12, 2015 at 8:14 Comment(0)
D
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.

Dunois answered 2/6, 2016 at 8:0 Comment(0)
G
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.

Grumpy answered 9/10, 2015 at 11:29 Comment(1)
Thanks, I have forgot to make empty constructor.Oenone
O
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

Ogletree answered 7/3, 2017 at 9:45 Comment(0)
T
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!

Treasure answered 14/6, 2017 at 21:37 Comment(1)
this worked for me. I had to override but this is a Life saver! tools:replace="android:icon,android:allowBackup" android:allowBackup="false"Sudan
D
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.

Deannadeanne answered 31/1, 2016 at 17:21 Comment(0)
H
1

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

Halfblood answered 4/7, 2016 at 15:9 Comment(0)
J
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

Jemimah answered 27/4, 2017 at 10:30 Comment(0)
O
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.** { *; }
Outdoor answered 2/4, 2016 at 10:48 Comment(2)
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.Cottonmouth
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.Outdoor
M
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.

Mediterranean answered 28/4, 2016 at 11:2 Comment(0)
O
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.

Orta answered 1/6, 2019 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.