Folgendes mache ich in meiner DatabaseHelper-Klasse.

public DatabaseHelper(Context context)
    {
        super(context, DATABASENAME, null, VERSION);
        context.openOrCreateDatabase(DATABASENAME, context.MODE_PRIVATE, null);

    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        String queryProductsAllData = "CREATE TABLE `"+TABLE_PRODUCTS+"` (\n" +
                        "\t`"+COLUMN_BRANDID                +"`\tINTEGER,\n" +
                        "\t`"+COLUMN_PRODUCTID              +"`\tINTEGER,\n" +
                        "\t`"+COLUMN_PRODUCTCODE            +"`\tINTEGER,\n" +
                        "\t`"+COLUMN_PIECESINCASE           +"`\tINTEGER,\n" +
                        "\t`"+COLUMN_PRODUCT_STOCK_UNITS    +"`\tINTEGER DEFAULT 0,\n" +
                        "\t`"+COLUMN_PRODUCTADDEDBY         +"`\tINTEGER,\n" +
                        "\t`"+COLUMN_BRANDCODE              +"`\tTEXT,\n" +
                        "\t`"+COLUMN_BRANDNAME              +"`\tTEXT,\n" +
                        "\t`"+COLUMN_PRODUCTNAME            +"`\tTEXT,\n" +
                        "\t`"+COLUMN_PRODUCTSHORTNAME       +"`\tTEXT,\n" +
                        "\t`"+COLUMN_PIECENETVOLUME         +"`\tTEXT,\n" +
                        "\t`"+COLUMN_PRICEPOINT             +"`\tTEXT,\n" +
                        "\t`"+COLUMN_TRADEPRICEPERCASE      +"`\tTEXT,\n" +
                        "\t`"+COLUMN_TRADEPRICEPERPIECE     +"`\tTEXT,\n" +
                        "\t`"+COLUMN_RETAILPRICEPERPIECE    +"`\tTEXT,\n" +
                        "\t`"+COLUMN_SERVICECHARGEPERPIECE  +"`\tTEXT,\n" +
                        "\t`"+COLUMN_SPDSHOPPERPRICEPERPIECE+"`\tTEXT,\n" +
                        "\t`"+COLUMN_JTAXPERCASE            +"`\tTEXT,\n" +
                        "\t`"+COLUMN_SPDPRICEPERCASE        +"`\tTEXT,\n" +
                        "\t`"+COLUMN_PRODUCTADDEDON         +"`\tTEXT,\n" +
                        "\t`"+COLUMN_PRODUCTSTATUS          +"`\tINTEGER\n" +
                        ");";
    db.execSQL(queryProductsAllData);

    }

 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_PRODUCTS);
}
`

Hier ist das Protokoll, das ich bekomme.

 12-28 10:27:52.509    2672-2672/? E/SQLiteLog﹕ (14) cannot open file at line 31278 of [2ef4f3a5b1]
12-28 10:27:52.509    2672-2672/? E/SQLiteLog﹕ (14) os_unix.c:31278: (24) open(/data/user/0/com.vergesystems.heartbeat/databases/Heart_Beat-journal) -
12-28 10:27:52.510    2672-2672/? E/SQLiteLog﹕ (14) cannot open file at line 31278 of [2ef4f3a5b1]
12-28 10:27:52.510    2672-2672/? E/SQLiteLog﹕ (14) os_unix.c:31278: (24) open(/data/user/0/com.vergesystems.heartbeat/databases/Heart_Beat-journal) -
12-28 10:27:52.510    2672-2672/? E/SQLiteLog﹕ (14) unable to open database file
12-28 10:27:52.512    2672-2672/? E/SQLiteDatabase﹕ Failed to open database '/data/user/0/com.vergesystems.heartbeat/databases/Heart_Beat'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14): , while compiling: PRAGMA journal_mode
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
            at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
            at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:318)
            at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:292)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
            at com.vergesystems.databases.ProductsTableHelper.<init>(ProductsTableHelper.java:29)
3
Saraz 28 Dez. 2015 im 08:49

2 Antworten

Beste Antwort

Ich habe festgestellt, was mit meinem Code nicht stimmt. Ich habe die TableHelper-Klasse im Konstruktor anderer Hilfsklassen initialisiert und dabei erneut onCreate von DatabaseHelper aufgerufen, sodass die Datenbank gesperrt wurde.

Die TableHelper-Initialisierung wurde aus dem Konstruktor des anderen TableHelper entfernt und dort platziert, wo sie benötigt wurde.

Vielen Dank.

2
Saraz 21 Sept. 2016 im 15:07

Entferne das

context.openOrCreateDatabase(DATABASENAME, context.MODE_PRIVATE, null);

Sie verwenden SQLiteOpenHelper und es verwaltet die Datenbankdatei für Sie.

0
laalto 28 Dez. 2015 im 12:07