问题
Here is what I am doing in my DatabaseHelper class.
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);
}
`
Here is the log I am getting.
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)
回答1:
I found what was wrong with my code. I was initialising TableHelper class in other helper classes' constructor and that was calling DatabaseHelper's onCreate again so that was making it lock the database.
Removed TableHelper initialisation from the other TableHelper's constructor and put it in method where it was needed.
Thank you.
回答2:
Remove the
context.openOrCreateDatabase(DATABASENAME, context.MODE_PRIVATE, null);
You're using SQLiteOpenHelper
and it does the database file management for you.
来源:https://stackoverflow.com/questions/34488306/unable-to-open-database-file-code-14-while-compiling-pragma-journal-mode