SQLite time insert query - sqlite

I want to insert time value to SQLite. I searched about functions, modifiers, timestrings but I could not achieve to my aim. When I write my query, this does not record the '.323', only records '08:25:01'. I want to record '08:25:01.323'. My query is:
insert into table_name (column_name) values (time('08:25:01.323'))
I'm waiting for your help..

Store the time as a string and only parse it whe necessary.
sqlite> create table t (t text);
sqlite> insert into t values ('08:25:01.323');
sqlite> select * from t;
08:25:01.323
sqlite> select t, time(t) from t;
08:25:01.323|08:25:01

Related

how to get the ordered query in sqlite?

I insert some data into test table.
C:\> sqlite3 e:\\test.db
sqlite> create table test(code TEXT,content numeric);
sqlite> insert into test(code,content)values('x',3);
sqlite> insert into test(code,content)values('x',1.5);
sqlite> insert into test(code,content)values('x',1);
sqlite> insert into test(code,content)values('y',9);
sqlite> insert into test(code,content)values('y',3);
sqlite> insert into test(code,content)values('y',2);
sqlite> insert into test(code,content)values('y',12.3);
sqlite> insert into test(code,content)values('y',11.5);
how can i get the ordered output as the following ?
select * from test group by code order by content;can not get it.
select * from test order by content;neither.
x|1
x|1.5
x|3
y|2
y|3
y|9
y|11.5
y|12.3
You can sort by multiple criteria:
SELECT * FROM test ORDER BY code, content;

Not empty string constraint in SQLite

Can I create a database constraint on a TEXT column in SQLite disallowing the value of the column to be empty string ""?
I want to allow the column to be null, but disallow empty string.
Yes you can:
sqlite> create table foo (bar TEXT, CHECK(bar <> ''));
sqlite> insert into foo values (NULL);
sqlite> insert into foo values ('bla');
sqlite> insert into foo values ('');
Error: constraint failed
You can use a CHECK constraint (http://www.sqlite.org/lang_createtable.html):
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> create table example(col, CHECK (col is null or length(col) > 0));
sqlite> insert into example values ('');
SQL error: constraint failed
sqlite> insert into example values (null);
sqlite> insert into example values ('sample');
sqlite> .nullvalue NULL
sqlite> select col from example;
NULL
sample
As far as i know doesn't exist a similar constraint in SQLite, but maybe you can workaround with a Trigger that on INSERT and/or UPDATE automatically change the string empty in NULL.

Sqlite - default timestamp to be `now + a few days`

I am trying to have a column holding the timestamp value whose default value is today + a few days. Could this be done during table creation time?
Yes it can be done as in the following example:
sqlite> create table foo (i int, j text default (datetime('now', '+5 days')));
sqlite> insert into foo (i) values (1);
sqlite> select * from foo;
1|2012-04-11 07:49:04
sqlite> insert into foo (i) values (2);
sqlite> select * from foo;
1|2012-04-11 07:49:04
2|2012-04-11 07:49:14
If you only want to store the date part, use date instead of datetime. Here I use datetime to show that the default expression is evaluated when inserting in the table, not when the table is created.

Update statement in sqlite

Hey what i want to do is update a table if the field exsits or insert the new value as given by the user.
For this what i do is select everything present in the table and check if the name entered by the user exists den update it or else insert it.
However the problem is when there is no data in the table the query fails and no data is inserted in the table.
NSString *query = #"select * from PPM";
sqlite_stmt *statement;
if(sqlite_prepare_v2(database,[query UTF8string],-1,&statement,NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
//select data from table
//if value entered is same as entered by user update the table
//else insert in the table
}
How about INSERT OR REPLACE ?
You can use it with the UNIQUE constraints in your database structure to replace a value if it already exists, and insert it if it not there already.
shell> sqlite3
SQLite version 3.6.23
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE user(name UNIQUE, value);
sqlite> INSERT OR REPLACE INTO user VALUES("foo", 123);
sqlite> SELECT * FROM user;
foo|123
sqlite> INSERT OR REPLACE INTO user VALUES("foo", 321);
sqlite> SELECT * FROM user;
foo|321
If you use the INSERT OR REPLACE variation of the INSERT command, you don't need to check for existing data, as SQLite will do it for you, as long as there is a column with a UNIQUE constraint.
For example:
CREATE TABLE db.table(id UNIQUE, name);
INSERT OR REPLACE INTO db.table (id, name) VALUES (5, 'exep');
If an id of 5 exists in the table, this command will behave like an update, else it will be an insert.
See http://www.sqlite.org/lang_conflict.html for more details.

How can I get the list of a columns in a table for a SQLite database?

I am looking to retrieve a list of columns in a table. The database is the latest release of SQLite (3.6, I believe). I am looking for code that does this with a SQL query. Extra bonus points for metadata related to the columns (e.g. length, data type, etc...)
What you're looking for is called the data dictionary. In sqlite a list of all tables can be found by querying sqlite_master table (or view?)
sqlite> create table people (first_name varchar, last_name varchar, email_address varchar);
sqlite> select * from sqlite_master;
table|people|people|2|CREATE TABLE people (first_name varchar, last_name varchar, email_address varchar)
To get column information you can use the pragma table_info(table_name) statement:
sqlite> pragma table_info(people);
0|first_name|varchar|0||0
1|last_name|varchar|0||0
2|email_address|varchar|0||0
For more information on the pragma statements, see the documentation.
Here's the simple way:
.schema <table>
The question is old but the following hasn't been mentioned yet.
Another convenient way in many cases is to turn headers on by:
sqlite> .headers on
Then,
sqlite> SELECT ... FROM table
will display a headline showing all selected fields (all if you SELECT *) at the top of the output.
Here's a SELECT statement that lists all tables and columns in the current database:
SELECT m.name as tableName,
p.name as columnName
FROM sqlite_master m
left outer join pragma_table_info((m.name)) p
on m.name <> p.name
order by tableName, columnName
;
just go into your sqlite shell:
$ sqlite3 path/to/db.sqlite3
and then just hit
sqlite> .schema
and you will get everything.
This is a query that lists all tables with their columns, and all the metadata I could get about each column as OP requested (as bonus points).
SELECT
m.name AS table_name,
p.cid AS col_id,
p.name AS col_name,
p.type AS col_type,
p.pk AS col_is_pk,
p.dflt_value AS col_default_val,
p.[notnull] AS col_is_not_null
FROM sqlite_master m
LEFT OUTER JOIN pragma_table_info((m.name)) p
ON m.name <> p.name
WHERE m.type = 'table'
ORDER BY table_name, col_id
Thanks to #David Garoutte for showing me how to get pragma_table_info to work in a query.
Run this query to see all the table metadata:
SELECT * FROM sqlite_master WHERE type = 'table'
Building on the above, you can do it all at once:
sqlite3 yourdb.db ".schema"
That will give you the SQL to create the table, which is effectively a list of the columns.
In case if you want to get all column names into one single comma separated string, you can use below.
SELECT GROUP_CONCAT(NAME,',') FROM PRAGMA_TABLE_INFO('table_name')
Here the pragma table_info is used as pragma_table_info for the select statement and GROUP_CONCAT is to combine all the field names into one string. for the second parameter of GROUP_CONCAT you can pass the separator.
I know, it’s been a long time but it’s never too late…
I had a similar question with TCL as interpreter and after several search, found nothing good for me. So I propose something based on PRAGMA, knowing that your DB is “main”
db eval { PRAGMA main.table_info(<your table name>) } TBL { puts $TBL(name) }
And array use to obtain a list
set col_list {}
db eval { PRAGMA main.table_info(<your table name>) } TBL { lappend col_list $TBL(name) }
puts $col_list

Resources