Android : Working With SQLite Database / Getting Cursor Out Of Bounds Exception
Jun 30, 2009
I am having some trouble working with the SQLite database. I am able to create the database, add to it, and delete from it with no problem by following the Notepad examples, but I am trying to avoid duplicates and therefore want to check to see if an entry exists already.
Below is the code I have for testing to see if a game exists. I get the correct Log message of "No Games" when there are no games, as well as the correct value for column from getColumnIndex which is proven to me by the log printout telling me that column = 1.
The error message that I receive if I don't catch the exception says "Caused by android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
Where is Index -1 being requested if my call to getColumnIndex is returning 1? Code...
View 3 Replies
Jun 22, 2010
The following function is giving me an out of bounds exception...
public void count(){
SQLiteDatabase db = table.getWritableDatabase();
String count = "SELECT count(*) FROM table";
Cursor mcursor = db.rawQuery(count, null);
int icount = mcursor.getInt(0);
System.out.println("NUMBER IN DB: " + icount);}
It's meant to return the number of rows in the database. Anybody know whats wrong? am I perhaps doing this task the wrong way?
View 2 Replies
View Related
May 6, 2009
I am seeing the exception in 'adb logcat'.But I don't know if it is caused by my application or android platform.Can you please give me any idea how to troubleshoot this exception?
View 3 Replies
View Related
Aug 25, 2009
How do you populate a spinner from a cursor accessing an SQLite database?
View 2 Replies
View Related
Aug 5, 2010
I don't know why this is generating an ArrayOutOfBoundsException! It's likely I've overlooked something because I'm so tired but if someone could tell me what is wrong I'd appreciate it. Code...
View 2 Replies
View Related
Aug 22, 2010
I am developing an Android application where I get the exception while writing to the database: An exception occurred: android.database.sqlite.SQLiteException
But it doest say anything else. In general I find it very difficult to debug the exceptions.
I am using Eclipse to develop the application. Please help me in solving this issue..
View 2 Replies
View Related
Jul 14, 2010
I created a sql lite database with the following columns:
static final String dbName="demoDB";
static final String tableName="Employees";
static final String colID="EmployeeID";
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE "+tableName+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colName+" TEXT, "+colAge+" Integer);");
I want to select all the records in the database like this and display them in a gridview:SQLiteDatabase db=this.getWritableDatabase();Cursor cur= db.rawQuery("Select "+colName+", "+colAge+" from "+tableName, new String [] {});String [] from=new String []{DatabaseHelper.colName,DatabaseHelper.colAge};
int [] to=new int [] {R.id.colName,R.id.colAge};
SimpleCursorAdapter sca=new SimpleCursorAdapter(this,R.layout.gridrow,c,from,to);
GridView grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(sca);but i receive the following exception:java.lang.IllegalArgumentException: column '_id' does not exist.the db table does not have a column with name '_id'so what is wrong with this code
View 3 Replies
View Related
Mar 21, 2010
I get records from the system by quering a ContentResolver. I maintain the order of the items in the database. So I want to display the items in the order taken from my database.
How do I merge these two informations?
I am looking after an alternative way now. As what I ideally want is:
Get order of contacts by a custom order held in my database (this involves joining CR with my DB cursor, and doing an order by, later seams it's not possible with CursorJoiner) but there is more, if the join is not unique I want to sort by contact's name as last measure
Which is impossible using Cursor and Joiners, because of the missing feature of order bys, also I need to return a Cursor, as I will be using the in an ExpandableList
Also this translated to TSQL it would look like
select * from contactsdata
left join category on contactsdata.catid=category.id
order by category.pos asc, contact.display_name asc
So I am looking now after an alternative. I have in mind to load in a temporary DB table all data from CR, then do the query on the temporary table where I can join tables and do order bys? How does this sound to you?
View 1 Replies
View Related
Oct 20, 2010
In my activity, I have an AutoCompleteTextView that gets its contents from my custom adapter. I created my adapter by following this example.The adapter works so far, but I am getting so many errors on leaks and cursors that are not finalized.My question is: how do I close the db in runQueryOnBackgroundThread?
View 1 Replies
View Related
Mar 18, 2010
I am creating an application which tracks the users location using GPS, stores the longitude and latitude in a database using a content provider then output the first long and lat to a mapview. I am able to create the cursor using this line of code...
View 1 Replies
View Related
Nov 13, 2010
I have decided that I hate SQL and for that purpose I am trying to make a helper class that I can reuse over and over and never have to mess with it again but it isnt working!
Here is what i have right now:
CODE:.........
I have included several System.out.println() statements to help me debug the code and find problems so please ignore them. PrintDB is another test that I wrote that I am using to make sure everything is working. Now this is what I have written to make sure everything is working....
CODE:.....................
Running this code everything goes great and all the System.out.println() statements have the right information in them until it gets to the databaseView.setText(db.printTable(tableName));
part and it throws an exception
CODE:................
This has me so confused because that same line of code is used in the getColNames() method that was called several times before this and ran with no problem. Also if the table didn't exist wouldn't it have thrown an exception when I called insertRow()? I called that method 5 times in a row with not a single exception thrown! What is happening here?
Implement onCreate() in your DBOpenHelper. Put there content of the createTable() method.
Why would it be necessary to create the table in the onCreate() method? If I were to do this would I be able to use my existing method to do so and just call it from this method?
And the second why you don't use content providers and uri >concept which is already SQLless?
View 1 Replies
View Related
Nov 24, 2010
My application contains SQLite .My application some time getting the following error
CODE:.............
I am closing the db in Destroy() using DataClass.dh.db.close(); Why this Error occured.Give me the solutions for this.
View 1 Replies
View Related
Aug 23, 2010
I'm trying to deploy an application with an existing SQLite database.I've been reading though the examples that are posted but they are always missing some part of the class. I feel like I'm trying to bake muffins but no one told me to use baking powder.Can someone post a full database helper class for depoying an SQLite database on Android? Edit : Delete old code because it doesn't work.
View 2 Replies
View Related
Apr 12, 2010
I'm currently developing a Field-Service application that stores data in the local sqlite database on an android device. At some point, usually after completing the data collection rounds, the local sqlite db is to be synchronized to a remote sybase db on the server.Any suggestions as to how this could be achieved or engineered as a solution? Or even better, are there alternatives to synchronizing data in such an application?
View 1 Replies
View Related
Jun 24, 2010
I've got a query, (I'm using rawQuery())
SELECT * FROM <table>
I'm then storing what it returns using a cursor. From their what I want to do is, start at the first row so.. cursor.moveToFirst() then take each column , column by column and store its particular value in a variable. I then want to move onto the next row and do the same. So I guess my question is How would I get cursor to deal with multiple columns?
View 2 Replies
View Related
Jul 29, 2010
Im trying to get an object form my SQLite database in my Android application. I know that the contact is in the database because when I add it and try finding by ID, it returns the object. When I try finding by first and last name, though, it throws an exception and Im left with a null reference.
Here is the query:
CODE:..........
Ive also tried to find just by first name and just by last name. Same result as above.
The exception is an SQLite exception
no such column: aurelius: , while compiling: SELECT _id, last_name, first_name,
email_address, organization_name, label, label_position FROM contacts WHERE
first_name=marcus AND last_name=aurelius
And also happens when trying to select just first or last name
no such column: marcus: , while compiling: SELECT _id, last_name, first_name,
email_address, organization_name, label, label_position FROM contacts WHERE
first_name=marcus
View 1 Replies
View Related
Sep 21, 2009
In the API Demo, there is example on using Simple Cursor Adapter. However, it is using the MediaS tore cursor. Is there any method so that I can use other external cursor prepare by SQL database. I don't know what should I input to the from String.
View 2 Replies
View Related
Jul 1, 2010
Here is my code for my db class:
package one.two;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import java.util.ArrayList;
public class DBAdapter extends ListActivity
{public String status = "status";
public String id = "id";
public String arrival = "arrival";
public String destination = "destination";
public String ferry = "ferry";
private static String DB_PATH = "/data/data/one.two/databases/";
private static final String DATABASE_NAME = "ferry.db";
private static final String DATABASE_TABLE = "port";
public static Context context;
public Cursor c;
public static SQLiteDatabase DbLib;
//overloaded non-null constructor
public DBAdapter(Context context) {
DbLib = context.openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.CREATE_IF_NECESSARY,null);
System.out.println("OpenOrCreateDB Done");
} public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}//end constructor DatabaseHelper
// ---closes the database---
public void close() {
DBHelper.close();
}//end close()
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion){
}//end onUpgrade()
@Override
public void onCreate(SQLiteDatabase db){
}//end onCreate()
}// end class DatabaseHelper
private static DatabaseHelper DBHelper;
private static final int DATABASE_VERSION = 1;
public static ArrayList<String> getAllTitles(){
ArrayList<String> port = new ArrayList<String>();
Cursor c=null;
c = DbLib.query("port"
new String[] { "status", "id", "arrival",
"destination", "ferry" }, null, null,
null, null, null);
try {
if (c!=null) { // start - when there is at least 1 record
System.out.println("Cursor is NOT NULL");
int i =0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{// Debug Stm
System.out.println("Record No. "+i);
System.out.println(c.getString(0));
System.out.println(c.getString(1));
System.out.println(c.getString(2));
System.out.println(c.getString(3));
System.out.println(c.getString(4));
// Assign database cursor.records to arraylist
port.add(i,c.getString(0));
port.add(i,c.getString(1));
port.add(i,c.getString(2));
port.add(i,c.getString(3));
port.add(i,c.getString(4));
i = i + 1;
} } // end - where there is at least 1 record
} finally {
if (c!=null) {
c.close();
} } return port;
}//end getAllTitles()
public void open() {
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
DbLib = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} }//end class DBAdapter
My Arrival class
package one.two;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Arrival extends ListActivity
{ private ListView listView;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{ ArrayList<String> retList = new ArrayList<String>();
System.out.println("Start onCreate Function");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("In onCreate Function");
System.out.println("In of GetData");
DBAdapter db = new DBAdapter(this);
System.out.println("DB Open");
db.open();
System.out.println("DB Opened");
retList = getData();
System.out.println("Out of GetData");
// force count no. of records in table
// dump to check index
int cnt = 2;
int i=0;
for (i = 0; i<cnt; i++)
System.out.println(retList.toString());
listView = (ListView) findViewById(android.R.id.list);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.id.list, null, null, null);
this.setListAdapter(mAdapter);
// db.close();
} @SuppressWarnings("static-access")
public static ArrayList<String> getData()
{ ArrayList<String> items = DBAdapter.getAllTitles();
// titles ???? redundant
//ArrayList<String> titles = new ArrayList<String>(items);
System.out.println("Return a LIST titles");
return items;
} }
View 1 Replies
View Related
Jun 29, 2009
I been using custom adapters based on the BaseAdapter and I usually populate my data at the getView() funtion.
Now I'm trying to write my custom adapter for my database cursor which now will be based on a CursorAdapter.
I'm getting confused where I should do the populating work? Along with how do I implement my inflating and populating "smart/efficient code based on RomainGuy presentation."
View 8 Replies
View Related
Feb 13, 2009
I'm new to Android, I just finished the Notepad Tutorials, where we can create SQLite Databases to save notes. So I wanted to know if it was possible to "import" my own SQLite database (not created with my App but with a 3rd-party software) to my project, and if the answer is yes, where should I save my SQLite databse and how can I have access to it.
View 9 Replies
View Related
Mar 25, 2010
I'm doing a application that will use a database with serialized object. The objects will be already serialized and the user will just display things. On a PC I would just make an XML but with android I'm not sure. Can I make an XML and "unpack" it when the user launch the application for the first time or should I do something else?
View 3 Replies
View Related
Mar 5, 2010
I put my database field in "assets" folder. And use the code from this blog to copy the database to "/data/data/my_packname/databases/", (This copy code i run it in the onCreate() method when i run this app) then use select * from ... to get data. But it gives me the exception: no such table. Someone told me that if i am attempting to copy the file in SQLiteOpenHelper's onCreate(), it's too late. So the copy file code can not copy the complete file. So i need to use adb or ddms to pull the database first? So, Anyone can teach me how to use my own databse?
View 3 Replies
View Related
Mar 30, 2010
I am having this problem with the android database. I adopted the DBAdapter file the NotepadAdv3 example from the google android page.
CODE:...........
The error that i am receiving is from Log.e(TAG, e.toString()) in the methods retrieveKey() and storeKey() "no such table: myuserkey: , while compiling: SELECT userkey FROM myuserkey"
View 2 Replies
View Related
Nov 18, 2009
I have a class like class My data{ String name; int data; Location[] locality.
View 8 Replies
View Related
May 23, 2009
I am new to android and I have a problem in creating a database.
public class database extends ListActivity {
/** Called when the activity is first created. */
private final String MY_DATABASE_NAME = "myCoolUserDB.db";
private final String MY_DATABASE_TABLE = "t_Users";
Context c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> results = new ArrayList<String>();
setContentView(R.layout.main);
SQLiteDatabase mydb=null;
try
{
mydb.openOrCreateDatabase(MY_DATABASE_NAME, null);
} catch(Exception e){}
}}
When I run this code it throws a run time exception.
View 4 Replies
View Related
Mar 23, 2010
How do I delete the sqlite database exists in my android? There is a db behind my android qro and delete the same
View 4 Replies
View Related
Aug 2, 2010
public void delete(String id, String name, String phonenumber, String time, String message)
{
String[] args={id};
getWritableDatabase().delete("texts", "_ID=?", args);
}
It works fine, and deletes the specified row. However, I also want to implement it so that I can delete the most recent (earliest date) entry in the database, which should be at the top of the table. I know I can do this by altering the last 2 parameters of delete, but I cannot figure out what to change them to.
View 1 Replies
View Related
Jan 25, 2010
Is possible update the records from SQLite from my Android Application1 from other Application2 Android
View 4 Replies
View Related
Jul 31, 2009
Is there any UI can connect to SQLite database? I hope that I can read the data without writing code.is there any method can make it happened?
View 6 Replies
View Related
Jul 22, 2010
Is it really necessary to close an SQLite database that your activity has opened (database is in local memory or on SD card)? I would think it would be good practice, but I noticed that the Android samples such as the Notepad tutorial and SearchableDictionary sample do not do this. I've also seen sample code where the database is consistently opened, read from, and closed, but that would seem to add unnecessary overhead.
View 5 Replies
View Related