Android : Sqlite Database insert? - android

How to insert a string array values to a table?
1,1,null,null,9876543210,null,1,-1,3,null,null,Testing message,null,0,0,0
The above is the value in a String[]. I want to insert this to my database table.
Here's my code : -
while((st = reader.readLine()) != null)
{
try
{
splitArray = st.split("\\n+");
}catch(PatternSyntaxException e ) { }
for(String result_array : splitArray)
{
Toast.makeText(context, result_array, Toast.LENGTH_SHORT).show();
}
What's the SQL statement to do this?

i have been trying to do the same thing and just worked it out. you
want to do something similar to this.
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
Code
public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE = "date";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String BODY = "body";
public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;
ContentValues values = new ContentValues();
values.put(SMSHelper.ADDRESS, "+61408219690");
values.put(SMSHelper.DATE, "1237080365055");
values.put(SMSHelper.READ, 1);
values.put(SMSHelper.STATUS, -1);
values.put(SMSHelper.TYPE, 2);
values.put(SMSHelper.BODY, "SMS inserting test");
Uri inserted = getContentResolver().insert(Uri.parse("content:// sms"), values);

If you have a table with one column of type 'TEXT' and all you wish is to enter
those string but in one commit: then as far as I know you have to make an INSERT statement
for each of them, there is no way to insert all the string in only 1 INSERT statement (as long as you want each string to be a row in the table)
Read more

Related

Java Android about Hashmap

I am trying to retrieve from UpdateProduct (Hashmap) to be able to modify my items by clicking the button. I implemented void modifyItem, but I see it is missing something.
File DataBase.java
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NOM = "Produits.db";
private static final String TABLE_PRODUCT = "Produits";
private static final String COLONNE_ID = "_id";
private static final String COLONNE_NAMEPRODUCT = "_nameProduct";
public void UpdateProduct(HashMap<String,String> object, String newValue) {
if(object == null) return;
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLONNE_NAMEPRODUCT, newValue);
int rows = db.update(TABLE_PRODUCT, values, "_id = ?", new String[]{object.get("id")});
db.close();
}
// MainActivity
Here my method to retrieve Hashmap data from UpdateProduct**
public void modifyItem(View view) {
if (objectSelected == null) return;
**????** selectedProduct = objectSelected.get("product");
if (selectedProduct != null && !selectedProduct.isEmpty()) {
DataBase.UpdateProduct(selectedProduct);
}
}`
Tryning to solve my project problems

how to turn two sqlite tables into a JSON object

Is it possible to turn a two table(relational tabel) from sqlite in to a JSON object? I've googling but still cannot find a way to convert those table. So far, i've only manage to turn one table into JSON object. If it's possible, can you tell me how to do it? if it's not, can you give me an alternatives? thanks.
here's the code that turn one table to JSON object:
private JSONArray getResults()
{
Context context = this;
String myPath = String.valueOf(context.getDatabasePath("ekantin1.db"));// Set path to database
String myTable = DatabaseHelper.ORDER_TABLE_NAME;//Set name of table
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null );
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for( int i=0 ; i< totalColumn ; i++ )
{
if( cursor.getColumnName(i) != null )
{
try
{
if( cursor.getString(i) != null )
{
Log.d("TAG_NAME", cursor.getString(i) );
rowObject.put(cursor.getColumnName(i) , cursor.getString(i) );
}
else
{
rowObject.put( cursor.getColumnName(i) , "" );
}
}
catch( Exception e )
{
Log.d("TAG_NAME", e.getMessage() );
}
}
}
resultSet.put(rowObject);
cursor.moveToNext();
}
cursor.close();
Log.d("TAG_NAME", resultSet.toString() );
Intent pass_data = new Intent(this,BluetoothOut.class);
pass_data.putExtra("pindah",resultSet.toString());
startActivity(pass_data);
return resultSet;
}
}
And this is my table in my DatabaseHelper :
//tabel order
public static final String ORDER_TABLE_NAME="tb_order";
public static final String COL_1="ORDERID";
public static final String COL_2="USERID";
public static final String COL_3="PASSWORD";
public static final String COL_4="MEJA";
public static final String COL_5="TOPUP";
public static final String COL_6="SALDO";
//tabel lineitems
public static final String LINEITEMS_TABLE_NAME="tb_lineitems";
public static final String COL1 = "FOODID";
public static final String COL2 = "PRICE";
public static final String COL3 = "NUM";
public static final String COL4 = "RES";
public static final String COL6 = "ORDERID_FK";
table line items and orderid related to each other where orderid in tb_order as PK and orderid_fk in tb_lineitems as FK.
A good way to export data from your database is to use Gson, which is Google's Json serialization/deserialization library.
Fetch your Objects from your database like normally, and then use Gson to convert it into Json and export it.
Here is an example of how you could do it.
private void exportDatabase() {
// Create an instance of Gson.
Gson gson = new Gson();
// You can easily convert Objects into Json.
MyItem item = new MyItem();
String json = gson.toJson(item);
// Fetch your items from your database.
ArrayList<MyItems> items = database.getAll();
// Arrays are a bit harder to convert, but not very.
json = gson.toJson(items, new TypeToken<ArrayList<MyItems>>(){}.getType());
// Now export it to some easily copy-pasted location.
System.out.println(json);
}

NOT NULL constraint failed: Facture.Mode

I tried to insert data into table Facture but I received a Crash report on a piece of code :"NOT NULL constraint failed: Facture.Mode (code 1299)".
I didn't find a way ti fix it. Could someone please help me?
There are my logcat.
07-22 13:59:01.565 2471-2471/com.example.pc.myapplication E/SQLiteDatabase: Error inserting U4=100 U1=100 A4=aa Q5=5 Q4=null A3=aaa A5=aa P5=100 P3=100 P4=100 U2=100 Q1=1 P2=100 A1=aa Q2=2 U5=100 Fournisseur=test Q3=3 U3=100 Datefactu=2/07/2018 Numero=123 A2=aaa Mode=null P1=100
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: Facture.Mode (code 1299)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.pc.myapplication.DatabaseHelper.insertFacture(DatabaseHelper.java:178)
at com.example.pc.myapplication.facture1.onOKClick(facture1.java:103)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
There is my class facture.java :
public void onOKClick ( View v )
{
if (v.getId() == R.id.buttonfacok) {
EditText numero = (EditText)findViewById(R.id.Etnum);
EditText date = (EditText)findViewById(R.id.Etdat);
EditText fournisseur = (EditText)findViewById(R.id.TVfour);
EditText modepaiement = (EditText)findViewById(R.id.TVmode);
EditText article1 = (EditText)findViewById(R.id.A1);
EditText article2 = (EditText)findViewById(R.id.A2);
EditText article3 = (EditText)findViewById(R.id.A3);
EditText article4 = (EditText)findViewById(R.id.A4);
EditText article5 = (EditText)findViewById(R.id.A5);
EditText quantite1 = (EditText)findViewById(R.id.Q1);
EditText quantite2 = (EditText)findViewById(R.id.Q2);
EditText quantite3 = (EditText)findViewById(R.id.Q3);
EditText quantite4 = (EditText)findViewById(R.id.Q4);
EditText quantite5 = (EditText)findViewById(R.id.Q5);
EditText unite1 = (EditText)findViewById(R.id.U1);
EditText unite2 = (EditText)findViewById(R.id.U2);
EditText unite3 = (EditText)findViewById(R.id.U3);
EditText unite4 = (EditText)findViewById(R.id.U4);
EditText unite5 = (EditText)findViewById(R.id.U5);
EditText prix1 = (EditText)findViewById(R.id.P1);
EditText prix2 = (EditText)findViewById(R.id.P2);
EditText prix3 = (EditText)findViewById(R.id.P3);
EditText prix4 = (EditText)findViewById(R.id.P4);
EditText prix5 = (EditText)findViewById(R.id.P5);
String strnumero = numero.getText().toString();
String strdate = date.getText().toString();
String strfournisseur = fournisseur.getText().toString();
String strmodepaiement = modepaiement.getText().toString();
String strarticle1 = article1.getText().toString();
String strarticle2 = article2.getText().toString();
String strarticle3 = article3.getText().toString();
String strarticle4 = article4.getText().toString();
String strarticle5 = article5.getText().toString();
String strquantite1 = quantite1.getText().toString();
String strquantite2 = quantite2.getText().toString();
String strquantite3 = quantite3.getText().toString();
String strquantite4 = quantite4.getText().toString();
String strquantite5 = quantite5.getText().toString();
String strunite1 = unite1.getText().toString();
String strunite2 = unite2.getText().toString();
String strunite3 = unite3.getText().toString();
String strunite4 = unite4.getText().toString();
String strunite5 = unite5.getText().toString();
String strprix1 = prix1.getText().toString();
String strprix2 = prix2.getText().toString();
String strprix3 = prix3.getText().toString();
String strprix4 = prix4.getText().toString();
String strprix5 = prix5.getText().toString();
Facture f = new Facture();
f.setNumero(strnumero);
f.setDatefactu(strdate);
f.setFournisseur(strfournisseur);
f.setMode(strmodepaiement);
f.setA1(strarticle1);
f.setA2(strarticle2);
f.setA3(strarticle3);
f.setA4(strarticle4);
f.setA5(strarticle5);
f.setQ1(strquantite1);
f.setQ2(strquantite2);
f.setQ3(strquantite3);
f.setQ4(strquantite4);
f.setQ5(strquantite5);
f.setU1(strunite1);
f.setU2(strunite2);
f.setU3(strunite3);
f.setU4(strunite4);
f.setU5(strunite5);
f.setP1(strprix1);
f.setP2(strprix2);
f.setP3(strprix3);
f.setP4(strprix4);
f.setP5(strprix5);
helper.insertFacture(f);
}
}
}
There is my databasehelper :
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4 ;
private static final String DATABASE_NAME = "contacts.db" ;
private static final String TABlE_NAME = "contacts" ;
private static final String COLUMN_ID = "id" ;
private static final String COLUMN_NOMETPRENOM = "nometprenom" ;
private static final String COLUMN_CIN = "cin" ;
private static final String COLUMN_MOTDEPASSE = "motdepasse" ;
private static final String TABlE_NAME1 = "Argent" ;
private static final String COLUMN_ENTREE = "Entree" ;
private static final String COLUMN_DATE = "date" ;
private static final String TABlE_NAME2 = "Facture" ;
private static final String COLUMN_NUMERO = "Numero" ;
private static final String COLUMN_DATEFOU = "Datefactu" ;
private static final String COLUMN_FOURNISSEUR = "Fournisseur" ;
private static final String COLUMN_MODE = "Mode" ;
private static final String COLUMN_Article1 = "A1" ;
private static final String COLUMN_Article2 = "A2" ;
private static final String COLUMN_Article3 = "A3" ;
private static final String COLUMN_Article4 = "A4" ;
private static final String COLUMN_Article5 = "A5" ;
private static final String COLUMN_Quantite1 = "Q1" ;
private static final String COLUMN_Quantite2 = "Q2" ;
private static final String COLUMN_Quantite3 = "Q3" ;
private static final String COLUMN_Quantite4 = "Q4" ;
private static final String COLUMN_Quantite5= "Q5" ;
private static final String COLUMN_Unitaire1 = "U1" ;
private static final String COLUMN_Unitaire2 = "U2" ;
private static final String COLUMN_Unitaire3 = "U3" ;
private static final String COLUMN_Unitaire4 = "U4" ;
private static final String COLUMN_Unitaire5 = "U5" ;
private static final String COLUMN_Prix1 = "P1" ;
private static final String COLUMN_Prix2 = "P2" ;
private static final String COLUMN_Prix3 = "P3" ;
private static final String COLUMN_Prix4 = "P4" ;
private static final String COLUMN_Prix5 = "P5" ;
SQLiteDatabase db;
private static final String TABlE_CREATE = "create table contacts ( id integer primary key not null , nometprenom Text not null , cin Text not null , motdepasse Text not null);" ;
private static final String TABlE_CREATE1 = "create table Argent ( id integer primary key not null , date Text not null , Entree Text not null);" ;
private static final String TABlE_CREATE2 = "create table Facture ( id integer primary key not null , Datefactu Text not null , Numero Text not null , Fournisseur Text not null , Mode Text not null , A1 Text not null , A2 Text not null , A3 Text not null , A4 Text not null , A5 Text not null , Q1 Text not null , Q2 Text not null , Q3 Text not null , Q4 Text not null , Q5 Text not null , U1 Text not null , U2 Text not null , U3 Text not null , U4 Text not null , U5 Text not null , P1 Text not null , P2 Text not null , P3 Text not null , P4 Text not null , P5 Text not null);" ;
public DatabaseHelper(Context context)
{
super(context ,DATABASE_NAME , null , DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABlE_CREATE2);
this.db=db;
}
public void insertFacture(Facture f)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Facture" ;
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
values.put(COLUMN_NUMERO, f.getNumero());
values.put(COLUMN_DATEFOU , f.getDatefactu());
values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
values.put(COLUMN_MODE, f.getMode());
values.put(COLUMN_Article1, f.getA1());
values.put(COLUMN_Article2, f.getA2());
values.put(COLUMN_Article3, f.getA3());
values.put(COLUMN_Article4, f.getA4());
values.put(COLUMN_Article5, f.getA5());
values.put(COLUMN_Quantite1, f.getQ1());
values.put(COLUMN_Quantite2, f.getQ2());
values.put(COLUMN_Quantite3, f.getQ3());
values.put(COLUMN_Quantite4, f.getQ4());
values.put(COLUMN_Quantite5, f.getQ5());
values.put(COLUMN_Unitaire1, f.getU1());
values.put(COLUMN_Unitaire2, f.getU2());
values.put(COLUMN_Unitaire3, f.getU3());
values.put(COLUMN_Unitaire4, f.getU4());
values.put(COLUMN_Unitaire5, f.getU5());
values.put(COLUMN_Prix1, f.getP1());
values.put(COLUMN_Prix2, f.getP2());
values.put(COLUMN_Prix3, f.getP3());
values.put(COLUMN_Prix4, f.getP4());
values.put(COLUMN_Prix5, f.getP5());
db.insert(TABlE_NAME2,null, values);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query2 = "DROP TABLE IF EXISTS "+TABlE_NAME2 ;
db.execSQL(query2);
this.onCreate(db);
}
}
Thanks in advance.
Mode's value as per Error inserting U4=100 U1=100 A4=aa Q5=5 Q4=null A3=aaa A5=aa P5=100 P3=100 P4=100 U2=100 Q1=1 P2=100 A1=aa Q2=2 U5=100 Fournisseur=test Q3=3 U3=100 Datefactu=2/07/2018 Numero=123 A2=aaa Mode=null P1=100
is null, you have defined the table so that it cannot be null as per .... , Mode Text not null , .....
You either need to pass a Facture object, to the insertFacture method, that provides, or results in a non-null value being used (which could be to only set the ContentValue for the column if Mode is not null) or you need to remove the not null constraint from the column's definition.
Re comment
#crammeur "Mode" must not be null . I declared as string but no result
When you declare a variable it's value will be null (unless it's a primitive type).
One get around could be to use the following as the insertFacture method :-
public void insertFacture(Facture f) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Facture" ;
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
values.put(COLUMN_NUMERO, f.getNumero());
values.put(COLUMN_DATEFOU , f.getDatefactu());
values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
if (f.getMode != null ) {
values.put(COLUMN_MODE, f.getMode());
} else {
values.put(COLUMN_MODE,"No mode supplied");
}
values.put(COLUMN_Article1, f.getA1());
values.put(COLUMN_Article2, f.getA2());
values.put(COLUMN_Article3, f.getA3());
values.put(COLUMN_Article4, f.getA4());
values.put(COLUMN_Article5, f.getA5());
values.put(COLUMN_Quantite1, f.getQ1());
values.put(COLUMN_Quantite2, f.getQ2());
values.put(COLUMN_Quantite3, f.getQ3());
values.put(COLUMN_Quantite4, f.getQ4());
values.put(COLUMN_Quantite5, f.getQ5());
values.put(COLUMN_Unitaire1, f.getU1());
values.put(COLUMN_Unitaire2, f.getU2());
values.put(COLUMN_Unitaire3, f.getU3());
values.put(COLUMN_Unitaire4, f.getU4());
values.put(COLUMN_Unitaire5, f.getU5());
values.put(COLUMN_Prix1, f.getP1());
values.put(COLUMN_Prix2, f.getP2());
values.put(COLUMN_Prix3, f.getP3());
values.put(COLUMN_Prix4, f.getP4());
values.put(COLUMN_Prix5, f.getP5());
db.insert(TABlE_NAME2,null, values);
}
This would then use a value of No mode supplied should the value of the getMode method return null. Not necessarily the ideal fix though. The real fix would be ascertain why f.setMode(strmodepaiement); is returning null, which may well be due to the Facture's setMode method setting Mode to null (or not setting Mode at all).

Get String Array from Cursor

I have a SQLite Database with 45 different entries, each with:
public static final String TABLE = "Table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_HOUR = "hour";
public static final String COLUMN_WEEK = "week";
public static final String COLUMN_DAY = "day";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_COLOUR = "colour";
public static final String COLUMN_ROOM = "room";
now I want to read out all. I Do this with following:
public Cursor fetchAllSubject(){
Cursor mCursor = database.query(true, TABLE, new String[] {
COLUMN_ID, COLUMN_HOUR, COLUMN_WEEK, COLUMN_DAY, COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_COLOUR, COLUMN_ROOM},null
, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
In a other class I have this code to read all out:
dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();
Now I want to have for each entry an array with ID, Hour, week, ... but I have no idea how to do that.
My first try was following:
ArrayList<String> mo1h = new ArrayList<String>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)));
subjectList.moveToNext();
}
But everything is in mo1h, and I dont know how to devide it.
The best would be to have a String[] for each. Has anybody an Idea?
Thanks!
You can create on Bean class and then create one ArrayList (Collection class)
public class Bean
{
public Bean();
String id, hour, week, day, name, description, color, room;
}
now create list of Bean
ArrayList<Bean> mo1h = new ArrayList<Bean>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
Bean b = new Bean();
b.id = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
b.hour =subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
...
...
// all your column
mo1h.add(b);
}
Why not continue with your strategy, but instead use an ArrayList of String[]:
ArrayList<String[]> mo1h = new ArrayList<String[]>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
String[] toUse = new String[8];
toUse[0] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
toUse[1] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
toUse[2] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK));
toUse[3] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY));
toUse[4] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME));
toUse[5] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION));
toUse[6] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR));
toUse[7] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM));
mo1h.add(toUse);
subjectList.moveToNext();
}

Best way to access all the details of android contacts

I am writing an application to sync contact details with my server. For this I need to query Contacts using content provider multiple times as all the data is not present in one table. For example by using contact id, I have to query separately to phone, email and address tables for each contact which I feel is not much efficient. It would be really helpful if someone could point me out ways to get all the contact details in a single query. Thanks in advance :)
If you have the rawContactId you don't need multiple query. You can have a single query with Data.CONTENT_URI as uri and with a selection on your rawContactId.
The you have to loop with the resulting cursor to read the info. To know in wich column you need to see for a given row in the Data table you need to check the MIMETYPE
EDIT
private interface DataQuery {
public static final String[] PROJECTION = new String[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3, };
public static final int COLUMN_ID = 0;
public static final int COLUMN_MIMETYPE = 1;
public static final int COLUMN_DATA1 = 2;
public static final int COLUMN_DATA2 = 3;
public static final int COLUMN_DATA3 = 4;
public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1;
public static final int COLUMN_PHONE_TYPE = COLUMN_DATA2;
public static final int COLUMN_GIVEN_NAME = COLUMN_DATA2;
public static final int COLUMN_FAMILY_NAME = COLUMN_DATA3;
public static final String SELECTION = Data.RAW_CONTACT_ID + "=?";
}
final Cursor c = resolver.query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, new String[] { String.valueOf(rawContactId) }, null);
try {
while (c.moveToNext()) {
final long id = c.getLong(DataQuery.COLUMN_ID);
final String mimeType = c.getString(DataQuery.COLUMN_MIMETYPE);
uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
final String oldLastName = c.getString(DataQuery.COLUMN_FAMILY_NAME);
final String oldFirstName = c.getString(DataQuery.COLUMN_GIVEN_NAME);
//store them where you need
} else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
final int type = c.getInt(DataQuery.COLUMN_PHONE_TYPE);
final String cellPhone = c.getString(DataQuery.COLUMN_PHONE_NUMBEIR);
//store them where you need
}
}
} // while
} finally {
if (c!=null) c.close();
}
Please consider that I did not check the code: I do not have a compiler here. I hope it will be useful anyway
Data is a generic table that can hold any kind of contact data.
The kind of data stored in a given row is specified by the row's MIMETYPE value, which determines the meaning of the generic columns DATA1 through DATA15.
For example, if the data kind is Phone.CONTENT_ITEM_TYPE, then the column DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address. Sync adapters and applications can introduce their own data kinds.

Categories

Resources