In bash, I use this to read from a sqlite database:
TMP=`echo "select * from Table limit 1;" | sqlite3 mysqlite3database.db`
tokens=(${TMP//|/ })
PARAM_1=${tokens[0]}
PARAM_2=${tokens[1]}
PARAM_3=${tokens[2]}
PARAM_4=${tokens[3]}
This unfortunately does not work with dash - is there a way?
PARAM_1=${tokens% *}
tokens=${tokens#* }
PARAM_2=${tokens% *}
and so forth
Related
I need to count entries grouped by date (field has TIMESTAMP type in database).
I wrote code
sql = "
SELECT COUNT(id) AS cnt, TO_CHAR(closed_on, 'YYYY-mm-dd') AS closed_on, issues.status_id
FROM issues
WHERE closed_on IS NOT NULL AND closed_on <= '#{end_from_created_date}'
AND created_on <= '#{end_from_created_date}'
GROUP BY closed_on, status_id
"
This query work on postrgesql, but it does not work on sqlite, because sqlite does not have TO_CHAR function
Solution should be compatible with mysql, postgresql, sqlite.
I can use different sql for different DB. But it is not good idea.
May be I can use functions from ActiveRecord, but i do not find solutions in docs
SQLite uses several date formats; PostgreSQL uses similar formats and more.
Both understand YYYY-MM-DD HH:MM:SS.
The solution is
sql = "SELECT COUNT(id) AS cnt, DATE(closed_on) AS closed_on, issues.status_id FROM issues WHERE closed_on IS NOT NULL AND closed_on <= '#{end_from_created_date}' AND created_on <= '#{end_from_created_date}' GROUP BY DATE(closed_on), status_id"
i am using informix DB , i need to get records that contain alpha [A-Za-z] character on last character
what i try is :
select * from table_name
where (SUBSTR(trim(customer),-1,1)!='0' and SUBSTR(trim(customer),-1,1)!='1' and SUBSTR(trim(customer),-1,1)!='2' and SUBSTR(trim(customer),-1,1)!='3' and SUBSTR(trim(customer),-1,1)!='4' and SUBSTR(trim(customer),-1,1)!='5' and SUBSTR(trim(customer),-1,1)!='6' and SUBSTR(trim(customer),-1,1)!='7' and SUBSTR(trim(customer),-1,1)!='8' and SUBSTR(trim(customer),-1,1)!='9') or (SUBSTR(trim(customer),-1,1)=' ') or (SUBSTR(trim(customer),-1,1)='') or (customer IS NULL)
is there are any way to write where SUBSTR(trim(customer),-1,1)=alpha rather than write
SUBSTR(trim(customer),-1,1)!='0' and SUBSTR(trim(customer),-1,1)!='1' and SUBSTR(trim(customer),-1,1)!='2' and SUBSTR(trim(customer),-1,1)!='3' and SUBSTR(trim(customer),-1,1)!='4' and SUBSTR(trim(customer),-1,1)!='5' and SUBSTR(trim(customer),-1,1)!='6' and SUBSTR(trim(customer),-1,1)!='7' and SUBSTR(trim(customer),-1,1)!='8' and SUBSTR(trim(customer),-1,1)!='9'
If you have a 'recent' version of Informix (anything over 12.10 should do) you can use regex_match():
https://www.ibm.com/support/knowledgecenter/SSGU8G_14.1.0/com.ibm.dbext.doc/ids_dbxt_544.htm
Something like :
> select * from table(set{'test','test1','tesT'})
where regex_match(unnamed_col_1, '[a-zA-Z]$');
unnamed_col_1
test
tesT
2 row(s) retrieved.
>
You can use the Informix MATCHES operator, that is supported in any version.
The query would be like this:
select * from table_name
where customer matches "*[A-Za-z]"
Is there a way to show all the content inside a table by using psql command line interface?
I can use \list to show all the databases, \d to show all the tables, but how can I show all the data in a table?
Newer versions: (from 8.4 - mentioned in release notes)
TABLE mytablename;
Longer but works on all versions:
SELECT * FROM mytablename;
You may wish to use \x first if it's a wide table, for readability.
For long data:
SELECT * FROM mytable LIMIT 10;
or similar.
For wide data (big rows), in the psql command line client, it's useful to use \x to show the rows in key/value form instead of tabulated, e.g.
\x
SELECT * FROM mytable LIMIT 10;
Note that in all cases the semicolon at the end is important.
Step 1. Check the display mode is "on" by using
\x
Step 2. Don't forget the ;
I tried for fifteen minutes just because I forgot the semicolon.
AND USE UPPERCASE ENGLISH.
TABLE users;
And you will get something like
On Windows use the name of the table in quotes:
TABLE "user"; or SELECT * FROM "user";
you should use quotes
example =>
1) \c mytablename
2) SELECT * FROM "mytablename"; OR TABLE "mytablename";
postgres commande line
to show databases : \l
to show tables : \dt
to show data in table x : SELECT * FROM "x";
to exit : \q
If you use schemas, the following will be correct:
SELECT * FROM "schema-name"."table-name";
Rails - 2.3.8
Database - Postgres(9.2)
Active record query is not able to generate tablename in double quotes ie
# this ran fine
Table.find_by_sql('Select * from "Table" Limit 1')
Sql generated - Select * from "Table" Limit 1
But issue comes in,
Table.find(:first)
Sql generated - Select * from Table Limit 1 (Clearly noticed that table not in double quotes)
Active record displaying error
ActiveRecord::StatementInvalid: RuntimeError: ERROR
C42P01 Mrelation "Table" does not exist
P15 Fparse_relation.c L864
RparserOpenTable: SELECT * FROM Table LIMIT 1
I feel that postgresql adapter is not able to generate tablename in double quotes.
I never get a chance to work on Postgres. But I have a workaround solution for this. Try as follows:
table_name = '"Table"'
table_name.find(:first)
I haven't try this in my machine since I do not have the required setup. I hope it should work.
I have a note field of string type in one of my models in my sqlite3 database, but I realized that I needed to store more text than string would allow.
I just ran a migration changing the type of the field from string to text. Looking at my database, it says that the type is now text(255), whereas before it was varchar(255).
What does the 255 mean? Is that a character limit? If so, would I have the same storage problems as before? How would I fix this?
Here is the migration I used to change the field type
change_column(:posts, :note, :text)
SQLite does not enforce text storage limits. If you declare a column VARCHAR(1) or TEXT(1) you still can store some very long blob of 500 Megabytes inside it. Even though that is not adviseable.
~$ sqlite
SQLite version 3.7.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo ( bar text(3));
sqlite> insert into foo values ('aeiou');
sqlite> select * from foo;
aeiou
You should just make your storage type text and not put any limit on it. The 255 denotes the maximum number of characters allowed in the field.