0

I've been having a major Issue with my code I think my table in my database is fine I've got three columns _id, jobNM, JobDetails and i've been over everything for weeks and I'm back to this same problem. I'm getting “Unable to start activity ComponentInfo” and in the Logcat it says “ Caused by: android.database.sqlite.SQLiteException: no such table: job (code 1): , while compiling: SELECT jobNM FROM job”. I've got all my code below and the logcat as well any idea?

[EDIT] here are some pictures of the db DB browser for sqlite as well as the file path

db layout enter image description here

file path

enter image description here

MainActivity

public class MainActivity extends AppCompatActivity {

    IntDataBaseHelper intDataBaseHelper;

    ListView lstJob;
    ArrayAdapter<String> mAdapter;





   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       intDataBaseHelper  = new IntDataBaseHelper(this);

        /*create instance of db helper and jobs
        Create the database (only if it doesn't exists)
        does so by copying from the assets */
       lstJob = (ListView) findViewById(R.id.lstJob);
        LoadJobList();
        if (CopyDBFromAssets.createDataBase(this,IntDataBaseHelper.DB_TABLE)) {
           // problem area
           // Get the data from the database

        ArrayList<String> jobs =  intDataBaseHelper.getJobList();
           for (String s : jobs) {
               Log.d("JobList ", "Found Job " + s);
              }
           } else {
               throw new RuntimeException("No Usable Database exists or was copied from the assets.");
           }

       }
      // loads job to screen
          public void LoadJobList() {
          ArrayList<String> JobList = intDataBaseHelper.getJobList();
          if (mAdapter == null) {
              mAdapter = new ArrayAdapter<>(this,R.layout.header,R.id.header);
              mAdapter = new ArrayAdapter<>(this,R.layout.row,R.id.BtnComplete,JobList);
              mAdapter = new ArrayAdapter<>(this, R.layout.row, R.id.Job_name,JobList);
              lstJob.setAdapter(mAdapter);
          } else
              {
              mAdapter.clear();
              mAdapter.addAll(JobList);
              mAdapter.notifyDataSetChanged();
          }
      }

}

IntDataBase

public class IntDataBaseHelper extends SQLiteOpenHelper{


   private static  String DB_PATH ="/data/data/com.example.joelg.clapp/databases";
   public static  String DB_NAME = "JobList";
   public static  String DB_COLUMN = "jobNM";
   public static  String DB_TABLE = "job";
   private static  String DB_JOB_DETAILS = "jobDetails";
   private static  String DB_ISDONE = "jobIsDone";
   private  Context jobContext;
   private SQLiteDatabase JobListDatabase;




       /**
        * constructor creater
        */
       public IntDataBaseHelper (Context context) {
           super (context, DB_NAME,null, 1);
           this.jobContext = context;
           DB_PATH = jobContext.getDatabasePath(DB_NAME).getPath();
       }


       public void OpenDataBase() {
           // open the database
           String JobListPath = DB_PATH;
           JobListDatabase = SQLiteDatabase.openDatabase(JobListPath,null,SQLiteDatabase.OPEN_READONLY);
       }


       // Getting Job Count
       public  ArrayList<String> getJobList() {
           ArrayList<String> JobList = new ArrayList<>();
           SQLiteDatabase db = this.getReadableDatabase();
           Cursor cursor =  db.query(DB_TABLE,new String[]
                   {DB_COLUMN},null,null,null,null,null);
               while(cursor.moveToNext()){
               int index = cursor.getColumnIndex(DB_COLUMN);
               JobList.add(cursor.getString(index));
           }

           cursor.close();
           db.close();
           return JobList;
       }


       // Gets the job state if it has been competed or not
   public ArrayList<String> getIsDone() {
       ArrayList<String>  IsDone = new ArrayList<>();
       SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursor = db.query(DB_TABLE,new String[]{DB_ISDONE},null,null,null,null,null);
       while(cursor.moveToNext()){
           int index = cursor.getColumnIndex(DB_ISDONE);
           IsDone.add(cursor.getString(index));
       }

       cursor.close();
       db.close();
       return IsDone;
   }




       @Override
       public synchronized void close(){

           if(JobListDatabase !=null){
               JobListDatabase.close();
               super.close();

           }
       }
       @Override
       public  void onCreate(SQLiteDatabase db) {

       }
       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       }
   }

LogCat

11-08 04:13:20.874 13790-13790/com.example.joelg.clapp E/SQLiteLog: (1) no such table: job
11-08 04:13:20.876 13790-13790/com.example.joelg.clapp E/AndroidRuntime: FATAL EXCEPTIO:main
Process: com.example.joelg.clapp, PID: 13790
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joelg.clapp/com.example.joelg.clapp.MainActivity}: android.database.sqlite.SQLiteException:no such table: job (code 1): , while compiling: SELECT jobNM FROM job
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
       Caused by: android.database.sqlite.SQLiteException: no such table: job (code 1): , while compiling: SELECT jobNM FROM job
    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:1318)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
    at com.example.joelg.clapp.IntDataBaseHelper.getJobList(IntDataBaseHelper.java:52)
    at com.example.joelg.clapp.MainActivity.LoadJobList(MainActivity.java:47)
    at com.example.joelg.clapp.MainActivity.onCreate(MainActivity.java:31)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Joel Geri
  • 47
  • 2
  • 9
  • Uninstall your app and install again – John Joe Nov 08 '17 at 05:22
  • 3
    Possible duplicate of [Caused by: android.database.sqlite.SQLiteException: no such table: (code 1) Android](https://stackoverflow.com/questions/24634116/caused-by-android-database-sqlite-sqliteexception-no-such-table-code-1-andr) – Ratilal Chopda Nov 08 '17 at 05:24
  • i have already uninstalled and reinstalled and it's not a duplicate the app is not working on any device – Joel Geri Nov 08 '17 at 05:40
  • i cannot see OpenDataBase() method. You didn't call the OpenDataBase() in your helper class before selection – missionMan Nov 08 '17 at 07:12
  • You should have uses [SQLiteAssetHelper](http://jgilfelt.github.io/android-sqlite-asset-helper/). – CL. Nov 08 '17 at 17:38
  • If you did updated your app, it could read the old SQLite file, I had exactly same issue before, and I solved it by change SQLite file to a new file name. – RRTW Nov 09 '17 at 02:05
  • Iv deleted all the DB files and I'm going to create a new one from scratch I should be putting it in my assets file? – Joel Geri Nov 09 '17 at 02:11
  • ok no luck there i cleared out all the databases and it cant find The job list table stille – Joel Geri Nov 09 '17 at 02:18
  • Had you rename your DB file ? – RRTW Nov 09 '17 at 02:21
  • I renamed it to masterList in the DB_NAME string and the actual file and no luck. – Joel Geri Nov 09 '17 at 02:37
  • Can you try to dump that DB file from phone ? https://stackoverflow.com/questions/4452538/location-of-sqlite-database-on-the-device/13154281#13154281 – RRTW Nov 09 '17 at 03:02
  • mm i don't thinks so the devices i need it to work for don't support and sd card – Joel Geri Nov 09 '17 at 03:04
  • You still can dump DB file into your phone's internal storage, like "download" folder... – RRTW Nov 09 '17 at 03:17
  • @RRTW isnt that where private static String DB_PATH = "/data/data/com.example.joelg.clapp/databases"; does ? – Joel Geri Nov 09 '17 at 03:29
  • That's SOURCE path, you can define your own TARGET path to any other accessible folder. – RRTW Nov 09 '17 at 05:58

2 Answers2

0

Using SQL-Lite explorer check whether the table exist or not, meaning is there even a table

LordGrim
  • 70
  • 5
0

I have started my app over it was getting to complicated and messy and i realised that i had the database path wrong

Joel Geri
  • 47
  • 2
  • 9