2

The problem I have is to identify the type of data entering the database, I think everyone as IntegerField model in Django and Python code only inserts it all in one list and then insert it into the base data.

On the other hand I have no very clear writing Python code length according to the rules of the line, what I do is just see that in the end the code is very long as separated with spaces to align the next line below do not know if this good in this way and that the code will not fail.

The data that has to enter ip_unidad is ('186 .99.41.000 ', 3333) found in' self.addr [0] 'and the data date is '091211' which is in 'self.Cadenapura [17] '

and try "self.Cadenapura [17] = int (self.Cadenapura [17])" but nothing

It records the input data in the database but the two spaces are 0.

any ideas would be grateful.


The console error is:

Warning: Incorrect integer value: 'self.addr[0]' for column 'ip_unidad' at row 1
  ('self.addr[0]','self.Cadenapura[17]')
Warning:Incorrect integer value: 'self.Cadenapura[17]' for column 'fecha' at row 1
  ('self.addr[0]','self.Cadenapura[17]')

The code. Py used is:

sql = """INSERT INTO carro ( ip_unidad , hora ) VALUES (%s,%s)"""

db = MySQLdb.Connect(host="localhost", user="root",passwd="--------",db="gprslmgs")
cursor = db.cursor()

try :
    cursor.execute(sql,('self.addr[0]','self.Cadenapura[17]'))
    db.commit()
except:
    db.rollback()

Django model used to create the database is:

class Carro(models.Model):
    ip_unidad = models.IntegerField(max_length=15)
    fecha = models.IntegerField(max_length=6)

Thank you.

pcalcao
  • 15,789
  • 1
  • 44
  • 64
user987055
  • 1,039
  • 4
  • 14
  • 25

3 Answers3

1

I am not very familiar with Django but what i see is that you specify object within ' - think you don't need to do this. Have you tried something like:

cursor.execute(sql % (self.addr[0], self.Cadenapura[17]))

Or:

cursor.execute(sql, (self.addr[0], self.Cadenapura[17],))

While browsing i found the following MySQLdb sample code:

import MySQLdb

db = MySQLdb.connect(passwd="moonpie",db="thangs")

c = db.cursor()
c.executemany(
      """INSERT INTO breakfast (name, spam, eggs, sausage, price)
      VALUES (%s, %s, %s, %s, %s)""",
      [
      ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
      ("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
      ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
      ] )

So i think it should work the second way i mentioned.

Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
  • cursor.execute(sql, (self.addr[0], self.Cadenapura[17],)) I'll see what happens with: cursor.execute(sql % (self.addr[0], self.Cadenapura[17])) – user987055 Dec 12 '11 at 21:43
  • nothing! with the% do not I get anything, as if does not work, and the other I keep getting the warning – user987055 Dec 12 '11 at 21:59
  • Check please your query - i mean make a variable: query = sql % (self.addr[0], self.Cadenapura[17]) and then print it out or check in your DB - whether it works or not, correct or not. – Artsiom Rudzenka Dec 12 '11 at 22:02
  • My problem is no connection, as the code tries to get the information provided, it does just that seeing it from phpmyadmin 0, the problem is how to get the data remotely from another server, send it in single quotes, for hours eg '091211 'and not as recognized by the server, which is why I think I get "Incorrect integer value" I try to put the data that is listed as an int using "self.Cadenapura [17] = int (self.Cadenapura [17]) "but nothing, not what happens? – user987055 Dec 12 '11 at 22:14
  • Can you please post the latest version of code you have and point out the row that raising exception? This would help me to understand what do you really want and where a problem is. – Artsiom Rudzenka Dec 18 '11 at 17:42
0

I see two main problems - first is that '186.99.41.000' is not an integer, this is a string. Django provides an ip address field which is designed for this.

Your second problem is similar to the first, in that '09876' is a string, and your column type is IntegerField, however when you convert '09876' to an integer, you'll get 9876 because in Python, a number starting from 0 is an octal literal:

>>> print 127
127
>>> print 0177
127
>>> print 0x7f
127

So you need to store '09876' as a string in your database, and to do that you need to change your column type to CharField.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • if I know but I try self.Cadenapura [17] = int (self.Cadenapura [17]) / addr = int (self.Cadenapura [17]) / change = self.Cadenapura [17] addr = int change. but nothing. Help me to see that I can do to make it? – user987055 Dec 14 '11 at 14:21
0

Like I said on your newer question, you should use Django's model api to handle the SQL for you.

Carro.objects.create(ip_unidad=self.addr[0], fecha=self.Cadenapura[17])

Furthermore, you should also revise your model. Instead of using IntegerFields, you should instead use

class Carro(models.Model):
    ip_unidad = models.IPAddressField()
    fecha = models.DateField()

Then when saving your data(with the model's objects.create), you need to make sure that your date is a python datetime.date object. Using the IPAddressField also means that you don't need to bother trying to convert the IP address to an int.

Community
  • 1
  • 1
forivall
  • 9,504
  • 2
  • 33
  • 58