How to insert data from a Python script in mysql?

0

I have not programmed in python for a while and I feel pretty rusty with the language and that's why I need a little help with my script: link What it does is that once executed, all the new files that arrive in a folder (in this case download) are moved to a folder newly created by the script depending on the date and time (in this case the files that arrive each 1 second will move to the folder with the name: 25-11-2017-00-20-11 This can also be modified to files that arrive at every minute, hour, day, month and last per year on line 33 ) To connect to the database the lines are:

import MySQLdb


class Database:

    host = 'localhost'
    user = 'root'
    password = ''
    db = 'watcherservice'

    def __init__(self):
        self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db)
        self.cursor = self.connection.cursor()

    def insert(self, query):
        try:
            self.cursor.execute(query)
            self.connection.commit()
        except:
            self.connection.rollback()



    def query(self, query):
        cursor = self.connection.cursor( MySQLdb.cursors.DictCursor )
        cursor.execute(query)

        return cursor.fetchall()

    def __del__(self):
        self.connection.close()


if __name__ == "__main__":

    db = Database()

The data in my table is as follows

CREATE TABLE IF NOT EXISTS 'detalles' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'fecha' date NOT NULL,
  'hora' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  'archivos_movidos' int(11) NOT NULL,
  'estado' text NOT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

My doubts are: How do I insert the number of files to a folder that were moved in the moved files column? If the files were moved successfully or if there was an error. As inserted in the column 'successful' or 'error'?

    
asked by Beta 07.12.2017 в 07:34
source

0 answers