2012-03-25 15 views
1

私はSQLiteデータベースをEclipseに接続する必要があるチュートリアルに従いました。しかし、どういうわけか私はいつもこのエラーを受け取り、それをptしていましたが、それでもエラーは常に出ていますか?コンストラクタRestaurant.DBHelper(レストラン)は定義されていません

public Restaurant open() throws SQLException 
{ 
    ourHelper = new DBHelper(this); 

これは私が続くのリンクです:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

public class Restaurant { 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "name"; 
public static final String KEY_DETAIL = "detail"; 
public static final String KEY_PRICE = "price"; 


private static final String DATABASE_NAME ="Restaurantdb"; 
private static final String DATABASE_TABLE ="Food"; 
private static final int DATABASE_VERSION = 1; 
private static String DB_PATH = "/data/data/com.restaurant.sesame/databases/"; 

private static SQLiteDatabase ourDatabase; 
private final Context ourContext; 
private DBHelper ourHelper; 


public class DBHelper extends SQLiteOpenHelper{ 


    private final Context myContext; 


    public DBHelper(Context context) { 

     super(context, DATABASE_NAME, null, 1); 
     this.myContext = context; 
    } 


    public void createDataBase() throws IOException{ 

     boolean dbExist = checkDataBase(); 

     if(dbExist){ 
      //do nothing - database already exist 
     }else{ 


      this.getReadableDatabase(); 

      try { 

       copyDataBase(); 

      } catch (IOException e) { 

       throw new Error("Error copying database"); 

      } 
     } 

    } 


    private boolean checkDataBase(){ 

     SQLiteDatabase checkDB = null; 

     try{ 
      String myPath = DB_PATH + DATABASE_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.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(DATABASE_NAME); 

     // Path to the just created empty db 
     String outFileName = DB_PATH + DATABASE_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 + DATABASE_NAME; 
     ourDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    } 

    @Override 
    public synchronized void close() { 

      if(ourDatabase != null) 
       ourDatabase.close(); 

      super.close(); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

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

    } 



} 
public Restaurant(Context c) 
{ 
    ourContext = c; 
} 

public Restaurant open() throws SQLException 
{ 
    ***ourHelper = new DBHelper(this);*** 


    try { 

     ourHelper.createDataBase(); 

} catch (IOException ioe) { 

    throw new Error("Unable to create database"); 

} 

try { 

    ourHelper.openDataBase(); 

}catch(SQLException sqle){ 

    throw sqle; 

} 
    return this; 
} 

public void close() 
{ 
    ourHelper.close(); 
} 
+0

を支援するデータベースとの接続を設定する方法に

希望としてステップバイステップでガイドしますでるコンストラクタ – Akram

+0

私はそれをコメントアウトしませんでした。私の食では普通です。 –

+0

![これは私のスクリーンショットです] [1]:http://i.stack.imgur.com/ZY504.png –

答えて

0

あなたは右のコンテキストを指定する必要があります。コンストラクタのような引数を探している私を賭け:あなたはそのエラーになります当然の

new DBHelper(MyActivity.this);

+0

私はDBHelper.thisを試しましたが、使用はありませんでした。 –

0

も...あなたはDBHelperクラスのコンストラクタとコンパイラをコメントアウトしているときにそれを見つけることができませんコンストラクタを呼び出す...

は、これらのチュートリアルhttp://thenewboston.org/list.php?cat=6をご確認ください....彼らはあなたが、これは、あなたがコメント思わ

関連する問題