SOL4Py Sample: UpdateTable

SOL4Py Samples



#******************************************************************************
#
#  Copyright (c) 2020 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#******************************************************************************


# 2020/01/30
# See also https://stackoverflow.com/questions/5241031/python-inserting-and-retrieving-binary-data-into-mysql

# encoding: utf-8
0
# UpdateTable.py


"""
create table test.User (
  id             bigint unsigned not null primary key auto_increment,
  auth_token     varchar(40)    default NULL,          
  email          varchar(255)   unique default NULL,
  password       varchar(512)   default NULL,      #(AES IV) + (AES ENCRUPTED Password) 
  validated      bool           default False,     
  last_modified  datetime       default NOW()
);

"""


import sys
import os
import configparser

sys.path.append('../')

from SOL4Py.ZMain  import *
import base64

from SOL4Py.mysql.ZMySQLDB  import *
from SOL4Py.crypto.ZAESIVEmbeddedCipher import *
from SOL4Py.generator.ZTokenGenerator import *
from SOL4Py.generator.ZEmailAddressGenerator import *
from SOL4Py.generator.ZPasswordGenerator import *


if main(__name__):
  # user password database 
  # foo password  test
  
  db =  ZMySQLDB(argv=sys.argv)
  
  try:
    select = "SELECT id, email  FROM test.User WHERE email LIKE %s"
    values = ('%.com',)
    
    rows = db.execute_fetchall(select, values)
   
    for row in rows:
        (id, email) = row
        print("Hit record: id {} email {}".format(id, email))
        
        token_generator    = ZTokenGenerator()
        password_generator = ZPasswordGenerator()

        new_atoken   = token_generator.generate(20)
        new_password = password_generator.generate()

        key  = email
        cipher = ZAESIVEmbeddedCipher(ZCipher.AS_HEX) 
        new_hex_iv_password = cipher.encrypt(new_password, key)

        print("New Auth_token   {}".format(new_atoken))
        print("New Password     {}".format(new_password))
   
        try:
            update = "UPDATE test.User SET auth_token=%s, password =%s WHERE id =%s"
            values = (new_atoken, new_hex_iv_password, id)
            db.execute(update, values)
        except:
            traceback.print_exc()

    db.commit()
        
  except Exception as ex:
    traceback.print_exc()
    db.rollback()
      
  finally:
    db.connection.close()

  

Last modified:27 Jan. 2020