What SQL can be used to list the tables, and the rows within those tables in an SQLite database file - once I have attached it with the ATTACH command on the SQLite 3 command line tool?
There are a few steps to see the tables in an SQLite database:
List the tables in your database:
.tables
List how the table looks:
.schema tablename
Print the entire table:
SELECT * FROM tablename;
List all of the available SQLite prompt commands:
.help
The .tables, and .schema "helper" functions don't look into ATTACHed databases: they just query the SQLITE_MASTER table for the "main" database. Consequently, if you used
ATTACH some_file.db AS my_db;
then you need to do
SELECT name FROM my_db.sqlite_master WHERE type='table';
Note that temporary tables don't show up with .tables either: you have to list sqlite_temp_master for that:
SELECT name FROM sqlite_temp_master WHERE type='table';
It appears you need to go through the sqlite_master table, like this:
SELECT * FROM dbname.sqlite_master WHERE type='table';
And then manually go through each table with a SELECT or similar to look at the rows.
The .DUMP and .SCHEMA commands doesn't appear to see the database at all.
To show all tables, use
SELECT name FROM sqlite_master WHERE type = "table"
To show all rows, I guess you can iterate through all tables and just do a SELECT * on each one. But maybe a DUMP is what you're after?
Use .help to check for available commands.
.table
This command would show all tables under your current database.
There is a command available for this on the SQLite command line:
.tables ?PATTERN? List names of tables matching a LIKE pattern
Which converts to the following SQL:
SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1
To list the tables you can also do:
SELECT name FROM sqlite_master
WHERE type='table';
I use this query to get it:
SELECT name FROM sqlite_master WHERE type='table'
And to use in iOS:
NSString *aStrQuery=[NSString stringWithFormat:#"SELECT name FROM sqlite_master WHERE type='table'"];
Try PRAGMA table_info(table-name);
http://www.sqlite.org/pragma.html#schema
According to the documentation, the equivalent of MySQL's SHOW TABLES; is:
The ".tables" command is similar to setting list mode then executing
the following query:
SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1;
However, if you are checking if a single table exists (or to get its details), see LuizGeron's answer.
As of the latest versions of SQLite 3 you can issue:
.fullschema
to see all of your create statements.
The easiest way to do this is to open the database directly and use the .dump command, rather than attaching it after invoking the SQLite 3 shell tool.
So... (assume your OS command line prompt is $) instead of $sqlite3:
sqlite3> ATTACH database.sqlite as "attached"
From your OS command line, open the database directly:
$sqlite3 database.sqlite
sqlite3> .dump
Via a union all, combine all tables into one list.
select name
from sqlite_master
where type='table'
union all
select name
from sqlite_temp_master
where type='table'
Use:
import sqlite3
TABLE_LIST_QUERY = "SELECT * FROM sqlite_master where type='table'"
Use .da to see all databases - one is called 'main'.
Tables of this database can be seen by:
SELECT distinct tbl_name from sqlite_master order by 1;
The attached databases need prefixes you chose with AS in the statement ATTACH, e.g., aa (, bb, cc...) so:
SELECT distinct tbl_name from **aa.sqlite_master** order by 1;
Note that here you get the views as well. To exclude these add:
where type = 'table'
before ' order'
Since nobody has mentioned about the official reference of SQLite, I think it may be useful to refer to it under this heading:
https://www.sqlite.org/cli.html
You can manipulate your database using the commands described in this link. Besides, if you are using Windows OS and do not know where the command shell is, that is in the SQLite's site:
https://www.sqlite.org/download.html
After downloading it, click sqlite3.exe file to initialize the SQLite command shell. When it is initialized, by default this SQLite session is using an in-memory database, not a file on disk, and so all changes will be lost when the session exits. To use a persistent disk file as the database, enter the ".open ex1.db" command immediately after the terminal window starts up.
The example above causes the database file named "ex1.db" to be opened and used, and created if it does not previously exist. You might want to use a full pathname to ensure that the file is in the directory that you think it is in. Use forward-slashes as the directory separator character. In other words use "c:/work/ex1.db", not "c:\work\ex1.db".
To see all tables in the database you have previously chosen, type the command .tables as it is said in the above link.
If you work in Windows, I think it might be useful to move this sqlite.exe file to same folder with the other Python files. In this way, the Python file writes to and the SQLite shell reads from .db files are in the same path.
The ".schema" commando will list available tables and their rows, by showing you the statement used to create said tables:
sqlite> create table_a (id int, a int, b int);
sqlite> .schema table_a
CREATE TABLE table_a (id int, a int, b int);
Related
I am creating a job to pull data from a database to CSV file using talend open studio. There are 100 of tables, the data types and no of columns differ in the tables, I want to pull the data from database tables with a single job and customizable SQL query. I know how to create and use context variables.
If I understood you correctly you should be using tMap's reload at each row -option and defining table names in Excel sheet or in tFixedFlowInput.
tMap settings
Whole job and results
SQL Script:
"SELECT TOP(1) Name, Code from mdm." + (String)globalMap.get("row4.table")
I used Microsoft SQL Server for example but same script works as well with MySQL.
You can simply use a context-variable which you set via the --context_param argument in a tWhicheverDatabaseInput. E.g. define a context variable "my_sql" which you can set in the commandline as
my_job.sh --context_param my_sql="select a,b,c from a_test_table"
and then use context.my_sql as the SQL in you database input component.
However, as garpitmzn already mentioned, you will need dynamic schemas to actually work with this unknown structure in Talend. This feature only exists in the enterprise version.
If the enterprise version is available to you, simply declare a single column of type "Dynamic", which you can pass through the rest of your flow.
Declare a local context say query as type string.
Prepare a context file with variable query: query=select name from employee
Excecute the query: toraclecomponent use context.query
Query is throwing some error when you have where conditions with string type.
Need to investigate more on that. Otherwise it works.
Hello guys I need fill a table with el result of my query like....
SELECT FIELD1, FIELD2, X FROM OLDTABLE WHERE X=Y
I am a Java developer, my friends, RPG developers in the AS400. When they execute a a Query have a option to save the query result in a file
The option is called SELECT output and can choice 1 Display 2 Printer 3 File
Can do this directly from a query? or is a native iSeries option ?
Create a table with iseries sql
create a table with data.
create table abc as (select x,y,z from sometable where x=y) with data
create an empty table.
create table abc as (select x,y,z from sometable where x=y) data definition only
There is no output to printer using just sql.
Query will prompt you to replace an existing table. Straight SQL won't prompt to replace an existing table so you have two scenarios (see note) .
If the output table doesn't exist, all you need is
create table newtable as (select <...> from oldtable) with data
If the output table already exists, all you need is an insert with sub-select.
Insert into newtable
Select <...> from oldtable
NOTE
With the release of TR10 for v7.1 and TR2 for v7.2 in May 2015, IBM has added support the OR REPLACE clause to the CREATE TABLE statement. So if you happen to be on those TRs or higher, you could simply use:
create or replace table newtable as (select <...> from oldtable) with data
Could compile the SQL into a query manager query (CRTQMQRY) then run the query via (STRQMQRY).
To do that, put the query into the some sort of source file with a member type of TXT. Get to a command line and run the CRTQMQRY command and create the output QMQRY. STRQMQRY can be prompted and you can save the results in either an output file or a printout or look at it interactively. If you submit it as a batch job, viewing the output interactively won't work too well.
During a query of a user's contacts, I want to use a temporary table specific to that user, e.g. tmp_contact_{userId}, if userId is 123, should resolve to tmp_contact_123
I have inherited a java project that uses iBatis to interoperate with a MySql database.
I've read some of the iBatis documentation, and I see replaceable parameters in the xml as either #{userId} or #userId#. The former appears to be interpreted as a parameter for a prepared statement and produces a question mark, e.g. 'create temporary table tmp_contact_?', while the latter was left as a literal, e.g. 'create temporary table tmp_contact_#userId#'.
Is there alternate syntax to produce 'create temporary table tmp_contact_123'?
You need to do following changes in your .xml file:
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="xxx" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<statements>
<select id="GetTempTableData" resultClass="Student" parameterClass="System.String">
CREATE TABLE ##TEMPTABLE (ID int, Name varchar(20))
INSERT INTO ##TEMPTABLE (ID, NAME) VALUES (1, 'RAVI')
INSERT INTO ##TEMPTABLE (ID, NAME) VALUES (2, 'KAPIL')
SELECT * FROM ##TEMPTABLE
</select>
</statements>
</sqlMap>
Note : You need to create temp table inside the select satement. You need to use double "##" to create temp table in IBATIS. "##" will replaced with single "#" and it will create temp table with the name of "#TEMPTABLE". Please remember their are some limitation of temp table that you need to search.
Hope this will help you..
Yes there is a way to do this!
Using # puts a quote around string params. You can use $ instead. For your example, it would be
create temporary table tmp_contact_$userId$
When iBatis compiles this statement it will be
create temporary table tmp_contact_123
and not
create temporary table tmp_contact_'123'
It is possible in MS SQL Server to store the results of query into a table, and most importantly, have the query create the table:
SELECT an_existing_column
INTO table_xyz
FROM an_existing_table
This is also possible in MySQL using:
CREATE TABLE table_xyz
SELECT an_existing_column
FROM an_existing_table
I have searched the Apache Derby Reference Guide and cannot see a method for achieving similar behaviour.
Does anyone know if this possible in Apache Derby?
Store the results of a query into a table:
INSERT INTO table_xyz (an_existing_column) SELECT an_existing_column FROM an_existing_table;
Create a table from another table:
All Columns:
CREATE TABLE table_xyz AS SELECT * FROM an_existing_table WITH NO DATA;
Specific Column:
CREATE TABLE table_xyz AS SELECT an_existing_column FROM an_existing_table WITH NO DATA;
It does not work in JAVA DB, the correct way to do it is:
For all columns:
Step 1: Create a new table with a different name. for example, my_new_table:
CREATE TABLE my_new_table AS SELECT * FROM original_table WITH NO DATA;
This statement creates a new table from original table in the same format and no data copied. It is required to specify WITH NO DATA for it creates a new table with the same columns.
Step 2: Copy data from orig_table to my_new_table using INSERT INTO.
INSERT INTO my_new_table SELECT * FROM orig_table.
Then you will have all the data copied.
I'm using Mysql database. I got stored data for certain columns with value of a\"a. I used following query to select but it failed:
select * from table_name where coloum_name like "%a\"%";
I spend some hours to find a query to select and the following one is working:
select * from table_name where coloum_name like "%a\\\\\\\\""%";
I'm using hibernate in my application, so I used:
criteria.add(Restrictions.ilike("coloum_name","%a\\\\\\\\""%"));
but it's not working. Any possible way to select via criteria??
Check this one. Hope this will help for you.
http://levanhuy.wordpress.com/2009/02/19/providing-an-escape-sequence-for-criteria-queries/