2017-07-16 16 views
-1

このコードは、システムC:とdb name SchoolのSQLite dbからデータを取得するdbhelperクラスです。メソッドのgetinformationがthrowされているエラーテーブルが存在しません。誰かが同じことを助けてもらえますか?このコードは、私のシステムC:とdb name SchoolのSQLite dbからデータを取得するdbhelperクラスです。メソッドのgetinformationがthrowされているエラーテーブルが存在しません。誰かが同じことを助けてもらえますか? このコードは、私のシステムC:とdb name SchoolのSQLite dbからデータを取得するdbhelperクラスです。メソッドのgetinformationがthrowされているエラーテーブルが存在しません。誰かが同じことを助けてもらえますか?すべての最初のSQLiteからデータを取得

package com.example.rabin_pc.myproject; 

    import android.app.Notification; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.SQLException; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteException; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.os.Message; 
    import android.support.v4.content.ContextCompat; 
    import android.util.Log; 

    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 

    import static android.content.ContentValues.TAG; 
    import static android.database.sqlite.SQLiteDatabase.*; 
    import static android.database.sqlite.SQLiteDatabase.openOrCreateDatabase; 

    /** 
    * Created by Rabin_PC on 14-Jul-17. 
    */ 

    public class CollegeDBHelper extends SQLiteOpenHelper { 

     private static String DB_PATH; 


     private static String DB_NAME="School.sqllite"; 

     private static final int DB_VERSION=1; 

     private SQLiteDatabase myDataBase; 
     private Context myContext=null; 

     public CollegeDBHelper(Context context){ 

      super(context,DB_NAME,null,DB_VERSION); 
      // log.e("DATABASE OPERATIONS" , "DATABASE OPENED"); 


     } 

     public CollegeDBHelper(Context ctx,String databaseName) { 
      super(ctx, databaseName, null, DB_VERSION); 
      DB_NAME = "School.db"; 
      this.myContext = ctx; 
      //DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getPath() ; 
      DB_PATH = "C:\\Users\\Rabin_PC\\Documents\\"; 
     } 

     public void createDataBase() throws IOException 
     { 
      boolean dbExist = checkDataBase(); 
      if(dbExist){ 
       //do nothing - database already exist 
      }else{ 
       //By calling this method and empty database will be created into the default system path 
       //of your application so we are gonna be able to overwrite that database with our database. 
       this.getReadableDatabase(); 
       try 
       { 
        copyDataBase(); 
       } catch (IOException e) 
       { 
        throw new Error("Error copying database"); 
       } 
      } 
     } 
     private boolean checkDataBase() 
     {  SQLiteDatabase checkDB = null; 
      try 
      { 
       String myPath = DB_PATH + DB_NAME; 

       Log.e(TAG,myPath); 
       checkDB = openDatabase(myPath, null, OPEN_READONLY); 
      }catch (SQLiteException e) 
      { 
       //database does't exist yet. 
      } 
      if(checkDB != null) 
      { 
       checkDB.close(); 
      } 
      return checkDB != null ? true : false; 
     } 

     private void copyDataBase() throws IOException 
     { 
      //Open your local db as the input stream 
      InputStream myInput = myContext.getAssets().open(DB_NAME); 
      // Path to the just created empty db 
      String outFileName = DB_PATH + DB_NAME; 
      //Open the empty db as the output stream 
      OutputStream myOutput = new FileOutputStream(outFileName); 
      //transfer bytes from the inputfile to the outputfile 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer))>0) 
      { 
       myOutput.write(buffer, 0, length); 
      } 
      //Close the streams 
      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 
     } 
     public void openDataBase() throws SQLException { 
      //Open the database 
      String myPath = DB_PATH + DB_NAME; 
      myDataBase = openDatabase(myPath, null, OPEN_READWRITE); 
     } 
     @Override 
     public synchronized void close() 
     { 
      if(myDataBase != null) 
       myDataBase.close(); 
      super.close(); 
     } 
     @Override 
     public void onCreate(SQLiteDatabase db) { 

     } 

     public Cursor getInformation(SQLiteDatabase db) 
     { 


       Cursor cursor; 

       String[] projections = {CollegeContract.newCollegeContract.COLLEGE_NAME, CollegeContract.newCollegeContract.COLLEGE_ADDRESS}; 
       // System.out.print("++++Before executing++++++++++++++ 4444444444444444"); 
       cursor = db.query(CollegeContract.newCollegeContract.TABLE_NAME, projections, null, null, null, null, null); 
       // System.out.print("++++Before executing++++++++++++++ 555555555555555"); 


        return cursor; 
     } 



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

     } 
    } 

+0

'DB_PATH =" C:\\ Users \\ Rabin_PC \\ Documents \\ ";'だから、あなたのAndroidデバイスでは、ドライブがあります。 'C:' ** ?! –

答えて

0

onCreate()であなたのテーブルを作成して、あなたはcreateDataBase()方法とonUpgrade()ドロップテーブルにして、再度のonCreate呼び出すメソッドと呼ばれていなかったとDB_NAMEはあなたがかもしれ2 DB_NAME School.sqlliteSchool.dbを使用異なっていますその上映エラーに名前を付け、DATABASE_NAME

のない初期化がないので、私は基本的なSQLiteOpenHelperクラス

を与えるだろう
関連する問題