问题1.django 中连接数据库使用ORM框架,新增数据时,产生报错。
问题描述: DatabaseError: [CODE:-2111]第1 行附近出现错误:无效的列名[AAAAAAAAAAAAAAAAAR]
查看数据库发现数据已经录入,问题出现在返回新增结果时候。通过查找发现是django_dmpython中自带文件调用方法时候的原生语法错误。
解决办法(windows)
修改文件:C:\Users\lenovo\Env\django2\Lib\site-packages\django_dmPython\operations.py
def last_insert_id(self, cursor, table_name, pk_name):
"""
Given a cursor object that has just performed an INSERT statement into
a table that has an auto-incrementing ID, returns the newly created ID.
This method also receives the table name and the name of the primary-key
column.
"""
# sq_name = self._get_sequence_name(table_name)
# cursor.execute('SELECT "%s".currval FROM dual' % sq_name)
if cursor.lastrowid is not None:
# query = 'select %s from %s where rowid = %s' %(self.quote_name(pk_name), self.quote_name(table_name), cursor.lastrowid)
# 把上面的代码注释掉,改成下面的!!!!!!!:
query = """select %s from %s where rowid = '%s'""" % (self.quote_name(pk_name), self.quote_name(table_name), cursor.lastrowid )
cursor.execute(query)
else:
cursor.execute('SELECT MAX(%s) from %s' %(self.quote_name(pk_name), self.quote_name(table_name)))
value = cursor.fetchone()[0]
return value
大功告成!!!
解决办法(中标麒麟)
修改文件:/lib/python3.9/site-packages/django_dmPython/operations.py
# 错误语句:
query = 'select %s from %s where rowid = %s' %(self.quote_name(pk_name), self.quote_name(table_name), cursor.lastrowid)
错误原因:where 条件查询中如果条件是字符串需要在语句中标记为字符串。
# 解决方式:修改本文件中语句为:
query = """select %s from %s where rowid = '%s'""" %(self.quote_name(pk_name), self.quote_name(table_name), cursor.lastrowid)
大功告成!!!
问题2:cannot import name 'Random'
from django.db.models.expressions import F, OrderBy, Random, RawSQL, Ref, Value
ImportError: cannot import name 'Random' from 'django.db.models.expressions' (/usr/local/lib/python3.10/site-packages/django/db/models/expressions.py)
打开文件:C:\Users\lenovo\Env\django2\Lib\site-packages\django_dmPython\compiler.py
C:\Users\lenovo\Env\django2\Lib\site-packages\django_dmPython\compiler.py,注释掉Random部分