我的Android sqlite数据库有问题,我只在jellybean版本上获取数据库锁定异常(尚未检查kitkat)。显示的是异常的logcat文件。
04-14 18:40:02.517: E/SQLiteLog(3917): (5) database is locked
04-14 18:40:02.524: E/SQLiteDatabase(3917): Failed to open database '/data/data/com.example.assets.screens/databases/AssetDB.db'.
04-14 18:40:02.524: E/SQLiteDatabase(3917): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:757)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:443)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:417)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:323)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:217)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:464)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:186)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:926)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at com.example.assets.utils.AssetDB.getAssetData(AssetDB.java:169)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.view.View.performClick(View.java:4222)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.view.View$PerformClick.run(View.java:17343)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.os.Handler.handleCallback(Handler.java:615)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.os.Looper.loop(Looper.java:137)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-14 18:40:02.524: E/SQLiteDatabase(3917): at dalvik.system.NativeStart.main(Native Method)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): Couldn't open AssetDB.db for writing (will try read-only):
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:757)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:443)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:417)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:323)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:217)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:464)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:186)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:926)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
04-14 18:40:02.548: E/SQLiteOpenHelper(3917): at com.example.assets.utils.AssetDB.getAssetData(AssetDB.java:169)
04-14 18:40:05.087: W/System.err(3917): Error:null
以下是我的数据库代码。数据库锁定问题仅适用于软糖。我在数据库的一些检查上缺少。在第一次单击数据库锁定后,按钮单击时会发生数据库读取..
public class AstDB {
static SQLiteDatabase db;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "DB.db";
private static Context mContext;
private static AsDatabase database;
public AsDB(Context ctx) {
this.mContext = ctx;
}
public static class AsDatabase extends SQLiteOpenHelper {
public static synchronized AsDatabase getInstance(Context context) {
if (database == null) {
database = new AsDatabase(context);
}
return database;
}
public AsDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
InputStream mngr = null;
String line = null;
BufferedReader reader = null;
try {
mngr = mContext.getAssets().open("TABLE_QUERIES.txt");
reader = new BufferedReader(new InputStreamReader(mngr));
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (line != null) {
db.execSQL(line);
System.out.println("table created");
try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
// ---opens the database---
public DB open() throws SQLException {
db = AsDatabase.getInstance(mContext).getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
AsDatabase.getInstance(mContext).close();
}
public void insertAssetData() {
InputStream mngr = null;
try {
mngr = mContext.getAssets().open("INSERT_ALL_QUERIES.txt");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(mngr));
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* db.beginTransaction();
*
* while (line != null) {
*
* try { db.execSQL(line); line = reader.readLine(); //
* db.setTransactionSuccessful(); } catch (IOException e) { // TODO
* Auto-generated catch block // db.endTransaction();
* e.printStackTrace(); }
*
* } db.setTransactionSuccessful(); db.endTransaction();
*/
db.beginTransaction();
try {
while (line != null) {
db.execSQL(line);
line = reader.readLine();
}
db.setTransactionSuccessful();
} catch (IOException e) {
// TODO Auto-generated catch block
db.endTransaction();
e.printStackTrace();
}
db.endTransaction();
}
public ArrayList<String> getAssetData(ArrayList<String> queryList,
ArrayList<String> columnList) {
Cursor cursor = null;
ArrayList<String> resultArray = new ArrayList<String>();
try {
db = AsDatabase.getInstance(mContext).getReadableDatabase();
db.beginTransaction();
for (int i = 0; i < queryList.size(); i++) {
cursor = db.rawQuery(queryList.get(i), null);
if (cursor.moveToFirst()) {
do {
//
System.out.println("allIndex="
+ cursor.getColumnIndexOrThrow(columnList
.get(i)));
System.out.println("allData="
+ cursor.getString(cursor
.getColumnIndex(columnList.get(i))));
resultArray.add(i, cursor.getString(cursor
.getColumnIndex(columnList.get(i))));
} while (cursor.moveToNext());
}
}
db.setTransactionSuccessful();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} finally {
cursor.close();
db.endTransaction();
AsDatabase.getInstance(mContext).close();
}
return resultArray;
}
}
任何帮助都将受到高度赞赏