-4

I have information about a car:

brand="bmw"
model = "h256"
miles = "22.35 mils"
year = 2017
price= "$22.35"

I want to save this information in mysql database. So if this information does not exist in the database, it is stored.

But I have a problem when inserting. I know that not exists should be used. But I do not know how to use not exists. Help me please.

The code I have in mind:

c.execute('INSERT INTO mydata VALUES("%s","%s","%s","%i","%s") IF NOT EXISTS (SELECT * FROM mydata)'%(brand,model,miles,year,price))

At the top, I wrote the code I wanted, but that code is wrong.

I have searched for this many times. But I did not find a clear answer. Help me please . for solving this problem

Thanks a lot

2
  • Don't store 'mils' or '$' Commented Jan 15, 2021 at 17:15
  • And I don't really understand what problem it is that you think you need to solve. See meta.stackoverflow.com/questions/333952/… Commented Jan 15, 2021 at 17:17

1 Answer 1

0

A good search term to use here is:

mysql insert if not exists

This will give several solutions. Here's a good example.

One of my favorite solutions based on the link is something like the following:

INSERT INTO mydata ([HEADER NAMES])
SELECT '%s','%s','%s','%i','%s'
WHERE NOT EXISTS (select * from mydata where [primary key] = '%s')
Sign up to request clarification or add additional context in comments.

2 Comments

Which means the question is a duplicate and should be closed as such, not answered again and again.
@Shadow No, the link I provided is off-site. If I was aware of a duplicate, I definitely would have VTC it as one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.