1

我正在尝试将 json 提要导入我的数据库。我首先编写了一个纯 python 脚本,然后对其进行编辑以使用 Django 的数据库函数。

def getListOfProperties():
    if getLastModifiedTimestamp() != 0:
        url = URL + "?from_date=" + str(getLastModifiedTimestamp())
    else:
        url = URL
    resp,cont= client.request(url, "GET")
    results = json.loads(cont)

    try:
        properties = results['properties']
    except KeyError, e:
        return "Geen panden in Max-immo"

    for property in properties:
        if checkIfPropertyInDBList(id=property['id'])== 0:
            #do something
        else:
            #do the other thing

    if len(properties)!=0:
        addPropertiesDetail()


getListOfProperties()

在纯 python 脚本中,函数checkIfpropertyDbList如下所示:

import settings
def checkIfPropertyInDBList(id=0):
    db = settings.db()
    cur = db.cursor()
    if id==0:
        cur.execute("SELECT COUNT(*) FROM mi_import")
    else:
        cur.execute("SELECT COUNT(*) FROM mi_import WHERE id = %s" % id)
    return cur.fetchone()[0]

设置模块如下所示:

###settings.py
import MySQLdb

def db():
    return MySQLdb.connect(
        host= "localhost",
        user="root",
        passwd="*****",
        db="*****",
    )

在终端中运行脚本时,它工作得很好。问题是,我需要将脚本集成到部署在 Google App Engine 上的 Django 站点中,所以我调整了上面的函数以使用 Django 的内置数据库模块:

from django.db import connection, DatabaseError

def checkIfPropertyInDBList(id=0):
    cur = connection.cursor()
    if id==0:
        cur.execute("SELECT COUNT(*) FROM mi_import")
    else:
        cur.execute("SELECT COUNT(*) FROM mi_import WHERE id = %s" % id)
    return cur.fetchone()[0]

然而,这给了我一个TypeError: 'int' object is not subscriptable

这是完整的回溯:

Environment:


Request Method: GET
Request URL: localhost:8088/cron/importAll/

Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'south',
 'app']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.csrf.CsrfResponseMiddleware')


Traceback:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_1_3/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/username/PycharmProjects/project/app/cron.py" in lookupProperties
  180.     getListOfProperties()
File "/Users/username/PycharmProjects/project/app/cron.py" in getListOfProperties
  169.             if checkIfPropertyInDBList(id=property['id'])== 0:

Exception Type: TypeError at /cron/importAll/
Exception Value: 'int' object is not subscriptable

有什么建议么?

4

0 回答 0