In my android project ,I have successfully created an SQLite Database , Insertion operations are also successful.
I cannot retrieve the List into a method .
here is my calling method:
protected void getTheServerList() {
ServerManger info = new ServerManger(this);
try {
info.open();
servers = info.getData();
info.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "EXCEPTION !",
Toast.LENGTH_SHORT).show();
} finally {
}
}
and here is my SQLIte method
public List<String > getData() {
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_UNAME,
KEY_PASSWORD };
List<String> sItems=null;
Cursor c = ourDatabase.query(TABLE_NAME, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iUname = c.getColumnIndex(KEY_UNAME);
int iPass = c.getColumnIndex(KEY_PASSWORD);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//sItems.add(KEY_NAME);
result =c.getString(iName);
sItems.add(result);
}
c.close();
return sItems;
}
and I get an Exception
03-12 12:19:15.798: E/Database(390): close() was never explicitly called on database '/data/data/com.example.proj/databases/PROJ_DB'
I can retrieve the String , but not the- List
Please help !
public List<String > getData() {
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_UNAME,
KEY_PASSWORD };
List<String> sItems=new ArrayList<String> // define instance Here you create listview but not creat this instance
Cursor c = ourDatabase.query(TABLE_NAME, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iUname = c.getColumnIndex(KEY_UNAME);
int iPass = c.getColumnIndex(KEY_PASSWORD);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//sItems.add(KEY_NAME);
result =c.getString(iName);
sItems.add(result);
}
c.close();
return sItems;
}
initiate your list.. list is null
List<String> sItems=new ArrayList<String>();
Related
I have a table having two columns - name and symbol. all i want is to get the symbol corresponding to the name. I use this method. It's kind of returning nothing. Please check this out.
public String getSymbol(String name) {
String Symbol = "";
String[] columns = new String[] { "name", "symbol" };
Cursor c = getReadableDatabase().query(DATABASE_TABLE_NAME, columns, null,
null, null, null, null);
int iName = c.getColumnIndex("name");
int iSymbol = c.getColumnIndex("symbol");
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
if (c.getString(iName) == name) {
Symbol = Symbol + c.getString(iSymbol);
}
}
return Symbol;
}
Try this function.
public String getSymbol(String name)
{
try
{
String Symbol = "";
Cursor c = db.query(DATABASE_TABLE_NAME, columns, "name = ?" , new String[] {name}, null, null, null);
if(c.getCount != 0)
{
c.moveToFirst();
Symbol = String.valueOf(c.getString(c.getColumnIndex("symbol")));
c.close();
return Symbol;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
Instead of using c.getString(iName) == name while comparing, use c.getString(iName).equalsIgnoreCase(name)
I'm a noob to android and im am trying to query a sqlite table. I have read several tutorials and know how to query a table to return an entire column of values, but i can't seem to figure out how to query a just a single value from a column by string. Any help is greatly appreciated. here is my code:
My successful code to query and return values for entire column:
public String getValue() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iValue) + "\n";
}
return result;
}
My attempt to query column for single value by string:
public String getSingleValue(String aCoin) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
if (c != null){
c.moveToFirst();
result= c.getString(1);
return result;
}
return null;
}
Change this
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
to
String[] columns = new String[]{ KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
String result = "";
if (c != null){
c.moveToFirst();
result= c.getString(0);
return result;
}
return null;
If you want to only get the name column.
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 an activity and want to get data from my sqlite database. It works, but my activity only retrieves the last row and not the whole column. What can i do to get the whole column?
This is the code in my database:
public String getName() {
String[] columns = new String[]{ KEY_ROWID, KEY_NAME };
Cursor c = db.query(NAME_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast()) {
String name = c.getString(c.getColumnIndex(KEY_NAME));
c.moveToNext();
}
c.close();
return name;
}
This is the code in my activities to contact the database:
Database getdata = new Database(this);
getdata.open();
String data = getdata.getName();
getdata.close();
use cursor.moveToFirst() method example given below
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
list.add(cursor.getString(1));
list.add(cursor.getString(2));
list.add(cursor.getString(3));
list.add(cursor.getString(4));
list.add(cursor.getString(5));
list.add(cursor.getString(6));
} while (cursor.moveToNext());
}
See, As I said, Without seeing code I m helpless.. And after seeing your code..
You are returning only single String from that function getName() instead it return String[].
Note: this is only pseudo code.. Actual may be different..
public String[] getName() {
int i=0;
String[] columns = new String[]{ KEY_ROWID, KEY_NAME };
Cursor c = db.query(NAME_TABLE, columns, null, null, null, null, null);
String name[] = new String[c.getCount()];
c.moveToFirst();
while(!c.isAfterLast()) {
name[i] = c.getString(c.getColumnIndex(KEY_NAME));
c.moveToNext();
i++;
}
c.close();
return name;
}
And this one,
Database getdata = new Database(this);
getdata.open();
String data[] = getdata.getName();
getdata.close();
Update your code...
public String[] getName() {
String[] columns = new String[]{ KEY_ROWID, KEY_NAME };
int i=0;
Cursor c = db.query(NAME_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
String[] names = new String[c.getCount()];
while(!c.isAfterLast()) {
String name = c.getString(c.getColumnIndex(KEY_NAME));
names[i]=name;
c.moveToNext();
i++;
}
c.close();
return names;
}
Database getdata = new Database(this);
getdata.open();
String[] data = getdata.getName();
getdata.close();
i have two mehods, one to collect SQL data and one to put it into a listview.. it seems i can't get the array to form correctly in the SQL part so that it can be passed tot the listview.
SQL act:
public String[] getDataInArray() { // get data for list and return in array form
// TODO Auto-generated method stub
String[] columms = new String[]{ KEY_ROWID, KEY_NAME};
String[] return_colums = null;
Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int rowcount = 0;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
rowcount = rowcount + 1;
}
return return_colums;
}
And the listview act:
public ListView whiskeylist;
public String[] DataArryWhiskey;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig whiskeyrows = new DBConfig(this);
whiskeyrows.open();
DataArryWhiskey = whiskeyrows.getDataInArray();
whiskeyrows.close();
Toast.makeText(MainScreen.this, result, Toast.LENGTH_LONG).show();
whiskeylist = (ListView)findViewById(R.id.listofWhiskey);
whiskeylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , DataArryWhiskey));
// End db view of whiskey
}// end onCreate
I keeps on crashing, can anybody help met a bit? Thx in advance
Moved return_colums defination and declaration.
public String[] getDataInArray() { // get data for list and return in array form
// TODO Auto-generated method stub
String[] columms = new String[]{ KEY_ROWID, KEY_NAME};
Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int rowcount = 0;
String[] return_colums = new String[c.getCount()];
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
rowcount = rowcount + 1;
}
return return_colums;
}
Move whiskeyrows.close(); from onCreate() to onDestroy(). More details in this link