这是我在DatabaseHelper类中所做的事情。
public DatabaseHelper(上下文上下文)
{
super(context,DATABASENAME,null,VERSION);
context.openOrCreateDatabase(DATABASENAME,context.MODE_PRIVATE,null);
}
@Override
public void onCreate(SQLiteDatabase db){
字符串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 +` TEXTtTEXT,\n +
\t` + COLUMN_PRODUCTSTATUS +`\tINTEGER\n +
);;
db.execSQL(queryProductsAllData);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL(如果存在则删除表 + TABLE_PRODUCTS);
}
`
这是我得到的日志。
12-28 10:27:52.509 2672-2672 /? E / SQLiteLog:(14)无法在[2ef4f3a5b1]的第31278行打开文件
12-28 10:27:52.509 2672-2672 /? E / SQLiteLog:(14)os_unix.c:31278:(24)打开(/data/user/0/com.vergesystems.heartbeat/databases/Heart_Beat-journal)-
12-28 10:27:52.510 2672-2672 /? E / SQLiteLog:(14)无法在[2ef4f3a5b1]的第31278行打开文件
12-28 10:27:52.510 2672-2672 /? E / SQLiteLog:(14)os_unix.c:31278:(24)打开(/data/user/0/com.vergesystems.heartbeat/databases/Heart_Beat-journal)-
12-28 10:27:52.510 2672-2672 /? E / SQLiteLog:(14)无法打开数据库文件
12-28 10:27:52.512 2672-2672 /? E / SQLiteDatabase:无法打开数据库 /data/user/0/com.vergesystems.heartbeat/databases/Heart_Beat。
android.database.sqlite.SQLiteCantOpenDatabaseException:无法打开数据库文件(代码14):编译时:PRAGMA journal_mode
在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
在android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
在android.database.sqlite.SQLiteConnection .setJournalMode(SQLiteConnection.java:318)
在android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:292)
在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213 )
在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
在android.database.sqlite.SQLiteDatabase .openInner(SQLiteDatabase.java:806)
在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694 )
在android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
在android.database.sqlite。 SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
在com.vergesystems.databases.ProductsTableHelper。< init>(ProductsTableHelper .java:29)
解决方案
我发现我的代码出了什么问题。我在其他帮助程序类的构造函数中初始化TableHelper类,然后再次调用DatabaseHelper的onCreate,以使其锁定数据库。
从另一个TableHelper的构造函数中删除了TableHelper初始化,并将其放在需要的方法中。
谢谢
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)
解决方案 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.
这篇关于编译时无法打开数据库文件(代码14):PRAGMA journal_mode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!