SQLite "Drop table" error message: "SQL logic error or missing database" - sqlite

I am running a SQLite database in memory and I am attempting to drop a table with the following command.
DROP TABLE 'testing' ;
But when I execute the SQL statement, I get this error
SQL logic error or missing database
Before I run the "Drop Table" query I check to make sure that the table exists in the database with this query. So I am pretty sure that the table exists and I have a connection to the database.
SELECT count(*) FROM sqlite_master WHERE type='table' and name='testing';
This database is loaded in to memory from a file database and after I attempt to drop this table the database is saved from memory to the file system. I can then use a third party SQLite utility to view the SQLite file and check to see if the "testing" exists, it does. Using the same 3rd party SQLite utility I am able to run the "Drop TABLE" SQL statement with out error.
I am able to create/update tables without any problems.
My questions:
Is there a difference between a memory database and a file database in SQLite when dropping a table?
Is there a way to disable the ability to drop a table in SQLite that I may have accentually turned on somehow?
Edit: It appears to have something to do with a locked table. Still investigating.

You should not have quotes in your DROP TABLE command. Use this instead:
DROP TABLE testing

I had the same problem when using Sqlite with the xerial jbdc driver in the version 3.7.2. and JRE7
I first listed all the tables with the select command as follows:
SELECT name FROM sqlite_master WHERE type='table'
And then tried to delete a table like this:
DROP TABLE IF EXISTS TableName
I was working on a database stored on the file system and so it seems not to effect the outcome.
I used the IF EXISTS command to avoid listing all the table from the master table first, but I needed the complete table list anyway.
For me the solution was simply to change the order of the SELECT and DROP.

Related

SQLite3 is not creating a database, only giving an error. Why?

I'm trying to create a database with SQLite3 but each time I run sqlite3 test.db it just takes me onto a new line and no database is created. I assumed I had to add a table to the database as well so I created one and once done it just says Error: near "sqlite3": syntax error. So creating a table to complete the database creation was not the issue. I have searched all over the web and each resource is telling me to do the same thing I have been doing that only results in an error. If this is the same syntax all other online resources are using successfully, why is it constantly failing to create a database for me?
Here is a screenshot of the exact attempt:
You seem to have the order/placement of commands mixed up. From the folder containing SQLite on your system, you should create a database via sqlite3 somedb.db:
C:\path\to\sqlite>sqlite3 test.db;
sqlite> <-- prompt lets you know that you have connected
Once you have connected to that database, you don't need to specify it again for operations intended against that database. So, I expect the following CREATE TABLE statement to work:
sqlite> CREATE TABLE test.testtbl (
...> col1 text,
...> col2 text,
...> col3 text);

BTEQ "Create set table" equivalent in Snowflake sql

I want to know the equivalent query of Teradata BTEQ "create set table" in Snowflake SQL. I'm working on query conversion between BTEQ to Snowflake. Is there any direct syntax? If not, how can I create a set(Allows only unique values/records) table?
Snowflake doesn't have this functionality, and I don't know any database other than Teradata that does.
You can try to emulate that e.g. by always inserting data using a temporary staging table and then MERGE or INSERT..SELECT.., explicitly avoiding duplicates (on the loading side), or access the data through a view that does SELECT DISTINCT * FROM table.

Copy SQLite table including metadata

I wanted to add a constraint to an existing column in my SQLite database. However, I read that it is not possible to do so.
I tried the solution from How do I rename a column in a SQLite database table?, but there seems to be missing the copying of all the metadata.
I pretty much want an exact copy of a given table, except for the new constraints.
How does the INSERT command look like to copy all the metadata, thus the indexes will increase correctly, for example.
I'm not a heavy user of sqlite3, but you can use the command line to get the data and "create table" and "create index" commands. I am using the 'History' DB from the Google chrome browser which has a table called "visits". The 'mode insert' command says to provide output in a format that can be used to input this data. The '.schema visits' command says to show the 'create table' and 'create index' statements. The 'select..' statement gives you the data. The database I used doesn't seem to have any foreign key constraints, but they could very well be part of the 'create table' information if your DB has any.
sqlite3 History
.mode insert
.schema visits
select * from visits;

Sqlite: How do I reset all database tables?

I want a debug function to do this, but I'm unaware of whether one already exists. Going through and using 'drop table' for each of my tables will be a pain.
Help appreciated.
Since the database is just one file, you can indeed just erase it. If you want something more automatic, you can use the following to do it all programmatically:
Recover your schema:
SELECT group_concat(sql,';') FROM sqlite_master;
Disconnect from the database
Delete the database file
Create your schema again with what was returned from the above query
If you used any particular options for your original database (page_size, etc), they will have to be declared manually as well.
to "drop database" for sqlite, simply delete the database file (and recreate if needed)

Is it safe to run 'create table ...' multiple times?

I have a parser that parses XML file into SQLite database, and the current implementation generates the 'create table xyz ...' even the table is already existed.
Is this OK? I mean, is this OK to run 'create table' even when the table exists in db?
If not, is there easy way to check the names of tables (and its contents) that SQLite db has?
What you are searching for is CREATE TABLE IF NOT EXISTS and the FAQ entry How do I list all tables/indices contained in an SQLite database.
Creating a table without the "IF NOT EXISTS" option will lead to an error.
You can DROP TABLE before CREATE TABLE, you can safety DROP TABLE who don't exists then you don't must checking for TABLE existence before DROP.
SQLite has a "IF NOT EXISTS" clause so that you can throw a "CREATE TABLE" at the database and it will ignore it if it already exists. For example:
CREATE TABLE IF NOT EXISTS mytable ( id INTEGER );
The URL to the documentation about this is at: http://www.sqlite.org/lang_createtable.html

Resources