通过 Rawsql 插入到 mysql 数据库

Insert to mysql database through Rawsql

我想在 mysql 数据库 table 上执行客户 sql 查询以存储记录 .通过rawsql.

插入查询的方法是什么

我正在使用以下型号

class Student(models.Model):
    userid = models.CharField(db_column='UserId', primary_key=True, max_length=12)  # Field name made lowercase.
    firstname = models.CharField(db_column='FirstName', max_length=30)  # Field name made lowercase.
    middlename = models.CharField(db_column='MiddleName', max_length=30, blank=True, null=True)  # Field name made lowercase.
    lastname = models.CharField(db_column='LastName', max_length=30)  # Field name made lowercase.
    password = models.CharField(db_column='Password', max_length=50, blank=True, null=True)  # Field name made lowercase.

并尝试通过原始方法插入值,因为想通过 mysql 提供的 md5('') 存储密码:

st = Student.object.raw("Insert into Student values(%s,%s,%s,%s,md5(%s))",['ST1','Fname','Mname','Lname','Pwd']);

但我不知道如何执行 this.Is ?

这里有一个简单的例子:

from django.db import connections

cursor = connections['default'].cursor()
cursor.execute("INSERT INTO TABLE(field1,field2) VALUES( %s , %s )", [value1, value2])