Android get list of tables - android

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'

Related

sqlite LIKE function doesnt works with comma?

I am wondering, my codes run but it doesn't return results as what I want. For example the data on my
column1 is : hotdog,bacon,cheese
and I want to search bacon. like query cant find bacon. but if I searched cheese or hotdog like query can find it. I think if the word surrounds with a comma. What do I need to do?
public ArrayList<String> getrecom(int recom1) {
ArrayList<String> result = new ArrayList<String>();
SQLiteDatabase db = dbHelperFoods.getWritableDatabase();
String selectQuery = "SELECT * FROM "+ recom.TABLE+" WHERE " +KEY_recom+ " LIKE '%"+recom1+"%'";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
result.add(cursor.getString(cursor.getColumnIndex("name")));
while (cursor.moveToNext()) {
result.add(cursor.getString(cursor.getColumnIndex("name")));
}
}
cursor.close();
db.close();
return result;
}

SQLite select all tables DESC

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;
}

searching a particular string in the entire database using Sqlite3?

I want to search a particular string in the whole database table and get the row details of the matched column. For example I am having the table as like below
**name email address designation**
sai xy#mail 75385 nagar
thiru y#mail 75893 city
Now I am having the searching key like "nagar" or "thiru" how can prepare a query for it. if any one knows Please help me. Thanks in advance.
Read the whole table into a cursor and search in each column to get column name.
May be like this:
private String getColumnName(String key){
String columnName="";
DataBaseAdapter dba=new DatabaseAdapter(getApplicationContext());
dba.open();
Cursor cr=dba.fetchAllData();
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("name")))){
columnName="name";
}
cr.moveToNext();
}
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("email")))){
columnName="email";
}
cr.moveToNext();
}
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("address")))){
columnName="address";
}
cr.moveToNext();
}
cr.moveToFirst();
while(!cr.isAfterLast()){
if(key.equals(cr.getString(cr.getColumnIndex("designation")))){
columnName="designation";
}
cr.moveToNext();
}
return columnName;
}
fetchAllData() function in DataBaseAdapter class contains following code:
public Cursor fetchAllData(){
try {
return database.query(TABLENAME, null, null, null, null,
null, null);
} catch (Exception e) {
return null;
}
}
This will give column name of your key.
So now it's easy to run query like this:
String query= "SELECT * FROM tablename where "+getColumnName()+"='"+key+"'" ;
try this query..
SELECT * FROM tablename WHERE name = 'nagar' or email = 'nagar' or address = 'nagar' or designation = 'nagar';
or
SELECT * FROM tablename WHERE name or email or address or designation = 'nagar';

How to set the arraylist item to be add in to reverse order?

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;
}

Android SQLite rawquery returns only first record

My SQLite query returning only one record, However, the table has multiple rows
cursor=mydb.rawQuery("Select category from items;", null);
I have even tried GROUP BY but still wont work.
I am new to SQLite, would appreciate any help. Thanks.
First of all your string query must not be terminated so instead of passing it as:
"Select category from items;"
you should try passing it as:
"Select category from items"
as mentioned on this page.
Also, are you looping over the cursor? Here is an example of how to get data out of a cursor with a while loop:
ArrayList<String> results = new ArrayList<String>()
while (cursor.moveNext()) {
results.add(cursor.getString(0)); // 0 is the first column
}
First, search:
Cursor cs = myDataBase.rawQuery("Your query", null);
if (cs.moveToFirst()) {
String a = cs.getString(cs.getColumnIndex("your_column_name"));
cs.close();
return a;
}
cs.close();
return "";
Get the information from cursor:
if (cs.moveToFirst()) {
ArrayList<String> results = new ArrayList<String>()
do {
results.add(cs.getString(cs.getColumnIndex("your_column_name")));
} while (cs.moveNext());
}
Without if, I took error in my project. But this worked for me. By the way, your query doesn't look good. If you give some information about your database, we can help much more.
Use this to select all items from the table:
Cursor cursor = db.rawQuery("SELECT * FROM Tablename ,
null);
this can help you
public ArrayList<mydata> getallContents() {
ArrayList<mydata> lst = new ArrayList<mydata>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + GAMETABLE, null);
if (c.moveToFirst()) {
do {
lst.add(new mydata(c.getString(1),
c.getString(3),c.getString(4),c.getString(5),c.getString(6)));
} while (c.moveToNext());
}
db.close();
return lst;
}
you don't need raw query method. i think that the android way is better in this case:
db.query(items, category, null, null, null, null, null);
than use the cursor how already is written in the other comment.

Categories

Resources