1

このdjango raw sql requestにとても混乱しています

設定.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 リクエスト

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);")

エラーが返されます(auth_userテーブルに何百人ものユーザーがいます)

AttributeError: 'int' object has no attribute 'endswith'

列をなして

for r in cursor2.fetchall():
    self.stdout.write(r[0])

そして、私はこの行を期待しています

cursor2.execute("insert into auth_user_groups(user_id,group_id) values (1,1);")

auth_user_groups テーブル ビットに 1 行挿入しませんでした。

ここで何が起こっているのですか?

ありがとう

アップデート

現在、問題は解決されています。私は欠けていた

connections['default'].commit() 

インサート用

4

1 に答える 1