Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Running sql statement with datetime format gives error Warning: (1292, "Incorrect date value: '2019/04/29:12:28:18' for column 'dateAdded' at row 1") #792

Closed
@kabads

Description

@kabads

SQL to recreate the database:

def insertDatabaseTable():
"""This function creates a Table that will hold the media in the database."""
try:
    with dbConnection.cursor() as cursor:
        # Create a new record
        sql = ("CREATE TABLE `media` (`id` int(11) NOT NULL AUTO_INCREMENT,\n"
               "            `name` varchar(255) COLLATE utf8_bin NOT NULL,\n"
               "            `platform` varchar(255) COLLATE utf8_bin NOT NULL,\n"
               "            `dateAdded` date NOT NULL,\n"
               "            `type` varchar(255),\n"
               "            PRIMARY KEY (`id`)\n"
               "            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin\n"
               "            AUTO_INCREMENT=1 ;")
        cursor.execute(sql)

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    dbConnection.commit()
except:
    print("Couldn't connect to database to create the table.")

Insert a new row:

def enterMusic(tpe):
    name = input('Name of the music: ')
    platform = input('Platform of the music: ')
    a = mediatypes.music.Music(name, platform, tpe, dateAdded())
    music = {a: tpe}
    media.append(music)
    enterMediaToDB(a, tpe)


def enterMediaToDB(mediaObj, tpe):
    if dbConnection:
        try:
            with dbConnection.cursor() as cursor:
                sql = 'insert into media (name, platform, dateAdded, type) values ("{}", "{}", "{}", "{}");'.format(mediaObj.name, mediaObj.platform, mediaObj.dateAdded, tpe)
                print(sql)
                cursor.execute(sql)
            dbConnection.commit()
        except:
            print("Couldn't write to database.")
        try:
            # Execute SQL
            print("Execute")
        except:
            print("Cannot write to database.")
            return

So, when I run this, I get the sql statement:
insert into media (name, platform, dateadded, type) values ("games", "steam", "2019/04/29:12:28:18", "Game");

which works in the mysql command line client, but gives the error:

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/cursors.py:329: Warning: (1292, "Incorrect date value: '2019/04/29:12:28:18' for column 'dateAdded' at row 1")
self._do_get_result()

when run from python.

There's a possibly related issue at:
https://stackoverflow.com/questions/55073124/pymysql-select-maxtimestamp-results-in-incorrect-datetime-value-0000-00-00

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions