0

Can't find where is this typo or something - everytime getting this error line 33, in <module> c_a.execute('ATTACH DATABASE temp.db AS check') sqlite3.OperationalError: near "check": syntax error

I'm trying to attach database temp.db to the prime.db due one query. This has help me to find duplicate records (checking login (one of the columns in both databases))

import sqlite3

db_a = sqlite3.connect('prime.db')
c_a = db_a.cursor()

db_b = sqlite3.connect('temp.db')
c_b = db_b.cursor()

c_a.execute('ATTACH DATABASE temp.db AS check')
c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login')
db_a.commit()
c_a.fetchall()

Also tried another version:

attachDatabaseSQL = "ATTACH DATABASE temp.db AS check"
dbSpec = ("temp.db",)
c_a.execute(attachDatabaseSQL, dbSpec)
c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login')
db_a.commit()
c_a.fetchall()

It found the error: line 41, in <module> c_a.execute(attachDatabaseSQL, dbSpec) sqlite3.OperationalError: near "check": syntax error

Use Python 3.8.1 and SQLite3.

UPDATE: Changed it to:

c_a.execute('ATTACH DATABASE temp.db AS dbcheck')
c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login')
db_a.commit()
c_a.fetchall()

And the ERROR NOW IS:

c_a.execute('ATTACH DATABASE temp.db AS dbcheck')
sqlite3.OperationalError: no such column: temp.db
pylsokol
  • 1
  • 1

1 Answers1

0

ATTACH DATABASE temp.db AS check;

check is a reserved word in SQLite, as in most (if not all) other databases. In the syntax, it comes into play to define check constraints when creating tables.

Consider using another name, that does not conflict with a language keyword. For example:

ATTACH DATABASE temp.db AS dbcheck;
GMB
  • 188,822
  • 23
  • 52
  • 100
  • Well, tried this but, appears another error this time... `c_a.execute('ATTACH DATABASE crm.db AS dbcheck') c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login') db_a.commit() c_a.fetchall() print('Records have been deduplicated.')` error this time is: `line 33, in c_a.execute('ATTACH DATABASE temp.db AS dbcheck') sqlite3.OperationalError: no such column: temp.db` – pylsokol Apr 11 '20 at 18:07