I'm trying to update the records in a column within database. My current codes are as below:
curs = conn.cursor()
statement='SELECT column FROM table'
curs.execute(statement)
curs.execute("INSERT INTO table VALUES (4, 'Five')")
In my understanding, the 4th row of the column should be updated to 'Five'. After I ran it, there is no error, but no update neither. There must be something wrong in my codes, or I'm missing something important. What if I want to update the whole column's records? Thanks in advance for any clarification.
update `table_name` set `column_name` ='value'
although you want to make VERY certain you are trying to update ALL of the rows to that new column values, otherwise you need to add
where `unique_key` ='unique_value'
Depending on the version of SQL you're using MSSQL, Oracle, MySQL you'll need different syntax. Looks like you're using MSSQL so you'll want to start with this:
UPDATE table SET column = "Five"
But you can't just say set row 4 to X value, SQL doesn't keep a set row number like an Excel spreadsheet. You'll want to add an int column and call it something like PK_tablename and set it to be the primary key for that table. Then you can write a statement like this and it will always update the correct row:
UPDATE table SET column = "Five" WHERE PK_tablename = 4
I would suggest reading up on Primary Keys in your SQL help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With