Search code examples
pythonmysqldjangodjango-modelsfieldtype

Incorrect value - python to model in Django


The problem I have is when trying to insert some values ​​in the database created by Django model, the values ​​are strictly numeric, but when trying to insert it I get an error:

567523
<type 'int'>
Warning: Incorrect integer value: 'id' for column 'id_unidad' at row 1
cursor.execute(sql, ('id',))

the code I'm using to insert using a mysql request is:

import MySQLdb
import _mysql

id = 567523
print id
print type(id)

sql = """INSERT INTO gprs_evento ( id_unidad ) VALUES (%s)"""

db = MySQLdb.Connect(host="localhost", user="*****",passwd="******",db="gp")
cursor = db.cursor()

try :
    cursor.execute(sql, ('id',))
    db.commit()
except _mysql.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])

Django model I'm using is:

from django.db import models

class Evento(models.Model):
    id_unidad = models.IntegerField(max_length=15)

Attempt to insert in each field in the database:

id: 13831010240120
<type 'long'>
ip:  3235021102
<type 'long'>
e: 2
<type 'int'>
H:  102718.0
<type 'float'>
LN:  210.128871
<type 'float'>
LO:  3203.323664
<type 'float'>
V:  28.0
<type 'float'>
A:  90.0
<type 'float'>
F:  40101.0
<type 'float'>

In the model I'm using the reference of the Field types and try and IntegerField, BigIntegerField, DecimalField but nothing, I just inserted a normal number of 15 characters from Python.


Solution

  • You're trying to insert the string "id", rather than the value of the variable id.

    It should be:

    cursor.execute(sql, (id,))
    

    Of course, there's no reason to be using raw SQL for this at all.