2017-01-07 38 views
0

這是一段代碼,這需要執行如下功能:從每個表搜索與列在數據庫中 Python的sqlite3的 - cursor.execute - 沒有錯誤

    • 轉儲所有的表名或者經緯度在
    • 存儲這些共ORDS作爲JSON文件

    的代碼進行了測試,一個單一的數據庫上工作。然而,一旦將它放入另一段用不同數據庫調用它的代碼中,它現在不會輸入第49行。但是沒有錯誤,所以我很難看出問題是什麼,因爲我沒有改變任何東西。

    代碼段第48行是底線 -

    cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") 
    print (cursor) 
        for tablerow in cursor.fetchall(): 
    

    我在/ tmp /目錄運行此因使用SQLite不是臨時在外打工以前的錯誤。

    有任何問題請諮詢他們。

    謝謝!

    #!/usr/bin/python 
    # -*- coding: utf-8 -*- 
    
    import sqlite3 
    import os 
    import sys 
    
    filename = sys.argv[1] 
    
    
    def validateFile(filename): 
        filename, fileExt = os.path.splitext(filename) 
        print ("[Jconsole] Python: Filename being tested - " + filename) 
        if fileExt == '.db': 
         databases(filename) 
        elif fileExt == '.json': 
         jsons(fileExt) 
        elif fileExt == '': 
         blank() 
        else: 
         print ('Unsupported format') 
         print (fileExt) 
    
    
    def validate(number): 
        try: 
         number = float(number) 
         if -90 <= number <= 180: 
          return True 
         else: 
          return False 
        except ValueError: 
         pass 
    
    
    def databases(filename): 
        dbName = sys.argv[2] 
        print (dbName) 
        idCounter = 0 
        mainList = [] 
        lat = 0 
        lon = 0 
        with sqlite3.connect(filename) as conn: 
         conn.row_factory = sqlite3.Row 
         cursor = conn.cursor() 
         cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") 
         print (cursor) 
         for tablerow in cursor.fetchall(): 
          print ("YAY1") 
          table = tablerow[0] 
          cursor.execute('SELECT * FROM {t}'.format(t=table)) 
          for row in cursor: 
           print(row) 
           print ("YAY") 
           tempList = [] 
           for field in row.keys(): 
            tempList.append(str(field)) 
            tempList.append(str(row[field])) 
           for i in tempList: 
            if i in ('latitude', 'Latitude'): 
             index = tempList.index(i) 
             if validate(tempList[index + 1]): 
              idCounter += 1 
              tempList.append(idCounter) 
              (current_item, next_item) = \ 
               (tempList[index], tempList[index + 1]) 
              lat = next_item 
            if i in ('longitude', 'Longitude'): 
             index = tempList.index(i) 
             if validate(tempList[index + 1]): 
              (current_item, next_item) = \ 
               (tempList[index], tempList[index + 1]) 
              lon = next_item 
            result = '{ "id": ' + str(idCounter) \ 
             + ', "content": "' + dbName + '", "title": "' \ 
             + str(lat) + '", "className": "' + str(lon) \ 
             + '", "type": "box"},' 
            mainList.append(result) 
        file = open('appData.json', 'a') 
        for item in mainList: 
         file.write('%s\n' % item) 
        file.close() 
    
    
        # { 
        # ...."id": 1, 
        # ...."content": "<a class='thumbnail' href='./img/thumbs/thumb_IMG_20161102_151122.jpg'>IMG_20161102_151122.jpg</><span><img src='./img/thumbs/thumb_IMG_20161102_151122.jpg' border='0' /></span></a>", 
        # ...."title": "50.7700721944444", 
        # ...."className": "-0.8727045", 
        # ...."start": "2016-11-02 15:11:22", 
        # ...."type": "box" 
        # }, 
    
    def jsons(filename): 
        print ('JSON') 
    def blank(): 
        print ('blank') 
    
    validateFile(filename) 
    
  • +0

    使用'json.dumps'代替構造json數據。 – Daniel

    +0

    'tempList'應該是一個元組列表,而不是一個列表,其中兩個連續的元素屬於一個列表。 – Daniel

    回答

    0

    固定。

    的問題是在這裏

    filename, fileExt = os.path.splitext(filename) 
    

    沒有,所以當SQLite的搜查沒有發現該文件的文件擴展名的文件名可變正被改寫。

    奇怪的是沒有錯誤出現,但現在通過將文件名var更改爲filename1來修復它。