I'm new to SQLite3 and would like to verify that the table and columns I have created were actually created. Is there a shell command that will display the table and columns? I tried Googling it but all I get is information on creating views. The .help doesn't appear to have anything that would help me. I would appreciate any information on this matter. Thank you in advance.
pragma table_info(YourTable);
lists all the columns of your table
To see the create table statement:
select * from sqlite_master where type = 'table' and tbl_name = 'YourTable';
From SQLite shell:
.schema <TABLE_NAME>
will show schema of TABLE_NAME. Remember not to place ';' after it.
Related
Using SQLite3 and Ubuntu 14.04-LTS. I managed to mis-type when creating a Virtual table for a FTS search. Now I cannot remove the Table. This is what I wanted:
CREATE VIRTUAL TABLE tFind USING FTS4(main TEXT, base TEXT, hash TEXT);
But, I must have hit "5" instead of "4" and now I can't DROP the table. When I try I get this error:
Error refreshing schema for table main.tFind : No such module FTS5
I searched and found information on SQLite3 for the Terminal (CLI), but I do not know how to use the commands within.
How can I repair this? I have many hundreds of rows of data and a dozen or so tables, so I cannot just create a new database with the same tables.
Problem solved: Found th following "
Drop a table originally created with 'unknown tokenizer'?
Where the
And then use the (very dangerous) PRAGMA writable_schema to remove the
remaining information about this table from the system table:
PRAGMA writable_schema = ON;
DELETE FROM sqlite_master WHERE type = 'table' AND name = 'tFind';
Many thanks to CL. for that Post.
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;
How can i see only VIEWS in SQLite.
.tables command list both views as well as tables. I want to see only VIEWS and not tables.
Thanks.
You can do:
SELECT sql FROM sqlite_master WHERE type = 'view';
In order to fine-tune it:
.headers on
select * from sqlite_master;
you will know what columns are available there.
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'm pretty new to SQLite 3 and just now I had to add a column to an existing table I had. I went about doing that by doing: ALTER TABLE thetable ADD COLUMN category;.
Of course, I forgot to specify that column's type. The first thing I was thinking about doing was dropping that column and then re-adding it. However, it seems that SQLite does not have a simple way of doing this, and I would have had to backup the table and re-create it without the column.
This seems messy, and I was wondering if there were just a way of modifying/adding a column's type. I would imagine so, but my searching around yielded no results, being new to SQLite, I imagine it was due to my wording being off in the query.
SQLite doesn't support removing or modifying columns, apparently. But do remember that column data types aren't rigid in SQLite, either.
See also:
SQLite Modify Column
If you prefer a GUI, DB Browser for SQLite will do this with a few clicks.
"File" - "Open Database"
In the "Database Structure" tab, click on the table content (not table name), then "Edit" menu, "Modify table", and now you can change the data type of any column with a drop down menu. I changed a 'text' field to 'numeric' in order to retrieve data in a number range.
DB Browser for SQLite is open source and free. For Linux it is available from the repository.
There is a much simpler way:
ALTER TABLE your_main_table
ADD COLUMN new_column_name new_column_data_type
UPDATE your_main_table
SET new_column_name = CAST(old_column_name as new_data_type_you_want)
I tried this on my machine locally and it works
It is possible by recreating table.Its work for me please follow following step:
create temporary table using as select * from your table
drop your table, create your table using modify column type
now insert records from temp table to your newly created table
drop temporary table
do all above steps in worker thread to reduce load on uithread
It is possible by dumping, editing and reimporting the table.
This script will do it for you (Adapt the values at the start of the script to your needs):
#!/bin/bash
DB=/tmp/synapse/homeserver.db
TABLE="public_room_list_stream"
FIELD=visibility
OLD="BOOLEAN NOT NULL"
NEW="INTEGER NOT NULL"
TMP=/tmp/sqlite_$TABLE.sql
echo "### create dump"
echo ".dump '$TABLE'" | sqlite3 "$DB" >$TMP
echo "### editing the create statement"
sed -i "s|$FIELD $OLD|$FIELD $NEW|g" $TMP
read -rsp $'Press any key to continue deleting and recreating the table $TABLE ...\n' -n1 key
echo "### rename the original to '$TABLE"_backup"'"
sqlite3 "$DB" "PRAGMA busy_timeout=20000; ALTER TABLE '$TABLE' RENAME TO '$TABLE"_backup"'"
echo "### delete the old indexes"
for idx in $(echo "SELECT name FROM sqlite_master WHERE type == 'index' AND tbl_name LIKE '$TABLE""%';" | sqlite3 $DB); do
echo "DROP INDEX '$idx';" | sqlite3 $DB
done
echo "### reinserting the edited table"
cat $TMP | sqlite3 $DB