Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:19:06+00:00 2026-06-01T04:19:06+00:00

I have the following file dupeExtracter.rb to remove duplicate entries from a DB table

  • 0

I have the following file “dupeExtracter.rb” to remove duplicate entries from a DB table that is throwing an exception when I run it:

require 'sqlite3'

 db = SQLite3::Database.new('development.sqlite3')
 db.results_as_hash = true;

 #This query will return the lowest id of every entry that occurs more than once in the   DB
 #Entries that occur only once will NOT be in this resultset
 #In other words this query will return the lowest id of every Duplicate in the DB
 #It takes all these entries and populates the duplicates table with them
 rows = db.execute("SELECT L1.* FROM listings L1
 WHERE L1.id = 
 (SELECT MIN(L2.id) FROM Listings L2 WHERE 
 L1.name = L2.name 
 AND L1.telephone = L2.telephone
 AND L1.latitude = L2.latitude
 AND L1.longitude = L2.longitude)
 ") 


rows.each do |row|

   db.execute("DELETE FROM listings L
   WHERE L.id <> row['id']
   AND L.name = row['name']
   AND L.telephone = row['telephone']
   AND L.latitude = row['latitude']
   AND L.longitude = row['longitude']
  ")

 end

This is the exception:

/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize': near "L": syntax error (SQLite3::SQLException)
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:134:in `execute'
from dupeExtracter.rb:22:in `block in <main>'
from dupeExtracter.rb:20:in `each'
from dupeExtracter.rb:20:in `<main>'

Im not sure how to debug it or fix it. Tried a couple of ways but it seems to not work.

Please help

EDIT changed the loop above to this after one of the answers below suggested this. But Im still getting an exception:

rows.each do |row|

db.execute("DELETE FROM listings 
WHERE id <> row['id']
AND name = row['name']
AND telephone = row['telephone']
AND latitude = row['latitude']
AND longitude = row['longitude']
")

Exception:

/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize': near "['id']": syntax error (SQLite3::SQLException)
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:134:in `execute'
from dupeExtracter.rb:22:in `block in <main>'
from dupeExtracter.rb:20:in `each'
from dupeExtracter.rb:20:in `<main>'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:19:09+00:00Added an answer on June 1, 2026 at 4:19 am

    Since the exception tells you that there’s an error near the token “L”, and there’s only one instance of this token in your SQL, it appears to me that SQLite does not accept the alias on the DELETE statement. A quick check of the syntax diagram indicates that, indeed, no alias is allowed for DELETE.

    But consider doing this in one statement:

    DELETE FROM listings WHERE EXISTS
       (SELECT * FROM Listings L2 
           WHERE id      < listings.id
           AND name      = listings.name
           AND telephone = listings.telephone
           AND latitude  = listings.latitude
           AND longitude = listings.longitude)
    

    This will delete every record in listings that has a similar record in the listings table with a lower id (that is, all but the lowest id row for every cohort).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have the following file How to remove by sed all FILE NAME lines
gcc 4.4.4 c89 I have the following file that contains name, age, and gender.
I have the following file name changing log table. ChangeNameLog( Date , OldName ,
Possible Duplicate: Join lines based on pattern I have the following file: test one
I have following Html file with javascript. This gives me error that testCircle is
I have the following file input box that allows for multiple upload: <input name=filesToUpload[]
i have following php file.... mysql_connect(localhost,root,); mysql_select_db(database_name); $sql=mysql_query(select * from members); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print(json_encode($output));
I have the following file from the PARSEC opensource benchmarks, and I want to
I have the following file that acts as the access point to my DB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.