I am so confused by this django raw sql request
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'myapp', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
#'OPTIONS': { 'init_command': 'SET storage_engine=INNODB;' }
},
'mysql': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'myapp', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
#'OPTIONS': { 'init_command': 'SET storage_engine=INNODB;' }
}
}
sql request
from django.db import connections
cursor = connections['mysql'].cursor()
cursor2 = connections['default'].cursor()
cursor.execute("select user_id,group_id from auth_user_groups")
cursor2.execute("select id,username from auth_user")
for r in cursor2.fetchall():
self.stdout.write(r[0])
cursor2.execute("insert into auth_user_groups(user_id,group_id) values (1,1);")
It returns an error (I do have hundreds users in auth_user table)
AttributeError: 'int' object has no attribute 'endswith'
In line
for r in cursor2.fetchall():
self.stdout.write(r[0])
And I expect this line
cursor2.execute("insert into auth_user_groups(user_id,group_id) values (1,1);")
insert one line to auth_user_groups table bit it didn't.
What is going on here?
Thanks