Making changes to a SQLite table by comparing it with another table to find matches

I need to perform an update on a table named stock where the product_id matches the product_id of another table called stock_in_temp, and also ensure that a column named crm_product_id is either null or ''. This task needs to be done using python sqlite3.

Here is the code snippet I attempted:

conn.execute("UPDATE stock SET stock.quantity += stock_in_temp.quantity FROM stock, stock_in_temp WHERE stock.product_id = stock_in_temp.product_id AND stock_in_temp.CRM_product_id !='' ")
sqlite3.OperationalError: near ".": syntax error

Answer №1

There appears to be an error in your syntax.

When updating a column to the left of the = operator, it should not include the table's name.
Additionally, do not reference the stock table again after the FROM clause, as this is a logical error rather than a syntactical one. The += operator may be valid in some programming languages but not in SQLite.

Consider revising to:

UPDATE stock AS s
SET quantity = s.quantity + t.quantity 
FROM stock_in_temp AS t 
WHERE s.product_id = t.product_id AND t.CRM_product_id <> ''; 

If the quantity field in the stock table is nullable, the SET statement should be adjusted as follows:

SET quantity = COALESCE(s.quantity, 0) + t.quantity

I have used aliases instead of full table names for brevity in the code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Iterating over a dataframe to generate additional columns while preventing fragmentation alerts

Currently, I have an extended version of this coding to iterate through a large dataset and generate new columns: categories = ['All Industries, All firms', 'All Industries, Large firms'] for category in categories: sa[ ...

"Converting a text file into a dictionary using Python: A step-by-step

Looking to create a dictionary from lines of various lengths containing player stats. The format of the text file is as displayed below in order to generate a dictionary with each player's statistics. {Lebron James:(25,7,1),(34,5,6), Stephen Curry: ( ...

Achieve visibility on the latest window using Selenium WebDriver in Python

After successfully clicking on a button that triggers the opening of a new browser window with Selenium Webdriver in Python, I'm faced with the challenge of shifting the focus to this newly opened window. Despite thorough internet searches, the lack o ...

Python: Receiving None as output when attempting to print

Why is the following code printing None? Could it be indicating that the page is not being located? import BeautifulSoup as soup import RoboBrowser br = RoboBrowser() login_url = 'https://www.cbssports.com/login' login_page = br.open(login_url ...

Creating a visual grid using a combination of x's and blank spaces arranged in rows according to user input

As part of my computer programming homework (for Year 8 in the UK), I need to create a program based on the following criteria: Develop a function that takes two numbers as parameters. The first number determines the amount of spaces to be displayed, while ...

Scrapy with integrated Selenium is experiencing difficulties

I currently have a scrapy Crawlspider set up to parse links and retrieve html content successfully. However, I encountered an issue when trying to scrape javascript pages, so I decided to use Selenium to access the 'hidden' content. The problem a ...

The on_message function will check if a message is received, only within the on_bot command

Brand new to posting and looking for guidance on upgrading my Discord Bot! Currently, the Bot can successfully execute the !decklist command. However, when I encounter a situation where there are no message attachments and attempt to run the !boh command, ...

Developing forms using the method="get" attribute in Django

Within my form, there are two text input types. The data from both inputs is then sent to the view myfunc, which in turn passes it along to another template. The desired URL for the form results should be: /app/1/new_page/?key=key&value=value However ...

Having trouble accessing a website using Selenium-wire after compiling with Pyinstaller

I am trying to access a website using proxies with authentication and a specific user-agent. Here is the code snippet I am using: def start_driver(proxy_data, user_agent): proxy = ( proxy_data.get('login') + ':' + proxy_ ...

How to extract information from a shared notebook on Evernote using Python

Trying to retrieve data from a shared Evernote notebook, such as this one: Initially attempted using Beautiful Soup: url = 'https://www.evernote.com/pub/missrspink/evernoteexamples#st=p&n=56b67555-158e-4d10-96e2-3b2c57ee372c' r = requests.g ...

Exporting the parameters of a PyTorch .pth model to a .txt or .json file

Looking for a way to save the weights of a PyTorch model into a .txt or .json file? One method is to write it to a .txt file using the following code: #import torch model = torch.load("model_path") string = str(model) with open('some_file.tx ...

Clicking the "OK" Button in an Alert Box with Python and Selenium: A Step-by-Step Guide

My goal is to interact with the "OK" button within a pop-up dialog https://i.stack.imgur.com/QFoLu.jpg I attempted the following code: driver.switchTo().alert().accept(); Unfortunately, this approach was unsuccessful ...

What is the best way to avoid the "/" character when using send_keys?

When running a script that writes code into a textarea on a website, I encountered an issue. The first four lines were being written correctly until var url = "https:, at which point the cursor would jump to the upper left of the text area before continuin ...

Issues with deleting dictionaries from a list using Python

I have encountered a puzzling issue in my code. There are certain pieces of logic where keys/values or dictionaries get removed under specific conditions. However, when these objects are added to a top-level dictionary and converted into a JSON object, the ...

Tips on parsing JSON data from a URL in Python to generate a helpful dictionary

The data that I am working with can be accessed here - JSON Information Currently, this is the code I am using to read the data. However, the output appears to be unfamiliar, and I am struggling to figure out how to utilize it: import requests site=&apo ...

Can anyone provide guidance on forecasting the upcoming X values in my model?

I'm currently working with a program designed to forecast reservoir outflow, but I'm facing an issue in predicting the next day's worth of data. The data is recorded in 30-minute intervals, so ideally I would need 48 data points to cover the ...

Discovering a deeply nested div or class using Beautiful Soup

Recently, I came across this URL: All I want to do is extract the zestimate and include it in a list. The specific class where it's located is: class="Text-c11n-8-65-2__sc-aiai24-0 eUxMDw". I attempted to target it at a higher level in th ...

Modify the CancelButton text on QInputDialog

I am trying to modify the text of the Cancel button in a QInputDialog used for password input. Despite setting the new text successfully, it does not display when using getText. Could anyone point out what I might be doing wrong? Thank you in advance. c ...

The art of Python, embracing the pythonic way

Anyone know of a pythonic way to write this code snippet? # Assuming linux.cmd is a dictionary cmd = linux.cmd.get(k, k) if not cmd: cmd = k The value stored in linux.cmd.get[k] might be False. ...

Transferring cookie data between requests in CrawlSpider

My current project involves scraping a bridge website to gather data from recent tournaments. I have previously asked for help on this issue here. Thanks to assistance from @alecxe, the scraper now successfully logs in while rendering JavaScript with Phant ...