2011-10-18 18 views
0

SQLiteデータベースブラウザを使用してデータベースを作成しました。私はこれに従わなければならないexample。データベースの enter image description hereSQLiteデータベースからデータを取得するときにエラーが発生する

名は次のとおりです。一つのテーブルを持っているjokesdatabase.sqlジョーク 私は資産の場所これは次のようにフォルダ:資産フォルダ

は今、私のコードは次のとおりです。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 
private Context mycontext; 

private String DB_PATH = "/data/data/a.b.c/databases/"; 
private static String DB_NAME = "jokesdatabase.sql"; //I have aslo tru .db extenstion 
private static String DB_TABLE = "jokes"; 

public SQLiteDatabase myDataBase; 

public DatabaseHelper(Context context) throws IOException { 
    super(context, DB_NAME, null, 1); 
    this.mycontext = context; 
    boolean dbexist = checkdatabase(); 
    if (dbexist) { 
     // System.out.println("Database exists"); 
     opendatabase(); 
    } else { 
     // System.out.println("Database doesn't exist"); 
     createdatabase(); 
    } 
} 

public void createdatabase() throws IOException { 
    boolean dbexist = checkdatabase(); 
    if (dbexist) { 
     System.out.println(" Database exists."); 
    } else { 
     this.getReadableDatabase(); 
     try { 
      copydatabase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 
} 

private boolean checkdatabase() { 
    // SQLiteDatabase checkdb = null; 
    boolean checkdb = false; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     File dbfile = new File(myPath); 
     checkdb = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READWRITE) != null; 
     checkdb = dbfile.exists(); 
    } catch (SQLiteException e) { 
     System.out.println("Database doesn't exist"); 
    } 

    return checkdb; 
} 

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 byte to inputfile to 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 = SQLiteDatabase.openDatabase(mypath, null, 
      SQLiteDatabase.OPEN_READONLY); 

} 

public synchronized void close() { 
    if (myDataBase != null) { 
     myDataBase.close(); 
    } 
    super.close(); 
} 

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

} 

// This will return a cursor containing database records 
public Cursor data() { 

    Cursor c; 
    c = myDataBase.query(DB_TABLE, null, null, null, null, null, null); 
    return c; 
} 

@Override 
public void onCreate(SQLiteDatabase arg0) { 
    // TODO Auto-generated method stub 

} 

}

Result.java

のAndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".DatabaseHelper" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".Result" android:label="@string/app_name"></activity> 
</application> 

最後に、logcatで次のエラーメッセージが表示されます。 enter image description here

私のコードのエラーは何ですか?私はsqliteブラウザを使用してデータベースを作成したいだけです。ビアコーディングソリューションを提供しないでください。 この問題のみを修正してください。linkです。

+0

StackTraceをテキストとして投稿してください! –

答えて

2

あなたのAndroidマニフェストであなたのDatabaseHelperをアクティビティとして定義していない場合は、ではないActivityです。

DatabaseHelperを他のどのクラスと同じようにActivityから作成して使用してください。

+0

正常に動作します。ありがとうございます。 –

関連する問題