Hallo,
und zwar habe ich eine SQLite Datenbank Klasse und möchte ein Objekt erstellen um auf die Klasse zuzugreifen. Nur kann ich keine Klasse in meiner Activity erzeugen. Es wäre schön wenn mir jemand helfen könnte.
Java
package fahrtenbuch.gui;
import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;
public class database_open_handler extends SQLiteOpenHelper {
private static final String TAG = database_open_handler.class .getSimpleName();
// Name und Version der Datenbank private static final String DATABASE_NAME = "logbook.db"; private static final int DATABASE_VERSION = 1; // Name und Attribute der Tabelle "mood" public static final String ID = "ID"; public static final String TABLE_NAME = "drive"; public static final String DATE = "DATE"; public static final String START_TIME = "START_TIME"; public static final String ARRIVE_TIME = "ARRIVE_TIME"; public static final String START_LOCATION = "START_LOCATION"; public static final String END_LOCATION = "END_LOCATION"; public static final String DRIVE_KM = "DRIVE_KM"; // Tabelle mood anlegen private static final String TABLE_MOOD_CREATE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " TEXT, " + START_TIME + " TEXT, " + ARRIVE_TIME + " TEXT, " + START_LOCATION + " TEXT, " + END_LOCATION + " TEXT, " + DRIVE_KM + " TEXT);"; // Tabelle mood löschen database_open_handler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_MOOD_CREATE); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrade der Datenbank von Version " + oldVersion + " zu " + newVersion + "; alle Daten werden gelöscht"); onCreate(db); } public void insert(String date, String start_time, String arrive_time, String start_location, String end_location, String drive_km) { // Tabellen felder als para übergeben long rowId = -1; try { // Datenbank öffnen SQLiteDatabase db = getWritableDatabase(); // die zu speichernden Werte ContentValues values = new ContentValues(); values.put(DATE, date); values.put(START_TIME, start_time); values.put(ARRIVE_TIME, arrive_time); values.put(START_LOCATION, start_location); values.put(END_LOCATION, end_location); values.put(DRIVE_KM, drive_km); // in die Tabelle mood einfügen rowId = db.insert(TABLE_NAME, null, values); } catch (SQLiteException e) { Log.e(TAG, "insert()", e); } finally { Log.d(TAG, "insert(): rowId=" + rowId); } } public Cursor query() { // ggf. Datenbank öffnen SQLiteDatabase db = getWritableDatabase(); return db.query(TABLE_NAME, null, null, null, null, null, DATE + " DESC"); } public void update(long id, int smiley) { // ggf. Datenbank öffnen SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put(DATE, smiley); int numUpdated = db.update(TABLE_NAME, values, ID + " = ?", new String[] { Long.toString(id) }); Log.d(TAG, "update(): id=" + id + " -> " + numUpdated); } public void delete(long id) { // ggf. Datenbank öffnen SQLiteDatabase db = getWritableDatabase(); int numDeleted = db.delete(TABLE_NAME, ID + " = ?", new String[] { Long.toString(id) }); Log.d(TAG, "delete(): id=" + id + " -> " + numDeleted);}
}
Vielen Dank.