2

I have 4 columns hotel_name, is_pool ,is_wifi ,is_gym.First time when i start the loop 10 hotels will added in hotel_name column and 1 is added to is_pool column. Second time when the loop starts for wifi it will check if the hotels already exist in hotel_name column then it will add 1 in is_spa(infront of that hotel which already exist like avari-hotel in below example) ,if these hotels not exist then it will add new hotel in hotel_name column and add 1 in is_wifi column same for is_gym etc...

 Hotel_name Is_pool Is_wifi Is_gym
Grand_hayat     1   0         0
Royal-marria    1   0         0         
Peart-continent 1   0         0
Sub-hotelways   1   0         0
Grand_marqs     1   0         0
Avari hotels    1   1         0
Chenone hotels  1   0         0
Savoey hotels   1   0         0
The grand       1   0         0
Hotel-range     1   0         0
Sub-marry       0   1         0
Royal-reside    0   1         0
Xyz             0   1         0
Abc             0   1         0
.               .   .         .
.               .   .         .

how i achieve this task kindly help :) thanks in advance

 CREATE TABLE "Hotels" (
`hotel_id`  INTEGER PRIMARY KEY AUTOINCREMENT,
`hotel_name`    TEXT NOT NULL,
`is_pool`   INTEGER DEFAULT 0,
`is_wifi`   INTEGER DEFAULT 0,
`is_gym`    INTEGER DEFAULT 0,

)

 if(prefrence[i]=='pool'):
     c.execute("INSERT INTO hotels (Hotel_name,is_pool) VALUES (?,?)" ,  [hotel], 1)
Techgeeks1
  • 556
  • 1
  • 4
  • 18
  • Similar question has been already answered, Ref: http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace – Murtuza Z Mar 23 '17 at 07:18
  • Possible duplicate of [SQLite - UPSERT \*not\* INSERT or REPLACE](http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace) – Noctis Skytower Mar 31 '17 at 14:19

1 Answers1

1

Just try to update the row. If the row was not found, insert it:

c.execute("UPDATE hotels SET is_wifi = 1 WHERE hotel_name = ?", [hotel])
if c.rowcount == 0:
    c.execute("INSERT INTO hotels..."...)
CL.
  • 173,858
  • 17
  • 217
  • 259