Python and pymysql connection

Asked

Viewed 565 times

0

I’m creating a login system and user records. I created a model to do the insertion and controller, validating the information and step for the model to save, however, my model is not working.

Here is the connection to the bank

def conexao():
    import pymysql.cursors

    config = {
        'user': 'root',
        'password': 'admin',
        'host': '127.0.0.1',
        'database': 'megasena'
    }

    try:
        conection = pymysql.connect(**config)
        return conection
    except pymysql.InternalError as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print('Dados de acessso a Database invalidos!')
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print('Database nao existe!')
        else:
            print(err)

On the model, I import the connection

import sys
sys.path.append('/home/felipe/Documents/Projetos/MegaSena/conection')
from conn import conexao

class User():

    def insere(self):
        t = conexao()
        cursor = t.cursor()
        cursor.execute("""INSERT INTO usuarios (nome, email, senha, username) VALUES(
                       "Felipe Paz",
                       "[email protected]",
                       "admin",
                       "felipepaz"
        )""")

In this case, I’m throwing the information straight into the model to test since this information will come from the controller. But even so, I can’t save this information in the comic book. Would someone tell me where the mistake is?

  • What is the error message? Why does it give import in pymysql.cursors instead of just pymysql?

  • Not from the error message. I put a print on Try to see if it connects and Try runs. I put Cursors pq I saw in a tutorial, as I said earlier, I’m novice in python.

  • Character type data must be between single and double quotes.

  • @Pagotti: The language does not distinguish between single and double quotes.

  • @jsbueno You refer to Python or SQL?

  • in Python. - hadn’t even seen his sql command. It won’t run because it imports the wrong module, as Andersn pointed out. In any case,.

Show 1 more comment

1 answer

0

Give a t.commit() after the cursor.execute()

import sys
sys.path.append('/home/felipe/Documents/Projetos/MegaSena/conection')
from conn import conexao

class User():

    def insere(self):
        t = conexao()
        cursor = t.cursor()
        cursor.execute("""INSERT INTO usuarios (nome, email, senha, username) VALUES(
                       "Felipe Paz",
                       "[email protected]",
                       "admin",
                       "felipepaz"
        )""")
        t.commit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.