我是django的新手。我想在MySQL数据库中插入一个新行,但是当我尝试执行以下操作时,它会出错。
from django.db import models
...
class Msg(models.Model):
MsgId = BigIntegerField(length = 20)
ToUserName = CharField(max_length = 45)
FromUserName = CharField(max_length = 45)
Content = TextField(max_length = 1024, blank = True)
...
db_entry = Msg(MsgId=received_MsgId, ToUserName=received_ToUserName,
FromUserName=received_FromUserName, MsgType=received_MsgType,
Content=received_Content)
db_entry.save()
以下是我的数据库中存在的表all_massages
,以及如何向其中添加新行,以及我还需要做些什么。
+--------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+-------+
| MsgId | bigint(20) unsigned | NO | PRI | NULL | |
| ToUserName | varchar(45) | NO | | NULL | |
| FromUserName | varchar(45) | NO | | NULL | |
| Content | text | YES | | NULL | |
+--------------+---------------------+------+-----+---------+-------+
答案 0 :(得分:1)
如果您更改了模式并执行了syncdb ...它将无法工作....所以删除现有的表并执行syncdb !!
答案 1 :(得分:0)
Django文档清楚地指定了如何执行原始SQL查询。 https://docs.djangoproject.com/en/dev/topics/db/sql/#performing-raw-sql-queries
用于直接执行UPDATE,INSERT或DELETE查询。 https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly