Matheus
Matheus

Reputation: 21

Error when sending information to the database

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

Upvotes: 1

Views: 96

Answers (1)

Vineeth Sai
Vineeth Sai

Reputation: 3447

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

Upvotes: 1

Related Questions