I use a string to populate a textview on an listview
String[] text1 = { "Afghanistan", "Algeria" ,"Fred"};
I want to replace the 3 strings in the string array with data from a database. I have tried the following
String text1[];
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAsset3();
int counter = 0;
while (c.moveToNext()) {
text1[counter]=c.getString(0);
counter++;
}
getAsset3 from DBAdapter
public Cursor getAsset3() throws SQLException
{
Cursor mCursor =
db.query(true, "SURVDAT", new String[] {KEY_SR1,KEY_SR2,KEY_SR3,KEY_SR4,KEY_SR5,KEY_SR6,KEY_SR7}, null, null,null, null, null, null);
if (mCursor != null) {
//mCursor.moveToFirst();
}
return mCursor;
}
When I run the app crashes saying Null PointerException
Any ideas where I'm going wrong?
Any help Appreciated
Mark
You didn't initialize text1 in your second snippet, so when you type text1[counter] = c.getString(0); you are trying to get counter'th index of null
You shall do something like
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAsset3();
String text1[] = new String[c.getCount()];
for(int i = 0; cursor.moveToNext(); ++i)
{
text1[i] = c.getString(0);
}
cursor.close();
Related
Here is the code in activity:
//query
final dbhelper helper = new dbhelper(this);
Cursor c = helper.query();
boolean exist =false;
if(c != null && c.moveToFirst()){
Log.d("atestdbChar1no",String.valueOf(c.getCount()));
int i=0;
while(c.isAfterLast()){
Log.d("atestdb1",String.valueOf(i++));
Log.d("atestdb2",String.valueOf(c.getInt(0)));
Log.d("atestdb3",c.getString(1));
Log.d("atestdb4",c.getString(2));
c.moveToNext();
}
//insert
ContentValues values = new ContentValues();
if(!finWords.equals("null")){
if(finWords.length()>index){
String selectWord = finWords.substring(index, index+1);
values.put("character", finChar);
values.put("word", selectWord);
helper.insert(values);
Here is the code in SQLiteOpenHelper:
public void insert(ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TBL_NAME, null, values);
Log.d("test", "dbinsert");
db.close();
}
public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
Log.d("test", "dbquery");
return c;
}
I can insert data into database
but I cannot query them
the logcat say the cursor index out of range
and just output some data for example just output with id 1,2,4,7 then force close the App
but I already insert 14 data
what wrong of my code?
You forgot to negate c.isAfterlast() and unless you pass null back you do not have to check for null cursor.
if(c.moveToFirst()){
Log.d("atestdbChar1no",String.valueOf(c.getCount()));
int i=0;
while(!c.isAfterLast()){
Log.d("atestdb1",String.valueOf(i++));
Log.d("atestdb2",String.valueOf(c.getInt(0)));
Log.d("atestdb3",c.getString(1));
Log.d("atestdb4",c.getString(2));
c.moveToNext();
}
I need to import a list of names from the SQLite Database (where it is stored ) in form of an array . Is this the right way to do it
Code for Database
public String queryAll() {
// TODO Auto-generated method stub
String [] columns = new String [] {KEY_NAME};
Cursor point = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iName = point.getColumnIndex(KEY_NAME);
for(point.moveToFirst();!point.isAfterLast();point.moveToNext()){
result = result + point.getString(iName);
}
return result;
Code to where I should import the data to
DBContact info = new DBContact (this);
info.open();
String data[] = info.queryAll();
info.close();
NewContact = (Button) findViewById(R.id.bAddContact);
I am a beginner , anything would help a lot.
Thank you
public String[] queryAll() {
String [] columns = new String [] {KEY_NAME};
Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (cursor != null) {
try {
final int nameColumnIndex = cursor.getColumnIndex(KEY_NAME);
List<String> names = new ArrayList<String>();
while (cursor.moveToNext()) {
names.add(cursor.getString(nameColumnIndex));
}
return names.toArray(new String[names.size()]);
} finally {
cursor.close();
}
}
return null;
}
I have to retreive a string from database using their id.I done thet retreival,But it is returning as some object format not as a string.Please help me..
My retreival place:
db.open();
for(int i=1;i<=5;i++)
{
if(i==1)
group1=db.getspintitle(1);
if(i==2)
group1=db.getspintitle(2);
if(i==3)
group1=db.getspintitle(3);
if(i==4)
group1=db.getspintitle(4);
if(i==5)
group1=db.getspintitle(5);
}
db.getspintitle() method:
public String getspintitle(int rowId)throws SQLException
{
String sumtotal="";
Cursor cursor1 = db.rawQuery(
"SELECT spin from spinner WHERE _id='rowId'", null);
sumtotal=cursor1.toString();
cursor1.close();
return sumtotal;
}
For your information, The below returns you the Cursor, not the simple String.
Cursor cursor1 = db.rawQuery("SELECT spin from spinner WHERE _id='rowId'", null);
To retrieve value from the cursor, you have to iterate through the cursor.
Now, here in your case, you can do as below:
Cursor cursor1 = db.rawQuery("SELECT spin from spinner WHERE _id='rowId'", null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToNext();
sumtotal= cursor.getString(0);
cursor.close();
}
String[] sumTotal= new String[cursor.getCount()]
while(cursor.moveToNext())
sumtotal[i] = cursor.getString(0);
Please help me with following, I have a database which contain strings ( e.g. fotonames ).
And I want to read entire fotonames column into an arraylist.
How can I do this?
public Cursor getfotoname() throws SQLException
{
String getRT = "SELECT fotonames from "+ TABLE_NAME+";";
Cursor mCur = sqldb.rawQuery(getRT, null);
return mCur;
}
Now when you call
Cursor mCursor=null;
mCursor= DatabaseObject.getfotoname();
ArrayList<WhateverTypeYouWant> mArrayList = new ArrayList<WhateverTypeYouWant>();
for(mCursor.moveToFirst(); mCursor.moveToNext(); mCursor.isAfterLast()) {
// The Cursor is now set to the right position
mArrayList.add(mCursor.getWhateverTypeYouWant(WHATEVER_COLUMN_INDEX_YOU_WANT));
}
Have a look at this tutorial will help you
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is where you can lear how to fetch all values from one column
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/3/#getrowasarray
or otherway is
fire query like this
public Cursor columnValues() throws SQLException{
// TODO Auto-generated method stub
Cursor mCursor = db.query(Course_Info_Table,
new String[] {Column1 , column2 },
null,null, null, null, null);
//Cursor mCursor = mDb.rawQuery("Select",null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
and receive it like
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list12 = new ArrayList<String>();
cursor = dbm.columnValueofCourse();
cursor.moveToFirst();
startManagingCursor(cursor);
for (int i = 0; i < cursor.getCount(); i++) {
String reciv = cursor.getString(cursor
.getColumnIndex("column1"));
String reciv3 = cursor.getString(cursor
.getColumnIndex("column2"));
list1.add(reciv);
list2.add(reciv3);
cursor.moveToNext();
}
i want to get the value from the cursor without the SimpleCursorAdapter part.here is the code
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2,KEY_CONTENT3};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
and the activity side code
cursor = mySQLiteAdapter.queueAll();
from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2, SQLiteAdapter.KEY_CONTENT3};
int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3};
cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
while(!cursor.isAfterLast())
{
String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
if(tilt.equals("LEFT"))
{
Log.v("LEFT",pkg);
}
else if(tilt.equals("RIGHT"))
{
Log.v("RIGHT",pkg);
}
cursor.moveToNext();
}
i am getting the pkg value correct.but i want to get the values directly from the cursor while removing SimpleCursorAdapter part the code doesn't work.
any help would be appreciated :)
You can get values without declaring adapters. Adapters are need if you want to show the data in the list widgets. So your can be the following:
cursor = mySQLiteAdapter.queueAll();
if (cursor == null) {
//check if there are errors or query just return null
}
if (cursor.moveToFirst()) {
while(!cursor.isAfterLast())
{
String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
if(tilt.equals("LEFT"))
{
Log.v("LEFT",pkg);
}
else if(tilt.equals("RIGHT"))
{
Log.v("RIGHT",pkg);
}
cursor.moveToNext();
}
}
First you must count how many rows are in your table. Then assign that count to variable and it use to create an array. For array size you can use that count variable. Then create a for loop and use that count variable for rounds. After create your query and assign values to your arrays. 100% worked!.
int sqlcount;
Cursor mFetch;
SQLiteDatabase mydb = openOrCreateDatabase("your database name", MODE_PRIVATE, null);
mydb.execSQL("create table if not exists customer(name varchar,email varchar)");
Cursor mCount = mydb.rawQuery("select count(*) from customer", null);
mCount.moveToFirst();
sqlcount = mCount.getInt(0);
mCount.close();
String cname[] = new String[sqlcount];
String cemail[] = new String[sqlcount];
for(int i=0;i<sqlcount;i++) {
mFetch= mydb.rawQuery("select name,email from customer", null);
mFetch.moveToPosition(i);
cname[i] = mFetch.getString(0);
cemail[i] = mFetch.getString(1);
}
mFetch.close();
Toast.makeText(MainActivity.this,String.valueOf(cname[0]),Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,String.valueOf(cemail[1]),Toast.LENGTH_SHORT).show();