Home > OS >  Where is the mistake in my sqlite Table insert?
Where is the mistake in my sqlite Table insert?

Time:02-03

I have a little question.. The OperationalError is that table Roomname_long has no column named Number but it has ??

def create_table_roomname_short():
    c.execute('CREATE TABLE IF NOT EXISTS Roomname_short ("Name" SHORT, "Number" NUM)')

def create_table_roomname_long():
    c.execute('CREATE TABLE IF NOT EXISTS Roomname_long ("GName" Gericht, "OName" Ort , "Number" NUM)')

def data_entry():
    short = "HH"
    num = 321
    c.execute("INSERT INTO Roomname_short (Name, Number) VALUES(?, ?)",
                                                  (short, num))
def data_entry_roomname_long():
    gericht = "Landgericht"
    ort = "Neuruppin"
    num = 4
    c.execute("INSERT INTO Roomname_long (GName, OName, Number) VALUES(?, ?, ?)",
                                                  (gericht, ort, num))
    connect.commit()

create_table_roomname_short()
create_table_roomname_long()
data_entry()
data_entry_roomname_long()

c.close()
connect.close()

CodePudding user response:

To turn the comments to an answer:

You're seeing an issue since you're using a pre-existing database file in which the Roomname_long table exists, but doesn't have the Number column.

Since CREATE TABLE IF NOT EXISTS does not create a table if it doesn't exist, or modify an existing table to match the new definition either, that's what you get.

Your options are to

  • use a new database file (though this obviously is quite terrible if you already have data in it)
  • use ALTER TABLE to modify the existing table to add a new column if one doesn't exist (which is generally called schema migration).

CodePudding user response:

My guess is that number is a reserved word, like date now etc.

Try changing the column name to Numberr and adjusting the query.

  •  Tags:  
  • Related