本文介绍了得到错误在Android的SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
10月9日至三日:53:15.349:E / SQLiteLog(25173):(14)在30191行无法打开文件[00bb9c9ce4]
10月9日至3日:53:15.349:E / SQLiteLog(25173):(14)os_unix.c:30191:(2)开(/data/data/uk.org.nt.android.app1/databases/ntdb) -
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):无法打开数据库'/data/data/uk.org.nt.android.app1/databases/ntdb。
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在uk.org.nt.android.dynamic.dao.HandbookDBHelper.getAttractionsFromDB(HandbookDBHelper.java:152)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在uk.org.nt.android.dynamic.dao.HandbookDBHelper.findAttractionsForDeletion(HandbookDBHelper.java:96)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在uk.org.nt.android.service.UpdateService.performDeletions(UpdateService.java:270)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在uk.org.nt.android.service.UpdateService.handleHtt presponse(UpdateService.java:138)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在uk.org.nt.android.dynamic.http.HTTPSHelper.onPostExecute(HTTPSHelper.java:91)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在uk.org.nt.android.dynamic.http.HTTPSHelper.onPostExecute(HTTPSHelper.java:1)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.os.AsyncTask.finish(AsyncTask.java:631)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
10月9日至3日:53:15.359:E / SQLiteDatabase(25173):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
公共类HandbookDBHelper扩展SQLiteOpenHelper {
公共静态最终诠释REALM = 1; //手册
私有静态最后弦乐LOG_TAG =HandbookDBHelper;
//为您的应用程序的数据库文件名 - 变化的东西
//适合您的应用
//私有静态最后弦乐DATABASE_NAME =NTDB;
//您更改数据库对象的任何时候,你可能要
//增加数据库版本
私有静态最终诠释DATABASE_VERSION = 1; 文件DBFILE = NULL; 私人静态字符串DATABASE_NAME =NTDB; 上下文语境; 静态的SimpleDateFormat pricesFormatter =新的SimpleDateFormat(YYYY-MM-DD); 静态最后长EPOCH_DIFF = 978307200; 公共HandbookDBHelper(上下文的背景下){
超级(上下文,DATABASE_NAME,空,1); //设置数据库版本 this.context =背景; 字符串fullDBName = context.getDatabasePath(DATABASE_NAME)
.getAbsolutePath();
DBFILE =新的文件(fullDBName);
} 公共无效unpackDatabase()抛出IOException 的OutputStream databaseOutputStream =新的FileOutputStream(DBFILE);
InputStream的databaseInputStream; 字节[]缓冲区=新的字节[1024];
INT partSuffix = 0;
INT DBID;
字符串dbPart preFIX =partdb;
长bytesWritten = 0;
而((DBID = context.getResources()则getIdentifier(
dbPart preFIX + partSuffix,原始,context.getPackageName()))!= 0){
databaseInputStream = context.getResources()openRawResource(DBID)。 而((databaseInputStream.read(缓冲液))大于0){
databaseOutputStream.write(缓冲液);
bytesWritten + = 1024;
}
databaseInputStream.close();
partSuffix ++;
}
databaseOutputStream.flush();
databaseOutputStream.close(); } / **
*其中的吸引力存在在数据库上,但在不存在
*进来的名单。
*
* /
公共地图<弦乐,HandbookAttraction> findAttractionsForDeletion(
HandbookAttractions handbookAttractions){
地图<弦乐,HandbookAttraction> attractionsMap = getAttractionsFromDB(); 对于(HandbookAttraction handbookAttraction:handbookAttractions
.getHandbookAttractionsList()){
如果(attractionsMap.get(handbookAttraction.getUUID())!= NULL){
attractionsMap.remove(handbookAttraction.getUUID());
}
}
返回attractionsMap;
} / **
*
* @参数attractionsList
* @返回
* /
公共地图<弦乐,HandbookAttraction> findAttractionsForUpdate(
HandbookAttractions handbookAttractions){ 地图<弦乐,HandbookAttraction> attractionsMap = getAttractionsFromDB(); //比较版本
Log.d(LOG_TAG,比较版本);
对于(HandbookAttraction handbookAttraction:handbookAttractions
.getHandbookAttractionsList()){
HandbookAttraction dbAttraction = attractionsMap
获得(handbookAttraction.getUUID());
如果(dbAttraction == NULL){
Log.d(LOG_TAG,NEW+ handbookAttraction.getName());
}否则如果(dbAttraction.getVersion()< handbookAttraction
.getVersion()){
Log.d(LOG_TAG,UPDATE:+ handbookAttraction.getName()
+由+ dbAttraction.getVersion()+到
+ handbookAttraction.getVersion());
}其他{
Log.d(LOG_TAG,不改变:+ handbookAttraction.getName()
+是+ dbAttraction.getVersion()+现在
+ handbookAttraction.getVersion()); attractionsMap.remove(dbAttraction.getUUID());
}
}
Log.d(LOG_TAG,添加或更改总数
+ attractionsMap.size());
// attractionsMap现在包含需要更新或增加景点。 返回attractionsMap;
} / **
*可从DB景点名单,并通过键控地图归还
* UUID
*
* @返回
* /
公共地图<弦乐,HandbookAttraction> getAttractionsFromDB(){
SQLiteDatabase attractionsDB = SQLiteDatabase.openDatabase(
dbFile.getAbsolutePath(),空,SQLiteDatabase.OPEN_READONLY); 地图<弦乐,HandbookAttraction> attractionsMap =新的Hashtable<弦乐,HandbookAttraction>(); 光标光标= attractionsDB.rawQuery(
选择_id,ZVERSION,ZUUID从ZATTRACTION,NULL);
INT CPOS = 0;
cursor.moveToFirst();
而(CPOS< cursor.getCount()){
HandbookAttraction吸引力=新HandbookAttraction();
attraction.setDbId(cursor.getInt(光标
.getColumnIndexOrThrow(_ ID)));
attraction.setVersion(cursor.getInt(光标
.getColumnIndexOrThrow(ZVERSION)));
attraction.setUUID(cursor.getString(光标
.getColumnIndexOrThrow(ZUUID))); attractionsMap.put(
cursor.getString(cursor.getColumnIndexOrThrow(ZUUID)),
吸引);
CPOS ++;
cursor.moveToPosition(CPOS); }
cursor.close(); attractionsDB.close();
Log.d(LOG_TAG,装+ attractionsMap.size()
+从数据库景点); 返回attractionsMap;
} 公共无效showDb(){ } @覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根 } @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根 } / **
*执行更新/除了给定的吸引力
*
* @参数handbookAttractionDetail
* /
公共无效updateAttraction(
HandbookAttractionDetail handbookAttractionDetail){
SQLiteDatabase attractionsDB = SQLiteDatabase.openDatabase(
dbFile.getAbsolutePath(),空,SQLiteDatabase.OPEN_READWRITE); INT attractionRowId = clearDownAttraction(handbookAttractionDetail,
attractionsDB);
布尔isNewAttraction =(attractionRowId == -1);
//
//现在重新构建
//
rebuildAttraction(handbookAttractionDetail,isNewAttraction);
attractionsDB.close();
}
解决方案
在特定的,有将要访问给定文件路径的数据库中的openDatabase()命令。
SQLiteDatabase DB = SQLiteDatabase.openDatabase(路径,NULL,0);
只要指定的路径到你的数据库的路径变量,它应该工作。
09-03 10:53:15.349: E/SQLiteLog(25173): (14) cannot open file at line 30191 of [00bb9c9ce4]
09-03 10:53:15.349: E/SQLiteLog(25173): (14) os_unix.c:30191: (2) open(/data/data/uk.org.nt.android.app1/databases/ntdb) -
09-03 10:53:15.359: E/SQLiteDatabase(25173): Failed to open database '/data/data/uk.org.nt.android.app1/databases/ntdb'.
09-03 10:53:15.359: E/SQLiteDatabase(25173): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at uk.org.nt.android.dynamic.dao.HandbookDBHelper.getAttractionsFromDB(HandbookDBHelper.java:152)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at uk.org.nt.android.dynamic.dao.HandbookDBHelper.findAttractionsForDeletion(HandbookDBHelper.java:96)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at uk.org.nt.android.service.UpdateService.performDeletions(UpdateService.java:270)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at uk.org.nt.android.service.UpdateService.handleHttpResponse(UpdateService.java:138)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at uk.org.nt.android.dynamic.http.HTTPSHelper.onPostExecute(HTTPSHelper.java:91)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at uk.org.nt.android.dynamic.http.HTTPSHelper.onPostExecute(HTTPSHelper.java:1)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-03 10:53:15.359: E/SQLiteDatabase(25173): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
public class HandbookDBHelper extends SQLiteOpenHelper {
public static final int REALM = 1; // handbook
private static final String LOG_TAG = "HandbookDBHelper";
// name of the database file for your application -- change to something
// appropriate for your app
// private static final String DATABASE_NAME = "ntdb";
// any time you make changes to your database objects, you may have to
// increase the database version
private static final int DATABASE_VERSION = 1;
File dbFile = null;
private static String DATABASE_NAME = "ntdb";
Context context;
static SimpleDateFormat pricesFormatter = new SimpleDateFormat("yyyy-MM-dd");
static final long EPOCH_DIFF = 978307200;
public HandbookDBHelper(Context context) {
super(context, DATABASE_NAME, null, 1); // set db version
this.context = context;
String fullDBName = context.getDatabasePath(DATABASE_NAME)
.getAbsolutePath();
dbFile = new File(fullDBName);
}
public void unpackDatabase() throws IOException {
OutputStream databaseOutputStream = new FileOutputStream(dbFile);
InputStream databaseInputStream;
byte[] buffer = new byte[1024];
int partSuffix = 0;
int dbId;
String dbPartPrefix = "partdb";
long bytesWritten = 0;
while ((dbId = context.getResources().getIdentifier(
dbPartPrefix + partSuffix, "raw", context.getPackageName())) != 0) {
databaseInputStream = context.getResources().openRawResource(dbId);
while ((databaseInputStream.read(buffer)) > 0) {
databaseOutputStream.write(buffer);
bytesWritten += 1024;
}
databaseInputStream.close();
partSuffix++;
}
databaseOutputStream.flush();
databaseOutputStream.close();
}
/**
* where an attraction exists on the database but does not exist in the
* incomming list.
*
*/
public Map<String, HandbookAttraction> findAttractionsForDeletion(
HandbookAttractions handbookAttractions) {
Map<String, HandbookAttraction> attractionsMap = getAttractionsFromDB();
for (HandbookAttraction handbookAttraction : handbookAttractions
.getHandbookAttractionsList()) {
if (attractionsMap.get(handbookAttraction.getUUID()) != null) {
attractionsMap.remove(handbookAttraction.getUUID());
}
}
return attractionsMap;
}
/**
*
* @param attractionsList
* @return
*/
public Map<String, HandbookAttraction> findAttractionsForUpdate(
HandbookAttractions handbookAttractions) {
Map<String, HandbookAttraction> attractionsMap = getAttractionsFromDB();
// compare versions
Log.d(LOG_TAG, "comparing versions");
for (HandbookAttraction handbookAttraction : handbookAttractions
.getHandbookAttractionsList()) {
HandbookAttraction dbAttraction = attractionsMap
.get(handbookAttraction.getUUID());
if (dbAttraction == null) {
Log.d(LOG_TAG, "NEW: " + handbookAttraction.getName());
} else if (dbAttraction.getVersion() < handbookAttraction
.getVersion()) {
Log.d(LOG_TAG, "UPDATE: " + handbookAttraction.getName()
+ " from " + dbAttraction.getVersion() + " to "
+ handbookAttraction.getVersion());
} else {
Log.d(LOG_TAG, "NO CHANGE: " + handbookAttraction.getName()
+ " was " + dbAttraction.getVersion() + " now "
+ handbookAttraction.getVersion());
attractionsMap.remove(dbAttraction.getUUID());
}
}
Log.d(LOG_TAG, "total number of additions or changes :"
+ attractionsMap.size());
// attractionsMap now contains attractions requiring update or addition.
return attractionsMap;
}
/**
* get the list of attractions from the DB and return them in a map keyed by
* UUID
*
* @return
*/
public Map<String, HandbookAttraction> getAttractionsFromDB() {
SQLiteDatabase attractionsDB = SQLiteDatabase.openDatabase(
dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
Map<String, HandbookAttraction> attractionsMap = new Hashtable<String, HandbookAttraction>();
Cursor cursor = attractionsDB.rawQuery(
"select _id, ZVERSION, ZUUID from ZATTRACTION", null);
int cpos = 0;
cursor.moveToFirst();
while (cpos < cursor.getCount()) {
HandbookAttraction attraction = new HandbookAttraction();
attraction.setDbId(cursor.getInt(cursor
.getColumnIndexOrThrow("_id")));
attraction.setVersion(cursor.getInt(cursor
.getColumnIndexOrThrow("ZVERSION")));
attraction.setUUID(cursor.getString(cursor
.getColumnIndexOrThrow("ZUUID")));
attractionsMap.put(
cursor.getString(cursor.getColumnIndexOrThrow("ZUUID")),
attraction);
cpos++;
cursor.moveToPosition(cpos);
}
cursor.close();
attractionsDB.close();
Log.d(LOG_TAG, "loaded " + attractionsMap.size()
+ " attractions from db");
return attractionsMap;
}
public void showDb() {
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/**
* perform update / addition for given attraction
*
* @param handbookAttractionDetail
*/
public void updateAttraction(
HandbookAttractionDetail handbookAttractionDetail) {
SQLiteDatabase attractionsDB = SQLiteDatabase.openDatabase(
dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
int attractionRowId = clearDownAttraction(handbookAttractionDetail,
attractionsDB);
boolean isNewAttraction = (attractionRowId == -1);
//
// now re-build
//
rebuildAttraction(handbookAttractionDetail, isNewAttraction);
attractionsDB.close();
}
解决方案
In particular, there's an openDatabase() command that will access a database given a file path.
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);
Just specify the path to your database as the path variable, and it should work.
这篇关于得到错误在Android的SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!