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)
Related
How can we delete a single SMS from the inbox, I've tried the code below but it still erased all.
Cursor c = getApplicationContext().getContentResolver().
query(Uri.parse("content://sms/sent"), new String[] { "_id", "thread_id", "address", "person","date", "body" }, null, null, null);
try {
while (c.moveToNext()) {
int id = c.getInt(0);
String address = c.getString(2);
if(address.equals(address)){
getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id),null, null);}
}
}catch(Exception e){
}
#user3739864 Try this;
try {
while (c.moveToNext()) {
int id = c.getInt(0);
String address = c.getString(2);
if(id == // the id you want to delete ){
getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id),null, null);}
}
} catch(Exception e){
}
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>();
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 am trying to get my search values from the android browser with following code:
public void getBrowserHist() {
String[] mColumnStrings =
{ Browser.SearchColumns._ID,
Browser.SearchColumns.SEARCH,
Browser.SearchColumns.DATE
};
Cursor mCur = getContentResolver().query(Browser.SEARCHES_URI,
mColumnStrings, Browser.SearchColumns.SEARCH, null, null);
int url = mCur.getColumnIndex(Browser.SearchColumns.SEARCH);
Log.d(DEBUG_TAG, "Bookmarks count: " + mCur.getCount());
mCur.moveToFirst();
while (!mCur.isAfterLast()) {
Log.v("titleIdx", mCur.getString(url));
mCur.moveToNext();
}
}
The problem is that search result is empty. the mcur.getCount() method returns 0, and I donĀ“t now why. Does anyone have any suggestion?
Cursor cursor = this.context.getContentResolver().query(Browser.SEARCHES_URI, null, null, null, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
final int indexDate = cursor.getColumnIndex(Browser.SearchColumns.DATE);
final int indexTerm = cursor.getColumnIndex(Browser.SearchColumns.SEARCH);
String date = cursor.getString(indexDate);
String term = cursor.getString(indexTerm);
cursor.moveToNext();
}
}
cursor.close();
I am trying to get the address of the sender but I run into a little problem. If the person that sends the message is the first person in any of the conversations to send one, the query of the content://mms/inbox returns with zero rows?? but when someone sends any other mms message it will return back with the _id fine and i dont understand why the first one wont work right?
private String checkMmsMessages(Context context){
String address = "address";
Cursor curPdu = context.getContentResolver ().query(Uri.parse("content://mms/inbox"), null, null, null, null);
if(curPdu.moveToNext()){ //first MMS message curPdu.moveToNext() is false
String id = curPdu.getString (curPdu.getColumnIndex ("_id"));
Log.v("MMS", "ID1: " + id.toString());
Uri uriAddr = Uri.parse ("content://mms/" + id + "/addr");
Cursor curAddr = context.getContentResolver().query(uriAddr,null,"type=137",null,null);
if(curAddr.moveToNext()){
address = curAddr.getString (curAddr.getColumnIndex ("address"));
Log.v("MMS", "Address1: " + address.toString());
if(address.contentEquals("insert-address-token")){
Cursor curAddr2 = context.getContentResolver().query(uriAddr,null,"type=151", null,null);
if(curAddr2.moveToNext()){
address = curAddr2.getString(curAddr2.getColumnIndex("address"));
}
}
}
}
Log.v("MMS", address.toString());
return address;
}
Also something else that does not make sense is when when i have the phone plugged into the computer and step through the that section with the debugger, that problem does not happen and it gets the address every time.... it only happens when the phone is not connected, i just dont understand?
The problem was I was checking the database before the message was put into the database so I had to put a delay on the check
I think the issue is you are passing a value for selectionArgs instead of null to the query() method. I am not actually calling the mCursor's moveToNext() method in my code but instead I am implementing this logic in the getView() method of a SimpleCursorAdapter.
Uri uri = Uri.parse("content://mms-sms/conversations/" + mThreadId);
String[] projection = new String[] { "body", "person", "sub",
"subject", "retr_st", "type", "date", "ct_cls", "sub_cs",
"_id", "read", "ct_l", "st", "msg_box", "reply_path_present",
"m_cls", "read_status", "ct_t", "status", "retr_txt_cs",
"d_rpt", "error_code", "m_id", "date_sent", "m_type", "v",
"exp", "pri", "service_center", "address", "rr", "rpt_a",
"resp_txt", "locked", "resp_st", "m_size" };
String sortOrder = "normalized_date";
Cursor mCursor = getActivity().getContentResolver().query(uri,projection, null, null, sortOrder);
String messageAddress;
int type;
while (mCursor.moveToNext()) {
String messageId = mCursor.getString(mCursor.getColumnIndex("_id"));
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(messageId).appendPath("addr");
Cursor c = mContext.getContentResolver().query(builder.build(), new String[] {
"*"
}, null, null, null);
while (c.moveToNext()) {
messageAddress = c.getString(c.getColumnIndex("address"));
if (!messageAddress.equals("insert-address-token")) {
type = c.getInt(c.getColumnIndex("type"));
c.moveToLast();
}
}
c.close();
}
try this...
private String getAddressNumber(String id) {
String selectionAdd = new String("msg_id=" + id);
String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
Uri uriAddress = Uri.parse(uriStr);
Cursor cursor = getContentResolver().query(uriAddress, null, selectionAdd, null, null);
String phoneNum = null;
if (cursor.moveToFirst()) {
do {
String number = cursor.getString(cursor.getColumnIndex("address"));
if (number != null) {
boolean isNumberFormat = true;
try {
Long.parseLong(number.replace("-", ""));
phoneNum = number;
} catch (NumberFormatException e) { // ex) "insert-address-token"
// if (phoneNum == null) {
// phoneNum = number;
// }
isNumberFormat = false;
}
if (isNumberFormat)
break;
}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
return phoneNum;
}