0

I`m using SQLite version 3.3.6 from python 2.7.2.

While executing this query with executescript() from python script:

attach database 'Capacity.db' as WeekAgo;
drop table if exists maximums;
create table maximums (Element TEXT UNIQUE, Score NUMBER , Report TEXT);
insert into maximums (Element, Score, Report) select * from WeekAgo.maximums;

I always ending up with deletion of WeekAgo.maximums table in attached database and script crashing that no WeekAgo.maximums exists anymore. What am I doing wrong? Without DROP IF EXISTS, everything works fine.

2
  • I`m amused why during DROP it drops WeekAgo.maximums instead of maximums ??? It can at least say that maximums not found , to force me use DB name as prefix, to strictly identify tables. Actually exactly what you suggested below. I expect some error to point me that i did not identified table properly. Commented Jul 24, 2012 at 12:44
  • "The optional IF EXISTS clause suppresses the error that would normally result if the table does not exist." sqlite.org/lang_droptable.html Commented Jul 24, 2012 at 12:46

1 Answer 1

5

Use the main keyword:

attach database 'Capacity.db' as WeekAgo;
drop table if exists main.maximums;
create table main.maximums (Element TEXT UNIQUE, Score NUMBER , Report TEXT);
insert into main.maximums (Element, Score, Report) select * from WeekAgo.maximums;

See https://www.sqlite.org/lang_attach.html

If two or more tables in different databases have the same name and the database-name prefix is not used on a table reference, then the table chosen is the one in the database that was least recently attached.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, for your answer. It is really unexpected behaviour. Logically , without DB prefix, it should use DB that was opened first , like main workplace.
Logicially it should behave as documented :)
I've sent a link of this post to the relevant reporting system - will post an update if it gets a ticket number or something.
Not necessarily an error; just not the behaviour one is led to expect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.