How to update column records in a database?

I am trying to update records in a column in a database. My current codes are:

curs = conn.cursor() statement='SELECT column FROM table' curs.execute(statement) curs.execute("INSERT INTO table VALUES (4, 'Five')") 

In my understanding, the fourth row of the column should be updated to "Five". After I launched it, there is no error, but no update. There should be something wrong in my codes, or I am missing something important. What if I want to update all column entries? Thanks in advance for any clarification.

+4
source share
5 answers

I solved my problem using the following codes:

 conn = psycopg2.connect(conn_string) curs = conn.cursor() statement='UPDATE table SET column = false' curs.execute(statement) conn.commit() conn.close() 

Tks helps you all

+1
source
 update `table_name` set `column_name` ='value' 

although you want to make it VERY sure that you are trying to update ALL rows to these new column values, otherwise you need to add

 where `unique_key` ='unique_value' 
+3
source

An insert does not update the values ​​in the database; it adds records. You need to use the UPDATE .

+2
source

Depending on the version of SQL you use MSSQL, Oracle, MySQL, you will need a different syntax. It looks like you are using MSSQL to start with this:

 UPDATE table SET column = "Five" 

But you cannot just specify the value of row 4 through X, SQL does not save the rowset number like an Excel spreadsheet. You want to add an int column and call it something like PK_tablename and set it as the primary key for this table. Then you can write an instruction like this, and it will always update the correct line:

 UPDATE table SET column = "Five" WHERE PK_tablename = 4 

I would suggest reading the primary keys in your SQL help.

+2
source

You are using the INSERT , not the UPDATE . As others have stated, this will insert the record, not update it.

There are 4 basic operations for simple SQL queries:

  • SELECT: used to view data. JOIN , set the WHERE , etc. to control the presentation of the data. This is the safest operation (although a bad SELECT can bring server performance to the knees).
  • INSERT: Insert a row into a table. This is pretty safe as it does not modify any existing data.
  • UPDATE: this will update one or more rows in the table. The main thing to worry about is the WHERE . If you do not enable it, you will update each row in the table. Usually you want to test the WHERE in the SELECT to make sure that you only update the rows you want to update.
  • REMOVAL: This is naturally the most dangerous. Again, without a WHERE it will delete all rows in the table. Check your WHERE to make sure that you only delete the rows you want to delete.

From the look of your INSERT it seems like you're trying to update a row based on a key (number 4). Based on the absence of error, this is not really the key. If it were a table key, it would return an error stating that you cannot insert a row with a duplicate key.

It seems (based on limited information in the question, of course) that you want it to be something like:

 UPDATE table SET column = 'five' WHERE id = 4 

More information can be found here , among many other places.

+1
source

Source: https://habr.com/ru/post/1343230/


All Articles