Android SQlite update query - android

i have an update query where i need to pass multiple parameter. Please guide how to do. Here in place of "id" i want multiple parameters for example name age.
//Code
ContentValues updateCountry = new ContentValues();
updateCountry.put("country_name", "United States");
db.update("tbl_countries", updateCountry, "id=?", new String[] {Long.toString(countryId)})

Try
ContentValues updateCountry = new ContentValues();
updateCountry.put("country_name", "United States");
db.update("tbl_countries", updateCountry, "id=? AND name=? AND age=?", new String[]{Long.toString(countryId),"name_value","age_value"});

Related

How to add more than 3 numbers in a contact in android programatically

I need to modify the contacts App contacts from my application. I am using the following way:
private void UpdateInsertContact()
{
Intent intentInsertEdit = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intentInsertEdit.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
// Add code here to insert extended data, if desired
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.PHONE, "7547874590");
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.NAME, contactName);
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, "7547874590");
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE, "7547874590");
// Sends the Intent with an request ID
startActivity(intentInsertEdit);
}
But this way i am able to add only 3 contacts. How do i add more than 3 conatcts.
yes you can add more than three contacts using Intent . but you have to use contentValues and add it to the ArrayList and pass it as an Extra through the intent, as shown below
Intent intentInsertEdit = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intentInsertEdit.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.NAME, contactName);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
//Filling data with phone numbers
ContentValues row = new ContentValues();
row.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row.put(Phone.NUMBER, "97");
row.put(Phone.TYPE, Phone.TYPE_MOBILE);
data.add(row);
ContentValues row2 = new ContentValues();
row2.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row2.put(Phone.NUMBER, "9746");
row2.put(Phone.TYPE, Phone.TYPE_WORK);
data.add(row2);
ContentValues row3 = new ContentValues();
row3.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row3.put(Phone.NUMBER, "974611");
row3.put(Phone.TYPE, Phone.TYPE_HOME);
data.add(row3);
ContentValues row4 = new ContentValues();
row4.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row4.put(Phone.NUMBER, "97461180");
row4.put(Phone.TYPE, Phone.TYPE_OTHER);
data.add(row4);
intentInsertEdit.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(intentInsertEdit);
this way you can add multiple numbers in the same contact, hope this answers your question :)

Android Intent to create Contact using StructuredName

I'm working on an app that create contact using Intent. Why using Intent you will ask : Because It's to let user modify the contact as they want before registering.
My problem is while I'm using StructuredName or StructuredPostal, for other CommonDataKinds it's working well. Here's my code:
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
ContentValues row1 = new ContentValues();
row1.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
row1.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "company");
row1.put(ContactsContract.CommonDataKinds.Organization.TITLE, "position");
data.add(row1);
ContentValues row3 = new ContentValues();
row3.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
row3.put(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_HOME);
row3.put(ContactsContract.CommonDataKinds.Website.URL, "website");
data.add(row3);
Here is StructuredPostal
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
data.add(row5);
i = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(i);
Due to this problem I momentarily did a patch :
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
i.putExtra(ContactsContract.Intents.Insert.POSTAL, "street, code city, country");
Someone knows what's wrong?
Thanks for answers :)
For the StructuredPostal add the type. And use a different mimetype. Do not add null values for the address fields like street etc.
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME);
data.add(row5);
StructuredName does not work for me either.
For using StructuredName in the Intent.INSERT you should add both Insert.Name and data row for StructuredName
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
Same goes for StructuredPostal. You need to add Insert.POSTAL and the StructuredPostal data row.
The Insert.Name is taken as the DISPLAY_NAME and the rest of the fields are used for StructuredName. The Insert.POSTAL is used as formatted address. This is why you need to provide both data.
To see how android processes your Intent, check method parseStructuredNameExtra in this file

Sqlite Update Query not working | Android

I am new to Android apps and trying to run Sqlite-query like this;
String query = "Select IsEnabled from Translation_Language ORDER BY TransLation_Language_ID";
Cursor Translations = SplashScreen.database.rawQuery(query,null);
and it is working. But i don't know how to update the same table. I have tried some methods but failed. Could anyone help me to resolve it???
Update query code
SplashScreen.database.update("Translation_Language", "IsEnabled", "TransLation_Language_ID", new String[] {"1", "8"});
I want to set IsEnabled property to 1 that have ID=8.
try this way:
ContentValues cv = new ContentValues();
cv.put("IsEnabled","1");
db.update("Translation_Language", cv, "TransLation_Language_ID=?", new String[] { "8" });

How can i show a pre populated postal address in contacts screen with street , city , zip on android

I am using following code for populating contacts in Android:
Intent addContactIntent = new Intent(Intent.ACTION_INSERT);
addContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME, "Store Name");
addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "1-869-270-9099");
addContactIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "postal address");*
But I also need to populate city, street and zip in postal address. How to populate these fields using the above code? Or any alternate way to do so.
Ok here is what i have done.
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
//Email
ContentValues row1 = new ContentValues();
row1.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
row1.put(Email.ADDRESS, "ADDRESSme");
data.add(row1);
//Website
ContentValues row2 = new ContentValues();
row2.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);
row2.put(Website.URL, "URLme");
data.add(row2);
//name
ContentValues row3 = new ContentValues();
row3.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
row3.put(StructuredName.DISPLAY_NAME, "NameMe");
data.add(row3);
//Address
ContentValues row4 = new ContentValues();
row4.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
row4.put(StructuredPostal.CITY, "CityMe");
row4.put(StructuredPostal.COUNTRY, "COUNTRYme");
row4.put(StructuredPostal.STREET, "STREETme");
row4.put(StructuredPostal.POSTCODE, "POSTCODEme");
data.add(row4);
//Phone
ContentValues row5 = new ContentValues();
row5.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row5.put(Phone.NUMBER, "NUMBERme");
data.add(row5);
Intent i = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(Insert.DATA, data);
But one thing i need to remind you is that Insert.DATA is available only from API level 11.So this solution only works for Android 3.0+.
If you guys have any better solution please let me know.

How can we add sms programatically in sim card in android

I have been trying to do this using content://sms/sim URI. But i can just access the data base, but not add to it. I actually want to implement 'copy to sim' functionality in my application.
This is my code snippet:
ArrayList listName=new ArrayList();
ArrayList listContactId=new ArrayList();
ArrayList listMobileNo=new ArrayList();
ArrayList listEmail=new ArrayList();
Uri simUri = Uri.parse("content://sms/sim");
Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null);
String[] coloumnName=new String[cursorSim.getColumnCount()];
for(int i=0;i<cursorSim.getColumnCount();i++)
{
coloumnName[i]= cursorSim.getColumnName(i);
Log.i("Coulmn name -------!!!!!----------------",coloumnName[i]);
}
while (cursorSim.moveToNext()) {
listName. add(cursorSim.getString(cursorSim.getColumnIndex("name")));
listContactId. add(cursorSim.getString(cursorSim.getColumnIndex("_id")));
listMobileNo. add(cursorSim.getString(cursorSim.getColumnIndex("number")));
listEmail.add(cursorSim.getString(cursorSim.getColumnIndex("emails")));
}
This just allows me to read messages.
When i try to insert data.
Code snippet:
//Insert
ContentValues values = new ContentValues();
// values.put("name", "One");
values.put("address", "1111111111");
values.put("body", "Its a great day. Thats is been expected since i was born");
values.put("date", "1312434417006");
Uri newRowUri = getContentResolver().insert(simUri , values);
09-02 17:35:07.209: ERROR/SmsProvider(1476): Invalid request: content://sms/icc
I have given necessary permissions in my manifest file.
Can anyone suggest me how to do it. I even want to know whether this can be done, are there enough permissions to do this also.
Thanks
Vaishnavi
to read an sms from the phone you can use the regular api
Cursor cursor = context.getContentResolver().query(
SMS_INBOX_CONTENT_URI,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
WHERE_CONDITION,
null,
SORT_ORDER);
now iterate through the cursor & get the message
you can use the SmsContentProvider to save the message in another folder.. (say the sim card)
ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
ofcourse there are some loose ends which needs to be tied up..
its important to remind you to be careful while using content providers check this link

Categories

Resources