以下是我在databasehelper类中所做的工作。
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);
}
`
这是我要的日志。
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)
最佳答案
我发现我的代码有什么问题。我在其他helper类的构造函数中初始化tablehelper类,然后再次调用databasehelper的oncreate,使其锁定数据库。
从其他tablehelper的构造函数中删除了tablehelper初始化,并将其放入需要的方法中。
谢谢您。