1

I am getting the following error while I am trying to perform CRUD operations on my db using sugarorm:

E/SQLiteLog: (1) no such table: CONTACT D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.sinha.abhishek.demoapp, PID: 2133
              android.database.sqlite.SQLiteException: no such table: CONTACT (code 1): , while compiling: SELECT * FROM CONTACT WHERE id=? LIMIT 1
                  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.SQLiteQuery.<init>(SQLiteQuery.java:37)
                  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1240)
                  at com.orm.SugarRecord.find(SugarRecord.java:192)
                  at com.orm.SugarRecord.findById(SugarRecord.java:102)
                  at com.example.sinha.abhishek.demoapp.Contact.<init>(Contact.java:31)
                  at com.example.sinha.abhishek.demoapp.MainActivity$1.onClick(MainActivity.java:48)
                  at android.view.View.performClick(View.java:4785)
                  at android.view.View$PerformClick.run(View.java:19888)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5276)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706) 

Can anybody please guide, what might be the probable cause for this and how can it be fixed.

This is my pojo class:

public class Contact extends SugarRecord{

     int contactid;
     String firstName;
     String lastName;
     int age;
     String address;
     String phone;

    public Contact(){
        super();
    }

    public Contact(int contactid, String firstName, String lastName, int age, String address, String phone){
        this.contactid = contactid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.address = address;
        this.phone= phone;

    }


}

This is my android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sinha.abhishek.demoapp">

    <application
        android:name="com.orm.SugarApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">



        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <meta-data
            android:name="DATABASE"
            android:value="hollywoodcontacts.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.sinha.abhishek.demoapp" />



    </application>

</manifest>

This is my sample code for CRUD operation written within main activity:

Contact  con  = new Contact(101 ,"Abcd","Xyz",22,"myaddress", "9849848943");
con.save();

I have already tried options like: 1. Disabling instant run 2. Contact.findById(Contact.class, (long) 1);

but none of then worked for me.

Directory structure of my project

A. Sinha
  • 2,666
  • 3
  • 26
  • 45
  • The cause is as it says there is not a Table named **CONTACT** in the database. You are also told that the error happened while compiling the SELECT statement. If you have hard coded **CONTACT** anywhere, then perhaps it should be **Contact** (table names are case sensitive as are column names). My guess is that the select is part of checking that the contact (as per id) doesn't already exist. – MikeT Jan 12 '17 at 05:49

3 Answers3

3

Android Studio instant run might be the problem (known SugarORM issue), try turning it off (further help can be found here).

However, those answers didn't help me with the same problem, and my guess is that doesn't help you either, because you've got allowBackup="true" declared in your manifest. Try setting it to false, run your app, and remove > re-install it.

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, but it's my best guess. Hope it'll help you too!

P Kuijpers
  • 1,593
  • 15
  • 27
1

Make sure you have correct domain package name. It generates tables up to these package. That package must have your SugarRecord classes.

 <meta-data
        android:name="DOMAIN_PACKAGE_NAME"
        android:value="com.example.sinha.abhishek.demoapp.(yourmodelpackage)" />

As you updated your question your packages looks good. The problem is you have added contactid to your package which is detected as custom field for sugar orm. Because sugar orm has own id implementation and findById works on it.

Please update your class as below;

public class Contact extends SugarRecord{

 String firstName;
 String lastName;
 int age;
 String address;
 String phone;

public Contact(){
    super();
}

public Contact(long contactid, String firstName, String lastName, int age, String address, String phone){
    setId(contactid)
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.address = address;
    this.phone= phone;
}}

And if you want to find a data up to a custom field you can modify given example. NamingHelper is critical here because your fields names are different than column names.

CampaignDAO dao = CampaignDAO.find(CampaignDAO.class, NamingHelper.toSQLNameDefault("isPersonal") + " = ? ", "1").get(0);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • I have added the screenshot of the directory structure of my app, I guess the package is correct or do I need to specify the class name as well? – A. Sinha Jan 12 '17 at 06:15
  • After making the changes in POJO as suggested by you, I am still getting the same error. `android.database.sqlite.SQLiteException: no such table: CONTACT (code 1): , while compiling: INSERT OR REPLACE INTO CONTACT(ID,FIRST_NAME,AGE,ADDRESS,LAST_NAME,PHONE) VALUES (?,?,?,?,?,?)` – A. Sinha Jan 12 '17 at 07:07
  • Can you try to make a clean install? (Unistall and install it agian.) Looks like you have version 2 database but didint suplied migration queries. You may add Contact SugarRecord later? – Emre Aktürk Jan 12 '17 at 08:14
  • I have same problem with sugar orm. I did edit my code as you mentioned in your answer but the problem is still with "no such table.." error – Younes Jafari Apr 22 '17 at 10:26
-1

Adding a default constructor should solve this problem:

public class User 
{
    public User() {
    }
    ..
}