上面這個是一個文件路徑,我現在在弄一個服務器端的個人文件夾(基于Web),想把這個文件路徑整出來。說明:就是子文件夾有一個名為fatherId的保存父文件夾主鍵的字段,我想這個邏輯大家應該都懂?,F在我的問題是:如果已知最后一級就是gen這個數據的id,我怎么利用java遞歸或是循環(當然要多次檢索數據庫)得到String path = ”根目錄\Android\outlet\gen“ 這個字符串,因為我要在服務器的此目錄下創建文件夾或文件夾,這個算法邏輯我有點混亂。求Java牛人幫忙,萬謝不辭?。。?
1 回答
FFIVE
TA貢獻1797條經驗 獲得超6個贊
我不認為多次查數據庫是個好習慣,不過除了oracle,別的數據庫好像都沒遞歸查詢。無所謂,用程序就用程序吧
class DBHelper(object):
def __init__(self, dbName):
self.dbName = dbName
def conn(self):
import sqlite3
self.conn = sqlite3.connect(self.dbName)
def close(self):
self.conn.close()
def getDir(self, subId, dirList):
c = self.conn.cursor()
dir = c.execute('select file_name,father_id from test where id=?', subId)
row = c.fetchone()
name = row[0]
pId = str(row[1])
dirList.append(name)
c.close()
if pId != '0':
self.getDir(pId, dirList)
if __name__ == '__main__':
db = DBHelper('temp.sqlite')
db.conn()
myDir = []
db.getDir('3', myDir)
myDir.reverse()
myDir = '/'.join(myDir)
print myDir
db.close()數據庫和測試結果:

沒有jdbc驅動,懶得開eclipse,相信改寫成java的應該很簡單吧 ;) 希望對你有所幫助
添加回答
舉報
0/150
提交
取消
