在本地数据库中,我有记录,其中包含有关目录和文件(文件系统树)的信息。例如,表" Dir"具有以下属性: record_id (autoincr。), dir_id , dirname , parentDir_id 。 我需要从数据库创建文件和目录树:
Dir_1
|
-----Dir_1.1
-----Dir_1.2
-----File_1.1
Dir_2
|
-----Dir_2.1
-----Dir_2.2
-----File_2.1
-----File_2.2
Dir_3
File_1
File_2
所以,我使用递归方法。 我的功能:
/**
* Append new childs for target dir
* @param targetObject directory
*/
public void addChilds(@Nullable SimpleSyncDirModel targetObject){
//no target - main parent dir.
if (targetObject == null){
//get childs for this target object (main dir without parent)
ArrayList<Object> foundedChilds = ThisApp.getDBhelper().getSharedSimpleObjects(this.dir_id);
//step by step (childs). recursive mecthod.
for (Object thisChild : foundedChilds){
if (thisChild instanceof SimpleSyncDirModel){
addNewChildDir((SimpleSyncDirModel) thisChild);
//find and add childs for current child
addChilds((SimpleSyncDirModel) thisChild);
}else if (thisChild instanceof SimpleSyncFileModel){
addNewChildFile((SimpleSyncFileModel) thisChild);
}
}
}else{
//get childs for this target object (not main, has parent)
ArrayList<Object> foundedChilds = ThisApp.getDBhelper().getSharedSimpleObjects(targetObject.dir_id);
//iterate, find and append childs (recursive)
for (Object thisChild : foundedChilds){
if (thisChild instanceof SimpleSyncDirModel){
targetObject.addNewChildDir((SimpleSyncDirModel) thisChild);
//recursive
addChilds((SimpleSyncDirModel) thisChild);
}else if (thisChild instanceof SimpleSyncFileModel){
targetObject.addNewChildFile((SimpleSyncFileModel) thisChild);
}
}
}
}
如您所见,我使用功能
ThisApp.getDBhelper()getSharedSimpleObjects(targetObject.dir_id);
用于搜索目标目录的孩子。 targetObject.dir_id - parent_id for&#34; SELECT&#34;。
我的搜索子功能:
/**
* Get childs with parent_id == 0 (without parent) or not (with parent)
* @param parendDir_id ID of dir, for what we search childs
* @return list of childs like {@link SimpleSyncDirModel}
* and {@link ru.rsit.megashare.models.global.SimpleSyncFileModel}
*/
public ArrayList<Object> getSharedSimpleObjects(String parendDir_id){
ArrayList<Object> result = new ArrayList<>();
//looking child dirs
String[] columns_ = new String[]{
"dir_id", "dirnameWithPath"
};
Cursor db_cursor = megashare_db.query(
"SharedDir", columns_,
"(parentDir_id = ?)",
new String[] { parendDir_id },
null, null, null);
if (db_cursor.getCount() > 0) {
db_cursor.moveToFirst();
while (db_cursor.getPosition() != db_cursor.getCount()) {
SimpleSyncDirModel simpleSyncDirModel = new SimpleSyncDirModel();
simpleSyncDirModel.dir_id =
Integer.toString(db_cursor.getInt(db_cursor.getColumnIndex("dir_id")));
simpleSyncDirModel.dirnameWithPath =
db_cursor.getString(db_cursor.getColumnIndex("dirnameWithPath"));
result.add(simpleSyncDirModel);
db_cursor.moveToNext();
}
}
db_cursor.close();
//looking child files
columns_ = new String[]{
"file_id", "filenameWithPath"
};
db_cursor = megashare_db.query(
"SharedFile", columns_,
"(parentDir_id = ?)",
new String[] { parendDir_id },
null, null, null);
if (db_cursor.getCount() > 0) {
db_cursor.moveToFirst();
while (db_cursor.getPosition() != db_cursor.getCount()) {
SimpleSyncFileModel simpleSyncFileModel = new SimpleSyncFileModel();
simpleSyncFileModel.file_id =
Integer.toString(db_cursor.getInt(db_cursor.getColumnIndex("file_id")));
simpleSyncFileModel.filenameWithPath =
db_cursor.getString(db_cursor.getColumnIndex("filenameWithPath"));
result.add(simpleSyncFileModel);
db_cursor.moveToNext();
}
}
db_cursor.close();
return result;
}
当我运行我的应用程序并尝试创建文件系统树时,我总是看到 java.lang.StackOverflowError :
FATAL EXCEPTION: Thread-23110
java.lang.StackOverflowError
at java.lang.ref.WeakReference.<init>(WeakReference.java:108)
at java.util.WeakHashMap$Entry.<init>(WeakHashMap.java:71)
at java.util.WeakHashMap.put(WeakHashMap.java:611)
at android.database.sqlite.SQLiteConnectionPool.finishAcquireConnectionLocked(SQLiteConnectionPool.java:980)
at android.database.sqlite.SQLiteConnectionPool.tryAcquirePrimaryConnectionLocked(SQLiteConnectionPool.java:916)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:682)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:400)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:905)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1283)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1154)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1322)
at ru.rsit.megashare.managers.DBHelper.getSharedSimpleObjects(DBHelper.java:582)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:64)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
at
错误在于:Cursor db_cursor = megashare_db.query
为什么我有这个错误?当然我总是关闭游标。我需要做些什么来解决这个问题?
答案 0 :(得分:0)
错误的原因是代码中的错误,我尝试在数据库目标目录中查找以进行检查:此目录是否没有父级。所以,如果我删除子对象,我的应用程序会复制一些目录。这就是递归循环的原因。