没有这样的表错误,我该怎么办? APP CRASH ON LAUNCH

时间:2017-11-08 05:18:51

标签: android sqlite android-layout android-studio

我的代码一直存在一个重大问题我认为我的数据库中的表格很好我已经有三个列_id,jobNM,JobDetails并且我已经在几周内完成了所有工作我回到了同样的问题。我得到“无法启动活动ComponentInfo”,并在Logcat中说“引起:android.database.sqlite.SQLiteException:没有这样的表:作业(代码1):,同时编译:SELECT jobNM FROM job” 。我已经获得了下面的所有代码和logcat以及任何想法吗?

[编辑] 这里是sqlite的db DB浏览器的一些图片以及文件路径

db布局 enter image description here

文件路径

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) 

2 个答案:

答案 0 :(得分:0)

使用SQL-Lite资源管理器检查表是否存在,意味着是否存在表

答案 1 :(得分:0)

我已经启动了我的应用程序,它变得复杂和混乱,我意识到我的数据库路径错误