Hola, estoy teniendo unos problemas con este script haaa, estoy intentando escribir un algoritmo que actualice mi sitio web, pero tengo algunos problemas con el insert del mysql, aquí está el código:
#! python3
# Just a test with mysqldb
import sys
import mysql.connector
from datetime import date, datetime, timedelta
def main():
db = mysql.connector.connect(user='', password='',
host='localhost',
database='')
cursor = db.cursor()
now_date = datetime.now().date()
cursor.execute("INSERT INTO games (name, description, url, category_id, category_parent, width, height, image, published, filetype, instructions, date_added, advert_id, highscores, mochi_id, seo_url, submitter)\
VALUES ('Tesla', 'this is just a description', 'http://juego..swf', 2, 0, 480, 248, 'http://localhsot.jpg', 0, 'swf', 'This are the instructions', %(now_date)s, 1, 1, '', 'some-game', 0)")
db.close()
if '__main__' == __name__:
sys.exit(main())
Pero al correrlo me manda este error:
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%(now_date)s, 1, 1, '', 'some-game', 0)' at line 1
Hola.
Te hace falta indicar el valor del parámetro now_date.
Tienes varias formas.
query = "INSERT INTO games (name, description, url, category_id, category_parent, width, height, image, published, filetype, instructions, date_added, advert_id, highscores, mochi_id, seo_url, submitter)\
VALUES ('Tesla', 'this is just a description', 'http://juego..swf', 2, 0, 480, 248, 'http://localhsot.jpg', 0, 'swf', 'This are the instructions', '%(date)s', 1, 1, '', 'some-game', 0)" % {'date': now_date}
cursor.execute(query)
Otra forma
query = "INSERT INTO games (name, description, url, category_id, category_parent, width, height, image, published, filetype, instructions, date_added, advert_id, highscores, mochi_id, seo_url, submitter)\
VALUES ('Tesla', 'this is just a description', 'http://juego..swf', 2, 0, 480, 248, 'http://localhsot.jpg', 0, 'swf', 'This are the instructions', '"+ now_date.strftime('%Y-%m-%d') +"', 1, 1, '', 'some-game', 0)"
cursor.execute(query)
Otra más
query = "INSERT INTO games (name, description, url, category_id, category_parent, width, height, image, published, filetype, instructions, date_added, advert_id, highscores, mochi_id, seo_url, submitter)\
VALUES ('Tesla', 'this is just a description', 'http://juego..swf', 2, 0, 480, 248, 'http://localhsot.jpg', 0, 'swf', 'This are the instructions', '"+ str(now_date) +"', 1, 1, '', 'some-game', 0)"
cursor.execute(query)
Con la función NOW() de MySQL
query = "INSERT INTO games (name, description, url, category_id, category_parent, width, height, image, published, filetype, instructions, date_added, advert_id, highscores, mochi_id, seo_url, submitter)\
VALUES ('Tesla', 'this is just a description', 'http://juego..swf', 2, 0, 480, 248, 'http://localhsot.jpg', 0, 'swf', 'This are the instructions', NOW(), 1, 1, '', 'some-game', 0)"
cursor.execute(query)
Este último utiliza la función de mysql, así que toma la fecha del servidor en donde se encuentre el SMBD.
No olvides hacer el commit para completar la transacción
cursor.execute(query)
db.commit()
db.close()
Cita de: 0roch1 en 6 Octubre 2015, 21:41 PM
Hola.
Te hace falta indicar el valor del parámetro now_date.
Tienes varias formas.
Gracias por la respuesta, pero ya encontré el error, execute() toma dos parámetros y sólo estaba pasando uno, el código ahora está así y funciona, también agregué el commit, gracias por
#! python3
# trying to connect to mysql
import sys
import mysql.connector
import time
def main():
db = mysql.connector.connect(user='user', password='password',
host='localhost',
database='new_games')
cursor = db.cursor()
query = ("INSERT INTO games (name, date_added) VALUES (%s, %s)")
data_games = ('Cyborg SpaceNow', time.strftime("%Y-%m-%d %H:%M:%S"))
cursor.execute(query, data_games)
db.commit()
cursor.close()
db.close()
if '__main__' == __name__:
sys.exit(main())