2017-07-16 94 views
-1

此代碼是dbhelper類,用於從我的系統C:和db name School中的SQLite數據庫獲取數據。方法getinformation正在拋出錯誤表不存在。有人可以請同樣的幫助。這段代碼是dbhelper類,用於從我的系統C:和db name School中的SQLite數據庫中獲取數據。方法getinformation正在拋出錯誤表不存在。有人可以請同樣的幫助。 這段代碼是dbhelper類,用於從我的系統C:和db name School中的SQLite數據庫中獲取數據。方法getinformation正在拋出錯誤表不存在。有人可以請同樣的幫助。從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() DROP TABLE和調用的onCreate再次方法和DB_NAME是使用了兩DB_NAME School.sqlliteSchool.db可能會有所不同因爲它的名字錯誤的表現並沒有什麼的DATABASE_NAME

初始化我將給出一個基本的SQLiteOpenHelper類

public class mySQLiteHelper extends SQLiteOpenHelper { 

     private static String DB_NAME="School.sqllite"; 


private static final String DATABASE_CREATE = "create table " 
      + TABLE_COMMENTS + "(" + COLUMN_ID 
      + " integer primary key autoincrement, " + COLUMN_COMMENT 
      + " text not null);"; 

    public MySQLiteHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     database.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(MySQLiteHelper.class.getName(), 
       "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS); 
     onCreate(db); 
    } 

}