chespinoza
chespinoza

Reputation: 2668

Python-Mysql, better lib or better approach?

I'm using mysql.connector and for some tasks, for example insert data in a local mysql database the process is taken 0.0436846 s. average, there are some way to improve it?

import mysql.connector
from mysql.connector import errorcode
    class Dal:
        @profile
        def __init__(self, dic):
            try:
                self.cnx = mysql.connector.connect(user=dic['user'],
                                    password=dic['password'],
                                    host=dic['host'],
                                    database=dic['database'])
            self.cursor = self.cnx.cursor()

            except mysql.connector.Error as err:
                if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                    print("User/Pass Fails")
                elif err.errno == errorcode.ER_BAD_DB_ERROR:
                    print("BD no exist")
                else:
                    print(err)


        def insert_event(self,dic):
            ins_ev = ("INSERT INTO events "
                        "(device, type, index, datetime, c1,\
                         c2, vel, head, pfm, age,vod,created_on ) "
                        "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
                        )
            self.cursor.execute(ins_ev, dic)
            self.cnx.commit()

Maybe another approach?

Upvotes: 1

Views: 83

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799560

mysql.connector is a pure Python module. Either of MySQL-python or oursql, which link against the C library, should give you better performance for at least the communications part. Other optimizations include disabling indexes and performing lazy inserts.

Upvotes: 2

Related Questions