0
我试图访问一个数据库中的数据库表。Django无法使用多个数据库检索数据
我尝试使用MytableObject.get.using(数据库名称)第一,但它扔错误:
django.db.utils.DatabaseError: relation "mytable" does not exist
所以我着手试图找出原因。只要我知道即时没有做任何事情比平常不同:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseDefault', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseDefault', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseDefault', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseDefault', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseDefault', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
},
'databaseEe': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseEe', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseEe', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseEe', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseEe', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseEe', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
},
'databaseLv': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseLv', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseLv', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseLv', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseLv', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseLt', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
},
'databaseLt': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseLt', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseLt', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseLt', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseLt', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseLt', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
}
}
,我开了一些testcode:
from django.db import connections
cursor = connections[database].cursor()
cursor.execute("select tablename from pg_tables WHERE tablename !~* 'pg_*'")
print cursor.fetchall()
这其中打印出我所有的表名,“MYTABLE”。
然后我尝试这种代码:
from django.db import connections
cursor = connections[database].cursor()
cursor.execute("select * from mytable")
print cursor.fetchall()
再次产生错误:
django.db.utils.DatabaseError: relation "mytable" does not exist
LINE 1: select * from mytable
那我做错了什么?我可以读取和写入我的默认数据库就好了。我跟踪下来到这条线:
cursor = connections[database].cursor()
,结果发现,如果我写了实际的连接名称,而不是数据库中,这样的:
cursor = connections['databaseLv'].cursor()
然后它工作得很好。那么我该如何动态地做到这一点?
艾伦