上面這個是一個文件路徑,我現(xiàn)在在弄一個服務(wù)器端的個人文件夾(基于Web),想把這個文件路徑整出來。說明:就是子文件夾有一個名為fatherId的保存父文件夾主鍵的字段,我想這個邏輯大家應(yīng)該都懂?,F(xiàn)在我的問題是:如果已知最后一級就是gen這個數(shù)據(jù)的id,我怎么利用java遞歸或是循環(huán)(當(dāng)然要多次檢索數(shù)據(jù)庫)得到String path = ”根目錄\Android\outlet\gen“ 這個字符串,因為我要在服務(wù)器的此目錄下創(chuàng)建文件夾或文件夾,這個算法邏輯我有點混亂。求Java牛人幫忙,萬謝不辭?。?!
1 回答
FFIVE
TA貢獻(xiàn)1797條經(jīng)驗 獲得超6個贊
我不認(rèn)為多次查數(shù)據(jù)庫是個好習(xí)慣,不過除了oracle,別的數(shù)據(jù)庫好像都沒遞歸查詢。無所謂,用程序就用程序吧
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()數(shù)據(jù)庫和測試結(jié)果:

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