Anyoneee Pliss Help, I Want To Show A Data From Database, But When I Run It Say's Error Outofboundsexception, Can Anyone Help Me? The Error Sign In Below
public ArrayList<Sma> getPoint(String name)
{
ArrayList <Sma> point = new ArrayList<Sma>();
String selectQuery = "SELECT latitude_sma, longtitude_sma FROM sma WHERE nama_sma ='" + name + "'"; >> FROM HERE I JUST WANT TO SHOW latitude_sma, and longtitude_sma IN A TEXTVIEW
open();
Cursor c = database.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
Sma sekolah = new Sma();
sekolah.setLatitude(c.getString(c.getColumnIndex(latitude_sma)));
sekolah.setLongitude(c.getString(c.getColumnIndex(longtitude_sma)));
point.add(sekolah);
}
while (c.moveToNext());
}
return point;
}
And here is my Activity
private void handleIntent(Intent intent)
{
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
dataSource.open();
String query = intent.getStringExtra(SearchManager.QUERY);
DBDataSource dataSource = new DBDataSource(this);
ArrayList<Sma> list = dataSource.getPoint(query);
TextView tv = (TextView) findViewById(R.id.txtQuery);
//WHEN I RUN THIS CODE BELOW, THE ERROR HAPPEND > IndexOutOfBounds
tv.setText(list.get(0).getLongitude().toString());
tv.setText(list.get(1).getLatitude().toString());
}
}
Can Anyone Help Me, Please, Thanks B4 :D
The problem is the way you are handling your cursor. You will eventually be trying to read column data beyond the last row of the cursor. Use the following idioms instead. (You are also not closing the cursor, which is a separate problem also taken care of by this sample.)
ArrayList <Sma> point = new ArrayList<Sma>();
Cursor cursor = null;
try {
cursor = database.rawQuery(...);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Sma sekolah = new Sma();
sekolah.setLatitude(cursor.getString(cursor.getColumnIndex(latitude_sma)));
sekolah.setLongitude(cursor.getString(cursor.getColumnIndex(longtitude_sma)));
points.add(sekolah);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return points;
You have simply no sure that you have two elements in your list.
try to check before you try to list.get(0) or list.get(1) it.
ArrayList<Sma> list = dataSource.getPoint(query);
if(list.size()>2){
TextView tv = (TextView) findViewById(R.id.txtQuery);
//WHEN I RUN THIS CODE BELOW, THE ERROR HAPPEND > IndexOutOfBounds
tv.setText(list.get(0).getLongitude().toString());
tv.setText(list.get(1).getLatitude().toString());
}
You can also think about changing your sql statement to sth like this
Cursor c = db.rawQuery("SELECT latitude_sma, longtitude_sma FROM sma WHERE nama_sma = ?", new String[] {name});
to check your data in sqlite db please follow it
# Switch to the data directory
cd /data/data
# Our application
cd de.vogella.android.todos
# Switch to the database dir
cd databases
# Check the content
ls
# Assuming that there is a todotable file
# connect to this table
sqlite3 yourDBName.db
Related
I got a redicioulous error.
My cursor wont fill with database entries.
The code i have right now is:
public HashMap<Integer, Team> getTeamsBasic(int leagueId) {
System.out.println("TEST!!!" + leagueId);
SQLiteDatabase db = dbHelper.getReadableDatabase();
HashMap<Integer, Team> teams = null;
System.out.println("TEST3!");
String query = "SELECT * FROM Teams WHERE leagueId = ?";
Cursor c = db.rawQuery(query, new String[] { Integer.toString(leagueId)});
System.out.println("TEST4!");
teams = new HashMap<Integer, Team>();
System.out.println("BOOL: " + c.moveToFirst());
if (c.moveToFirst()) {
System.out.println("TEST5!");
while (c.isAfterLast() == false) {
Team team = new Team();
System.out.println("TEST!6");
team.setId(c.getInt(c.getColumnIndex("id")));
team.setName(c.getString(c.getColumnIndex("name")));
team.setStadium(c.getString(c.getColumnIndex("stadium")));
System.out.println("TES2T!");
teams.put(team.getId(), team);
c.moveToNext();
}
c.close();
System.out.println("TEAMSIZE: " + teams.size());
}
return teams;}
The problem is, I KNOW FOR SURE my database.db is filled with the correct items.
As i have downloaded my DB from my phone, and checked it with the same query as my script does.
Except my script gets no output while me doing the same thing in DB Browser for SQLite i get a ton of output:
https://gyazo.com/2a14458f50fd5a836c55a383d65d0d65
Its the exact same query i use when doing it in android as doing it manually.
The leagueId = 1.
Also the output of the BOOL: = FALSE :(
Whats going (wr)on(g)?
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;
}
I'm trying to understand how to save result from SQL queries in android to an arraylist. Actually I find a few questions but none of them can answer why my code is wrong. So here is what I'm trying to do :
String query = null;
query = "SELECT * FROM users WHERE objectId = "+objectId+" AND serverName "+serverName;
SQLiteDatabase db;
// Insert results from query in database.
ArrayList <String[]> result = new ArrayList<String[]>();
ResultSet rs = db.execSQL(query);
int columnCount = rs.getMetaData().getColumnCount();
while(rs.next())
{
String[] row = new String[columnCount];
for (int i=0; i <columnCount ; i++)
{
row[i] = rs.getString(i + 1);
}
result.add(row);
}
And the error is saying that I cannot convert void to ResultSet.
Any suggestions how to fix that or which is the right way to do this.
Thanks in advance
Once your database is open for reading use db.query or db.rawQuery to get a cursor that can be used to create your ArrayList
cursor = db.rawQuery(<query>, null);
if(cursor.getCount() > 0)
{
cursor.moveToFirst();
//add to list here
}
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'
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.