I have found the solution to get all tables in SQLite here.
How to list the tables in an SQLite database file that was opened with ATTACH?
However, when I change:
SELECT name FROM sqlite_master WHERE type='table';
into:
SELECT name FROM sqlite_master WHERE type='table' ORDER BY name DESC;
the output is completely weird.
The first query gives me:
tbl201306 --> only 1 table so far, for June 2013.
The second query gives me:
android_metadata --> the name is not changed, but it returns this name.
I want to have these tables in descending order because in the future, the newest table would be on the top then.
My complete code:
public ArrayList<String> getDBTables() {
ArrayList<String> toReturn = new ArrayList<String>();
try {
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name DESC", null);
c.moveToFirst();
while (c.moveToNext()) {
toReturn.add(c.getString(c.getColumnIndex("name")));
}
c.close();
}
catch(SQLiteException e) {
e.printStackTrace();
}
return toReturn;
}
Your code skips over the first returned record.
You can either
keep the call to moveToFirst(), and change the loop into a do { ... } while (); loop, or
just remove the call to moveToFirst().
This is the ultimate solution I used:
public ArrayList<String> getDBTables() {
ArrayList<String> toReturn = new ArrayList<String>();
try {
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'tbl%' ORDER BY name DESC", null);
while(c.moveToNext()) {
toReturn.add(c.getString(c.getColumnIndex("name")));
}
c.close();
}
catch(SQLiteException e) {
e.printStackTrace();
}
return toReturn;
}
Related
Can anyone help me how to execute this query?
I am trying to fetch distinct supervisor from project table. supervisor is column name and its index is 5.
try {
String query = "select distinct supervisor from project ";
Cursor cursor= db.rawQuery(query,null);//query( query , null, null,null,null,null,null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(5));
} while (cursor.moveToNext());
}
// finish();
cursor.close();
db.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return labels;
}
Your query is wrong.
You have to query write like this.
select * from project where YOUR_CONDITION
I don't know what you exactly trying to do.
Example query.
select * from customers where LastName = 'Tremblay'
To get Distinct value of supervisor you should query like this.
select * from project group by supervisor
Hope it helps:)
You could use an if statement ie if cursor != null, do something.
I am trying to get the last 3 entries made to the database. Im using the following code but im only getting the last entry not the last 3.
String query = "Select * from " + TABLE_DETAILS + " DESC limit 3;";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
cursor.moveToFirst();
System.out.println(Integer.parseInt(cursor.getString(2)));
cursor.close();
} else {
}
db.close();
Thanks
You'll need to loop with the cursor to get all result rows, e.g. instead of
if (cursor.moveToFirst()) {
...
loop like this
while (cursor.moveToNext()) {
System.out.println(cursor.getString(2));
}
cursor.close();
To change the ordering, add ORDER BY <expression> to your query, for example
SELECT ... ORDER BY _id DESC LIMIT 3
I have a tables and records like :
EmployeeName
------------
Ram
Laxman
Bharat
Shatrugn
where i want to output to concat all values in one row in a single query:
I want result like:
Ram,Laxman,bharat,shatrugn
Concat string with ,(comma) in singlee line..
but i don't know that how to concat in android using cursor...
In SQLite, you can use GROUP_CONCAT():
select Group_Concat(EmployeeName)
from table1
See SQL Fiddle with Demo
If you had multiple fields that you want to return, then you would use a GROUP BY with the query, similar to this:
select id, Group_Concat(EmployeeName)
from table1
group by id
See SQL Fiddle with Demo
Here is my code i used...hope it helps you.
private SQLiteDatabase myDataBase;
Cursor cursor;
String S="";
String myPath2 = yourDBpath + yourDBNAME;
try{
myDataBase = SQLiteDatabase.openDatabase(myPath2, null,SQLiteDatabase.OPEN_READWRITE);
String sql="your query";
cursor=myDataBase.rawQuery(sql, null);
if(cursor != null)
{
while(cursor.moveToNext())
{
S=S.append(cursor.getString(0));
}
}
}
}catch(Exception e){
}finally{
myDataBase.close();
}
Final result will be there in String S.
String values;
if (cursor.moveToFirst()) {
do {
values=values + cursor.getString(0)+",";
} while (cursor.moveToNext());
remove last comma
if (values.length() > 0)
{
values= values.substring(0,values.length() - 1);
}
In My Application, I am going to store name of all the available tables from my database with this code:
public ArrayList<Object> showAllTable()
{
ArrayList<Object> tableList = new ArrayList<Object>();
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
if(cursor.getString(0).equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
tableList.add(cursor.getString(0));
}
}
while (cursor.moveToNext());
}
cursor.close();
return tableList;
}
Yes i am able to store it. But while i am going to display it in to ListView, it will display in such way that last created shown at last. I want to display the list of the table as Last created, display first in to ListView.
Please help me, What should I have to do in my code to set such way of displaying tables?
There are several ways to do it. The easiest is probably to change your SQL call by adding ORDER BY. Here's something you can try out:
To order the items ascending:
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ASC";
Or descending:
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name DESC";
Alternative solution
Another option would be to do it like you're doing it right now, and instead go through the Cursor from the last to the first item. Here's some pseudo code:
if (cursor.moveToLast()) {
while (cursor.moveToPrevious()) {
// Add the Cursor data to your ArrayList
}
}
before return the values just call the method
Collections.reverse(tableList);
see the full code
public ArrayList<Object> showAllTable()
{
ArrayList tableList = new ArrayList();
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
if(cursor.getString(0).equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
tableList.add(cursor.getString(0));
}
}
while (cursor.moveToNext());
}
cursor.close();
Collections.reverse(tableList);
return tableList;
}
Does anyone know the SQL to get a list of table names via code in Android? I know .tables does it through command shell but this doesn't work through code. Is it anything to do with the meta-data, etc.?
Just had to do the same. This seems to work:
public ArrayList<Object> listTables()
{
ArrayList<Object> tableList = new ArrayList<Object>();
String SQL_GET_ALL_TABLES = "SELECT name FROM " +
"sqlite_master WHERE type='table' ORDER BY name";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
tableList.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
cursor.close();
return tableList;
}
Got it:
SELECT * FROM sqlite_master WHERE type='table'