[Python-es] Problema para crear tabla idéntica a la que crea Django usando python.

Diego Uribe Gamez diego.uribe.gamez en gmail.com
Mie Mar 21 23:11:41 CET 2012


El problema que tengo es que no logro crear la tabla de la base de datos
basado en el mismo modelo que usa django para hacer las suyas, esto se va a
usar de manera inteligente para crear un registro automático usando un
demonio en python que estará pendiente a la entrada de los datos en el
servidor:
----------------------------------------------------------------------------
Código empleado: "sqlinteligent.py"
----------------------------------------------------------------------------
    creartabla = ('''create table id_%(table_name)s stocks (id integer
AUTO_INCREMENT NOT NULL PRIMARY KEY,id_unidad bigint NOT NULL,
    ip_unidad bigint NOT NULL, fecha numeric(7, 1) NOT NULL)''' %
dict(table_name = table_name))
    # Guardar los eventos
    print creartabla
    cursor.execute(creartabla)
----------------------------------------------------------------------------
Modelo de Django:
----------------------------------------------------------------------------
class Evento(models.Model):
    id_unidad = models.BigIntegerField(max_length=15)
    ip_unidad = models.BigIntegerField(max_length=11)
    fecha = models.DecimalField(max_digits=7,decimal_places=1)

----------------------------------------------------------------------------
python manage.py sql gps
----------------------------------------------------------------------------
root en servidor:/home/lmgs/plataforma# python manage.py sql gps
BEGIN;
CREATE TABLE `gprs_evento` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `id_unidad` bigint NOT NULL,
    `fecha_satelite` numeric(7, 1) NOT NULL
)
----------------------------------------------------------------------------
Error en la salida de la terminal:
----------------------------------------------------------------------------
create table id_481031 stocks (id integer AUTO_INCREMENT NOT NULL PRIMARY
KEY,id_unidad bigint NOT NULL,
    ip_unidad bigint NOT NULL, fecha numeric(7, 1) NOT NULL)

Traceback (most recent call last):
  File "sqlinteligent.py", line 42, in <module>
    cursor.execute(creartabla)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in
execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in
defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near 'stocks (id integer AUTO_INCREMENT NOT NULL
PRIMARY KEY,id_unidad bigint NOT NULL' at line 1")

-- 
 *Diego Alonso Uribe Gamez*
------------------------------

*Desarrollador web*

Twitter: @DiegoUG <http://www.twitter.com/DiegoUG>

Google+: http://gplus.to/diegoug
------------------------------
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://mail.python.org/pipermail/python-es/attachments/20120321/df0a4365/attachment.html>


Más información sobre la lista de distribución Python-es