Python Forum
Inserting a variable as column name in sqlite3 python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inserting a variable as column name in sqlite3 python
#1
So how exactly do you do it? I have tried doing this -
'UPDATE Table SET {} WHERE name=?'.format(col) , [name])
and this -
'UPDATE Table SET %s WHERE name=?' % (col,), [name]
Both have been giving me the error of
Error:
OperationalError: near "WHERE": syntax error
. Is this even possible?
Reply
#2
use the verb ALTER
It will add the new column as last column (required to maintain integrity of old table.
see: https://www.sqlitetutorial.net/sqlite-alter-table/
Reply
#3
(Oct-22-2020, 11:17 AM)Larz60+ Wrote: use the verb ALTER
It will add the new column as last column (required to maintain integrity of old table.
see: https://www.sqlitetutorial.net/sqlite-alter-table/

Alter creates a new column, I would like to use a variable to search and update a pre-existing column.
Reply
#4
After SET you should assign a value to a column. The syntax is:
Update <table_name>
Set <Column_name> = <value>
where <column_name = <another_value>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inserting data from python list into a pandas dataframe mahmoud899 0 2,601 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  How to modify and save a column in the sqlite3 database using python? laithsky1 3 2,798 Dec-18-2018, 03:39 AM
Last Post: micseydel
  Bar Plot with Python ,matplotlib and sqlite3 tables gauravbhardwajee 0 4,954 Sep-25-2018, 06:17 PM
Last Post: gauravbhardwajee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020