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
Related
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;
I use Navicat and this command to create temp table in sqlite:
create temp table search as select * from documents
Then when i try to query:
select * from search
I got:
no such table: temp.sqlite_master
or:
no such table
The table doesn't appear in table list too, but when I try to create it again I get:
table search already exists
What is the problem? is it from navicat?
You create statement looks correct to me. When you create a temp table it is deleted when you close the connection string used to create the table. Are you closing the connection after you create the table and then opening it again when you are sending the query?
If not, can you include your query statement too?
It's like a bug in SQLite DLL shipped with Navicat. Test it somewhere else worked ok.
Documentation of SQLite tells this about CREATE TABLE:
If a is specified, it must be either "main", "temp",
or the name of an attached database. In this case the new table is
created in the named database. If the "TEMP" or "TEMPORARY" keyword
occurs between the "CREATE" and "TABLE" then the new table is created
in the temp database. It is an error to specify both a
and the TEMP or TEMPORARY keyword, unless the is
"temp". If no database name is specified and the TEMP keyword is not
present then the table is created in the main database.
May be you should accesse table via temp prefix like this: temp.search.
In MySQL Workbench you can add COMMENTs to tables and columns in a MySQL database.
Does Sqlite support adding comments to tables and columns?
I don't think it does. The "SQL As Understood By SQLite" page makes no mention of table or column comments nor does the CREATE TABLE or ALTER TABLE documentation.
Also, the Unsupported SQL wiki page has this:
2009-08-04: Table and column comments - I have scoured the doco and can't find anything about applying comments to tables or their columns.
Yes, that's a wiki page from 2009 but that note is supported by the rest of the documentation.
However, SQLite does preserve SQL comments that you put in your DDL. If you feed this to the sqlite3 CLI tool:
CREATE TABLE User
-- A table comment
(
uid INTEGER, -- A field comment
flags INTEGER -- Another field comment
);
Then you get exactly that back from a .schema command:
sqlite> .schema
CREATE TABLE User
-- A table comment
(
uid INTEGER, -- A field comment
flags INTEGER -- Another field comment
);
So you should be able to fake it if you can control the DDL used to create your tables.
When creating a table using sqlite (I'm using sqlite3 in python), the COMMENT section is not supported.
This fails (works in full MySql syntax):
CREATE TABLE `Info` (
`Test` VARCHAR(512) NOT NULL COMMENT 'Column info here'
);
This works (no COMMENT in the column declaration):
CREATE TABLE `Info` (
`Test` VARCHAR(512) NOT NULL
);
(This isn't what the original poster was asking, but this is what I was looking for when I first found this question based on the keywords in the title.)
How to make comments in SQLite
There are two ways to make comments in SQLite code:
Hyphens
-- this is my comment
SELECT * FROM employees;
C-style
/* this is my comment */
SELECT * FROM employees;
I appreciate that this is an old post but for what it's worth, you can add comments when creating a table in SQLITE3, in Python and in Java. Probably works for other languages as well.
You need to add new lines to your sql string as you would if you were typing in the command at the SQLITE3 prompt -
sql_str = 'CREATE TABLE properties (\nproperty TEXT NOT NULL, -- A property\nvalue TEXT -- The value of the property\n);'
When executed the table is created like so:
sqlite> .schema
CREATE TABLE properties (
property TEXT NOT NULL, -- A property
value TEXT -- The value of the property
);
I suspect that this works because the connector is actually echoing in the commands via the command prompt, rather than some sort of API.
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.
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)