How to get table rows from sqlite_master using tbl_name - android-sqlite

I am trying to get a table based on the rootpage number/tbl_name and store in a custom TableObject.
I am able to get the table name by the root page number/tbl_name, but I have no idea about getting the row values from that table.
Here is what I have:
SELECT * FROM sqlite_master WHERE name='test1';
or
SELECT * FROM sqlite_master WHERE rootpage=4;
This thing gives me a row which has a table .
Now how can I get the test1 contents ?
The test1 table have two rows in it, called age and name. How can I access those columns ?
At the end I end up using the cursor, so the query will be something like:
cursor=db.rawQuery("SELECT * FROM sqlite_master WHERE rootpage = '" + rootNumber + "'",null);
I can use
cursor.getString(getColumnIndex("name")) //w.r.t sqlite_master
cursor.getString(getColumnIndex("rootpage")) //w.r.t sqlite_master
How can I use the same cursor to get something like:
cursor.getString(getColumnIndex("age")) //w.r.t test1
cursor.getString(getColumnIndex("name")) //w.r.t test1
to get the values from the resulted table.

I would suggest you that once you query sqlite_master table using cursor A, don't use the same cursor A as it contains result of the that query result.
Hence you need to fire another query and store the next result in another cursor B so that you don't lose the data from your first query.
For example, for master table,you do something like :
Cursor cursorA = db.rawQuery("SELECT * FROM sqlite_master WHERE rootpage = '" + rootNumber + "'",null);
cursorA.moveToFirst();
String tableName = cursorA.getString(getColumnIndex("name"));
For further getting result from the table "test1" that you got result from the previous query, you can do something like this :
Cursor cursorB = db.rawQuery("SELECT * FROM '" + tableName + "'",null);
cursorB.moveToFirst();
String name= cursorB.getString(getColumnIndex("name")) ;
int age= cursorB.getString(getColumnIndex("age")) ;
Hence you will get the result that you need further from table "test1".
Hope this helps you :)

Related

QSqlQuery Postgres multiple select

I have a query which contains two part. First part call function which creates a temporary table, second part select data from this table.
SELECT create_data_slice(15962, NULL, ARRAY[[15726]]);
SELECT
AK."15962_15726" as AK_NAME
FROM
t15962 AK
GROUP BY
AK."15962_15726;"
If I execute this query in PgAdmin, it turns right result with data. But if I execute it in Qt:
QSqlDatabase db = store->get_db();
QSqlQuery query(db);
query.exec(sql);
it executes only the first part (create temporary table), but do not execute second part and do not return data.
You can use a transaction like this:
QSqlDatabase::database().transaction();
QSqlQuery query;
query.exec("SELECT create_data_slice(15962, NULL, ARRAY[[15726]]);");
if (query.next())
{
int employeeId = query.value(0).toInt();
query.exec("SELECT AK."15962_15726" as AK_NAME FROM t15962 AK GROUP BY AK."15962_15726;");
while(query.next())
{
qDebug() << query.value().toString(); ///or what you want to do with data
}
}
QSqlDatabase::database().commit();

AS400 index configuration table

How can I view index of particular table in AS400? In which table index description of table is stored?
If your "index" is really a logical file, you can see a list of these using:
select * from qsys2.systables
where table_schema = 'YOURLIBNAME' and table_type = 'L'
To complete the previous answers: if your AS400/IBMi's files are "IBM's old style" Physical and Logical files, the qsys2.syskeys and qsys2.sysindexes are empty.
==> you retrieve index infos in QADBKFLD (for "indexes" info) and QADBXREF(for fields list) tables
select * from QSYS.QADBXREF where DBXFIL = 'YOUR_LOGICAL_FILE_NAME' and DBXLIB = 'YOUR_LIBRARY'
select * from QSYS.QADBKFLD where DBKFIL = 'YOUR_LOGICAL_FILE_NAME' and DBKLB2 = 'YOUR_LIBRARY'
WARNING: YOUR_LOGICAL_FILE_NAME is not your "table name", but the name of the file ! You have to join another table QSYS.QADBFDEP to match LOGICAL_FILE_NAME / TABLE_NAME :
To found indexes from your table's name:
Select r.*
from QSYS.QADBXREF r, QSYS.QADBFDEP d
where d.DBFFDP = r.DBXFIL and d.DBFLIB=r.DBXLIB
and d.DBFFIL = 'YOUR_TABLE_NAME' and d.DBFLIB = 'YOUR_LIBRARY'
To found all indexes' fields from your table:
Select DBXFIL , f.DBKFLD, DBKPOS , t.DBXUNQ
from QSYS.QADBXREF t
INNER JOIN QSYS.QADBKFLD f on DBXFIL = DBKFIL and DBXLIB = DBKLIB
INNER JOIN QSYS.QADBFDEP d on d.DBFFDP = t.DBXFIL and d.DBFLIB=t.DBXLIB
where d.DBFFIL = 'YOUR_TABLE_NAME' and d.DBFLIB = 'YOUR_LIBRARY'
order by DBXFIL, DBKPOS
if your indexes is create with SQL you can see liste of index in sysindexes system view
SELECT * FROM qsys2.sysindexes WHERE TABLE_SCHEMA='YOURLIBNAME' and
TABLE_NAME = 'YOURTABLENAME'
if you want detail columns for index you can join syskeys tables
SELECT KEYS.INDEX_NAME, KEYS.COLUMN_NAME
FROM qsys2.syskeys KEYS
JOIN qsys2.sysindexes IX ON KEYS.ixname = IX.name
WHERE TABLE_SCHEMA='YOURLIBNAME' and TABLE_NAME = 'YOURTABLENAME'
order by INDEX_NAME
You could also use commands to get the information. Command DSPDBR FILE(LIBNAME/FILENAME) will show a list of the objects dependent on a physical file. The objects that show a data dependency can then be further explored by running DSPFD FILE(LIBNAME/FILENAME). This will show the access paths of the logical file.

Comparing two fileds in select command

I am trying to select all records from Transaction_Table where Tr_Amount = Instrument_Number using following Code
Select * from Transaction_Table
where abs(Tr_Amount) = Cast(Instrument_number as INTEGER)
However there are some rows in the table where Instrument_Number is Alphanumeric instead of Just Numeric Data. I there a way to skip the alphanumeric instances in Instrument_Number field in the command.
Switch to TO_NUMBER, which returns NULL for bad data:
Select * from Transaction_Table
where abs(Tr_Amount) = TO_NUMBER(Instrument_number)
TD15.10 implements a TRYCAST:
Select * from Transaction_Table
where abs(Tr_Amount) = TRY_CAST(Instrument_number as INTEGER)

Create a temp table from a select query -- dbVisualizer vs SQL Developer

I've got a query:
SELECT < column names >
INTO <#temp_table>
FROM < table >
WHERE < stuff >
It runs fine in dbVisualizer. However, running it in Oracle SQL Developer gives me the error "The executeQuery method must return a result set."
What is happening here, and how can I fix it in SQL Developer?
EDIT: In response to Tanner, I get the errors when I try the following things (tell me if something I try is invalid. I'm new to SQL):
This:
select * into #temp_table from status
produces this:
The executeQuery method must return a result set.
This:
select * into #temp_table from status;
select * from #temp_table;
produces this:
Invalid object name '#temp_table'.
And this:
select *
from(
select * into #temp_table from status)
produces this:
Incorrect syntax near the keyword 'into'.
I'm lost, ladies and gentledudes.
If you have a query like:
SELECT *
INTO #TEMP
FROM TABLE_A
That is simply creating and inserting data into a temp table.
What you need to do is return that temp table, so after you have run that code you need to do this:
SELECT *
FROM #TEMP

SQL Server : select nvarchar column with null doesn't return any result

I have a table dbo.Villa with this data
and when I run this query, no results are returned:
SELECT *
FROM dbo.Villa v
WHERE v.Section = N'مرکزي'
It is becase of "ي" character in your value. replace it with shift x in persian keyboard layout or change or search with "ی" like this :
SELECT *
FROM dbo.Villa v
WHERE v.Section = N'مرکزی'