开发者

SQlite From clause SELECT statement

开发者 https://www.devze.com 2023-03-24 00:05 出处:网络
My requirement is list of table in the list , when looping the list need to get the android table name from mapping table & call that table name:

My requirement is list of table in the list , when looping the list need to get the android table name from mapping table & call that table name:

see my query :

 SELECT * FROM SELECT ToTable  FROM RDSynchronisationControlHeader 
 WHERE FromTable ='RD.TransactionControl'  

When I run this query from query browser, This is not working? Can't we use select statement in the from clause?

  private boolean isTableRecords开发者_JAVA技巧(String tablename){
    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this);
    boolean recordStatus = false;
      try {
          dbAdapter.createDataBase();
     } catch (IOException e) {
          Log.i("*** select ",e.getMessage());
     }
     dbAdapter.openDataBase();
     String query = "SELECT * FROM SELECT ToTable FROM RDSynchronisationControlHeader WHERE FromTable ='?';";

     String[]d = new String[]{tablename};
     ArrayList stringList = dbAdapter.selectRecordsFromDBList(query, d);


     dbAdapter.close();

     ArrayList<Object> wmRouteList = new ArrayList<Object>();
     recordStatus =  stringList.size() >0 ? true : false;
    return recordStatus;
}

Please help me..

Thanks in advance


Not a big SQLite guy, but in any environment I've worked in, when you use a sub-select, you'll want the subquery to be in parentheses, like:

SELECT * FROM
    (
        SELECT ToTable
        FROM RDSynchronisationControlHeader 
        WHERE FromTable ='RD.TransactionControl'
    )


This should be possible as a query in the form of

$stored_value = SELECT ToTable FROM RDSynchronisationControlHeader WHERE FromTable = 'RD.TransactionControl';

$final_value = SELECT * FROM quote($stored_value);

That may or may not work as is, SQL is not my forte. However, the quote function is capable of turning text into a Table identifier, which is what I believe you need.


SQLite's syntax diagrams (www.sqlite.org) for the SELECT statement contain a confusion between a table's contents and a table's name.

Suppose there's a table called xxxx in your database:

SELECT * FROM (SELECT tbl_name  FROM sqlite_master WHERE tbl_name = 'xxxx' AND Type='table');

returns a totally different result than

SELECT * FROM 'xxxx'

In the first statement the table-or-subquery is something treated as a table's contents, in the second statement the table-or-subquery is a table's name.

So you have to do something like Josh's answer. Run SQLite twice, using the result of the first invocation to construct the second SQL statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消