Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'syntax': 0.04; 'insert': 0.05; 'table.': 0.07; 'parameter': 0.09; 'parsing': 0.09; 'subject:into': 0.09; 'python': 0.11; '(either': 0.16; '36,': 0.16; 'corresponds': 0.16; 'csv': 0.16; 'data)': 0.16; 'placeholder': 0.16; 'all,': 0.19; 'bit': 0.19; 'value.': 0.19; '>>>': 0.22; 'import': 0.22; 'manual': 0.22; 'error': 0.23; '"you': 0.24; 'script': 0.25; 'holds': 0.26; 'query': 0.26; 'tried': 0.27; 'function': 0.29; 'raise': 0.29; 'field,': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'file': 0.32; 'skip:m 30': 0.32; '(most': 0.33; 'actual': 0.34; "i'd": 0.34; 'received:google.com': 0.35; 'version': 0.36; 'data,': 0.36; 'i.e.': 0.36; 'skip:" 50': 0.36; 'possible': 0.36; 'hi,': 0.36; 'should': 0.36; 'wrong': 0.37; 'server': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'skip:_ 30': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'you.': 0.62; 'more': 0.64; 'fails,': 0.84; 'received:mail- ob0-x22d.google.com': 0.84; '2013,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=eEmKGb/FYhUyIz29rgkNaZ9Z5iMvvkg9ErBSCNKzvaA=; b=DBAbvPTRORPjVr901LXV5qfcMft91YcHsUDQPMP3tUSkxc0So3dEGYnC080l8oynAb wbq5djk7ixwQCKitKh3OCVy25BOYwpzJLdEj7W/gs5aovIJEmzb4/YaKiZbfEluTP8oO k6Osnr2nsLJpYgNAI5/zs1RLX+pBYKSY9U0nzp2sxur3zr7yuY2qe+ngftAMiOIUv/Gk ag9blQaUau8tezdwULx8K3qkUnd/M/KiHkFXhUsamB2A0lwQ25xYC8ANYMhNvbapeDsu AqpDnMxTekLKdBYJgBa5h4Ohm6ou+tTczumnZYlsKKNwA+wAPhau6H9y78l+CH5FgH8v AAtA== MIME-Version: 1.0 X-Received: by 10.182.166.40 with SMTP id zd8mr23855149obb.25.1387931600807; Tue, 24 Dec 2013 16:33:20 -0800 (PST) Date: Tue, 24 Dec 2013 16:33:20 -0800 Subject: Insert NULL into mySQL datetime From: Igor Korot To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387931604 news.xs4all.nl 2903 [2001:888:2000:d::a6]:37253 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62692 Hi, ALL, I am working on a script that parses CSV file and after successful parsing insert data ino mySQL table. One of the fields in CSV holds a date+time value. What the script should do is check if the cell has any data, i.e. not empty and then make sure that the data is date+time. If the validation fails, it should insert NULL into the mySQL datetime field, otherwise the actual datetime will be inserted. Right now the script uses a placeholder "0000-00-00 00:00:00.000" if the date validation fails (either cell is empty or it has wrong data) What I tried so far is: C:\Documents and Settings\Igor.FORDANWORK\My Documents\GitHub\image_export\Artef acts>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb as mdb >>> conn = mdb.connect('127.0.0.1','root','pass') >>> cur = conn.cursor() >>> a = None >>> cur.execute("Use mydb") 0L >>> cur.execute("Insert Into mytable(datefield) VALUES(STR_TO_DATE(%s))", a) Traceback (most recent call last): File "", line 1, in File "c:\python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute self.errorhandler(self, exc, value) File "c:\python27\lib\site-packages\MySQLdb\connections.py", line 36, 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 '%s))' at line 1") >>> a = "" >>> cur.execute("Insert Into mytable(datefield) VALUES(STR_TO_DATE(%s))", a) Traceback (most recent call last): File "", line 1, in File "c:\python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute self.errorhandler(self, exc, value) File "c:\python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1582, "Incorrect parameter count in the call to native function 'STR_TO_DATE'") >>> Is it possible to do what I want? I'd like to use one query to insert the data into the table. Thank you.