Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update column records in database?

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.

like image 426
widget Avatar asked Jan 20 '23 06:01

widget


2 Answers

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'
like image 151
FatherStorm Avatar answered Jan 31 '23 03:01

FatherStorm


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.

like image 39
Matt Lind Avatar answered Jan 31 '23 02:01

Matt Lind