简体   繁体   English

Python SQLite3-cursor.execute-没有错误

[英]Python SQLite3 - cursor.execute - no error

This is a piece of code which needs to perform the follow functionality: 这是一段代码,需要执行以下功能:

  • Dump all table names in a database 转储数据库中的所有表名
  • From each table search for a column with either Latitude or Longitude in 从每个表中搜索一个包含“纬度”或“经度”的列
  • Store these co-ords as a json file 将这些坐标存储为json文件

The code was tested and working on a single database. 该代码已经过测试,可以在单个数据库上工作。 However once it was put into another piece of code which calls it with different databases it now is not entering line 49. However there is no error either so I am struggling to see what the issue is as I have not changed anything. 但是,一旦将它放到另一段代码中,并用不同的数据库对其进行调用,它现在就不会进入第49行。但是也没有错误,因此我一直在努力寻找问题所在,因为我没有进行任何更改。

Code snippet line 48 is the bottom line - 代码段第48行是最底行-

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

I am running this in the /tmp/ dir due to an earlier error with sqlite not working outside the temp. 我在/ tmp /目录中运行此命令,原因是sqlite先前出现错误,无法在温度范围之外工作。

Any questions please ask them. 有任何问题请问他们。

Thanks!! 谢谢!!

#!/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)

Fixed. 固定。

The issue was up here 问题在这里

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

The filename variable was being overwritten without the file extension so when SQLite searched it didn't find the file. 没有文件扩展名的文件名变量被覆盖,因此当SQLite搜索时找不到文件。

Strange no error appeared but it is fixed now by changing the filename var to filename1. 奇怪的是没有错误出现,但是现在可以通过将文件名var更改为filename1来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM