drop table if exists X

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

drop table if exists X

Kenneth Shuai
db_source.db with tbs1, tbs2
db_dest.db with tbd1, tbd2

conn = sqlite3.connect(db_dest)

sql = """
attach db_source.db as db_source;
drop table if exists tbs1  # it is used to check if tbs1 already in db_dest.
"""
Conn.execute(sql)
Above will delete tbs1 from db_source.db
it might be a bug cause wrong deletion of tbs1 from db_source

I also tried other cases,
conn = sqlite3.connect(db_dest)
Sql = """
Attach db_source.db as db_source;
Select * from tbs1;
"""
Conn.execute(sql)
Above code works, it might also be a bug, it should be "select * from db_source.tbs1",
"select * from tbs1" should point to "db_dest.tbs1" instead of db_source.tbs1"


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: drop table if exists X

Clemens Ladisch
Kenneth Shuai wrote:
> conn = sqlite3.connect(db_dest)
>
> sql = """
> attach db_source.db as db_source;
> drop table if exists tbs1  # it is used to check if tbs1 already in db_dest.
> """
> Conn.execute(sql)

Okay, let's try:

  >>> conn = sqlite3.connect(db_dest)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  NameError: name 'db_dest' is not defined
  >>> conn = sqlite3.connect("db_dest.db")
  >>> sql = """
  ... attach db_source.db as db_source;
  ... drop table if exists tbs1  # it is used to check if tbs1 already in db_dest.
  ... """
  >>> Conn.execute(sql)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  NameError: name 'Conn' is not defined
  >>> conn.execute(sql)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  sqlite3.OperationalError: no such column: db_source.db
  >>> sql = """
  ... attach "db_source.db" as db_source;
  ... drop table if exists tbs1  # it is used to check if tbs1 already in db_dest.
  ... """
  >>> conn.execute(sql)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  sqlite3.Warning: You can only execute one statement at a time.
  >>> sql2 = """
  ... drop table if exists tbs1  # it is used to check if tbs1 already in db_dest.
  ... """
  >>> conn.execute(sql2)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  sqlite3.OperationalError: unrecognized token: "#"
  >>> sql2 = """
  ... drop table if exists tbs1  -- it is used to check if tbs1 already in db_dest.
  ... """
  >>> conn.execute(sql2)
  >>>

> Above will delete tbs1 from db_source.db

Yes.

> it might be a bug cause wrong deletion of tbs1 from db_source

Why do you think is is wrong?

<http://www.sqlite.org/lang_attach.html> says:
| Tables in an attached database can be referred to using the syntax
| schema-name.table-name. If the name of the table is unique across all
| attached databases and the main and temp databases, then the schema-
| name prefix is not required.


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users