0

I want the insertion query do nothing if it's nothing new in csv file , In Case it is , i want to insert only this one and not again all the csv, any suggestion would be great!

PS: it's not duplicate with other questions because here we have "%s" no stable values and in python it's different the syntax!

    cursorobject=connection.cursor()
    sql2="CREATE DATABASE IF NOT EXISTS mydb"
    cursorobject.execute(sql2)
    sql1="CREATE TABLE IF NOT EXISTS users(id int(11) NOT NULL AUTO_INCREMENT,first_name varchar(255),last_name varchar(255),company_name varchar(255),address varchar(255),city varchar(255),country varchar(255),postal varchar(255),phone1 varchar(255),phone2 varchar(255),email varchar(255),web varchar(255),PRIMARY KEY(id))"
    cursorobject.execute(sql1)
    csvfile=open('Entries.csv','r')
    reader = csv.reader(csvfile,delimiter=',')

    for row in reader:
      cursorobject.execute("INSERT INTO users(first_name,last_name,company_name,address,city,country,postal,phone1,phone2,email,web) VALUES (%s ,%s, %s,%s,%s,%s,%s,%s,%s,%s,%s)",row)
laland
  • 61
  • 7
  • Possible duplicate of [How to 'insert if not exists' in MySQL?](https://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql) – Raymond Nijland Jul 18 '18 at 11:05
  • @RaymondNijland nop , here we have some values that is inserted from csv – laland Jul 18 '18 at 11:07
  • "in python it's different the syntax!" The duplication link shows you what you need to do in MySQL.. In Python SQL syntax isn't different might appear so because of the prepared statements... "here we have some values that is inserted from csv " So the CSV values still needs to bounds within the prepared statements you are making it harder on yourself by disbelieving.. Besides @lucumt appears that he has given a valid answer based on the duplication link i have given. – Raymond Nijland Jul 18 '18 at 11:35

2 Answers2

0

before entering the record in the database, make a select query on users table may be with first name and last name

Amir Gohar
  • 58
  • 6
0

You can use INSERT INTO SELECT to do it

INSERT INTO users
(first_name,last_name,company_name,address,city,country,postal,phone1,phone2,email,web)
SELECT %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s FROM users WHERE NOT EXISTS
(SELECT * FROM users WHERE first_name=%s AND last_name=%s)

Note:As you have said %s is not a static value,however if you execute your sql,%s will convert to actual data according to the parameter your passed via %s

lucumt
  • 6,642
  • 2
  • 16
  • 32
  • It drops me error " not enough arguments for format string" i think the two extra "%s" "confuse" the program because row has 11 %s , no 13 – laland Jul 18 '18 at 11:24
  • 1
    @laland you need to pass enough parameter to it,based on your question,the parameter count should be 13,which means you need to pass `first_name` and `last_name` twice – lucumt Jul 18 '18 at 11:26
  • can you be more specific please , i know that we have 11 %s and you use 13 how we can balance them? – laland Jul 18 '18 at 11:28
  • @laland `for row in reader:` variable row is a list of strings now.. you need to do something like `row.append(row['first_name']; row.append(row['last_name'];` or `row.append(row[0]; row.append(row[1];` to add new list items. – Raymond Nijland Jul 18 '18 at 11:44
  • @RaymondNijland it doesn't work can you make it more specific? – laland Jul 18 '18 at 13:59