1

I'm doing a code where the program ask for personal informations of someone, then he reads those infomations and send to the database. So I have the option to register and to consult. I don't know if the program is with more problems because I need to fix it first.

When I try to register the person It's give me the error "Is not possible to register." and I can't find why.

import sqlite3
conn = sqlite3.connect('database.db')
c = conn.cursor()

def criardb():
    c.execute('CREATE TABLE IF NOT EXISTS pessoas(id INTEGER PRIMARY KEY 
AUTOINCREMENT,nome VARCHAR, idade INT, tel VARCHAR, pais VARCHAR)')
    conn.commit()
def insertdata(nome,idade,tel,pais):
    c.execute = ('INSERT INTO pessoas VALUES (?,?,?,?)', 
(nome,idade,tel,pais))
    conn.commit()
def consultdata():
    sql = c.execute('SELECT * FROM pessoas')
    for row in sql:
        print("Nome: {}".format(row[0]))
def consultdataind(esc):
    sql = c.execute('SELECT * FROM pessoas WHERE id = ?')
    for row in sql(sql,(esc)):
        print("Nome: {} Idade: {} Telefone: {} País: 
{}".format(row[0],int(row[1]),row[2],row[3]))

try:
    print("Creating database...")
    criardb()

except:
    print("ERRO: It was not possible to create the database.")
else:
    print("Database successfully created.")

while True:
    print("Welcome to the register system, choose a option:")
    op = int(input("| 1 - Register | 2 - Consult | 3 - Exit | "))

    if op == 1:
        n = input("Nome: ")
        i = int(input("Idade: "))
        t = input("Telefone: ")
        p = input("País: ")

        try:
            insertdata(n,i,t,p)

        except:
            print("ERRO: It's not possible to register")
        else:
            print("Successfully registered.")
    elif op == 2:
        print("List of registered users:")
        try:
            consultdata()
        except:
            print("It was not possible to load the list.")

        print("Write the person ID to individual consult.")
        esc = int(input(">> "))
        consultdataind(esc)

    elif op == 3:
        break
    else:
        print("Invalid command.")

I need to send all the information to the database and return the consult, first it will show all the registered person then the user can write the respective ID of someone in the list and it will show all the details about this person

Matheus
  • 21
  • 3
  • 2
    using `except` directly is a very bad idea. It hides the real error in your code, I suggest you remove `try-except` or handle it properly. Then paste the `Traceback` to the question. – Vineeth Sai Jan 15 '19 at 14:58
  • Other than that the error seems obvious. Instead of calling the `.execute()` method on the connection (like you also do in all the other functions), you assign a tuple to it. Is this the actual code throwing errors? – Fynn Becker Jan 15 '19 at 15:01
  • 1
    You are playing Pokemon with your exceptions here, you are *trying to catch them all*. Don't hide your errors, remove the blanket `except:` and only handle exceptions that should can be expected to occur but are not an issue with your program. – Martijn Pieters Jan 15 '19 at 15:56
  • I'm gonna steal your way of telling it lol @MartijnPieters – Vineeth Sai Jan 15 '19 at 16:02
  • 1
    Yes. It's the code that was throwing error, but after the answer of @VineethSai I fixed the error, other problems happened but I fixed by myself and now the program is working exactly as i want. Thakyou very much for the help. The error was so simple lol. I will stop using the except without necessity. Thanks – Matheus Jan 15 '19 at 16:04
  • 1
    @VineethSai: [*pokemon exception handling*](https://stackoverflow.com/a/416624/100297) is not a term I invented, so go right ahead. – Martijn Pieters Jan 15 '19 at 16:08

1 Answers1

1

Replace your insertdata with this and everything should work fine.

def insertdata(nome,idade,tel,pais):
    c.execute('INSERT INTO pessoas (nome,idade,tel,pais) VALUES (?,?,?,?)', [nome,idade,tel,pais])
    conn.commit()

You need to call the execute method of the cursor here.

And on the side note, never use except directly without specifying an exception. It hides the simplest of error messages which will make your code very hard to debug. As of now it was AttributeError that was causing this problem as you were trying to assign value to Cursor which is ReadOnly

Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34