vendredi 2 janvier 2015

Android inserting duplicated SQLite entries

My android inserting duplicated entries into two rows for each entry that i insert, that makes each entry have two ids into the database


the data base handler java Class



public class DatabaseHandler extends SQLiteOpenHelper {
private static final String KEY_ID = "_id";
private static final String FIRST_NAME = "first_name";
private static final String SECOND_NAME = "second_name";
private static final String FIRST_PHONE = "first_phone";
private static final String SECOND_PHONE = "second_phone";
private static final String EMAIL = "email";
private static final String ADDRESS = "address";
private static final String DEPARTMENT = "department";
private static final String TITLE = "title";
private static final String DATABASE_NAME = "employees";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "contacts";

public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "create table " + TABLE_NAME + "("
+ KEY_ID + " Integer primary key autoincrement,"
+ FIRST_NAME + " text,"
+ SECOND_NAME + " text,"
+ FIRST_PHONE + " text,"
+ SECOND_PHONE + " text,"
+ EMAIL + " text,"
+ ADDRESS + " text,"
+ DEPARTMENT + " text,"
+ TITLE + " text" + ")";

try {
db.execSQL(CREATE_CONTACTS_TABLE);
}
catch(android.database.SQLException SE) {
SE.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableIfExists = "drop table if exists contacts";
db.execSQL(dropTableIfExists);

}


public void insert(
String fName,
String sName,
String fPhone,
String sPhone,
String email,
String address,
String department,
String title
) {
SQLiteDatabase db2 = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FIRST_NAME, fName);
values.put(SECOND_NAME, sName);
values.put(FIRST_PHONE, fPhone);
values.put(SECOND_PHONE, sPhone);
values.put(EMAIL, email);
values.put(ADDRESS, address);
values.put(DEPARTMENT, department);
values.put(TITLE , title);
getWritableDatabase().insert(TABLE_NAME,FIRST_NAME, values );
db2.insert(TABLE_NAME, null, values);
db2.close();
}
}


here am inserting data into the database over AddNewEmployee Java Class



public class AddNewEmployees extends Activity {

private EditText firstName, secondName, phone_1, phone_2, email, address, department, title;
private DataBaseHandler helper;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_new_employees);
helper = new DataBaseHandler(AddNewEmployees.this);




}

public void getContacts(){

firstName = (EditText) findViewById(R.id.enter_first_name);
secondName = (EditText) findViewById(R.id.enter_last_name);
phone_1 = (EditText) findViewById(R.id.phone_1);
phone_2 = (EditText) findViewById(R.id.phone_2);
email = (EditText) findViewById(R.id.email);
address = (EditText) findViewById(R.id.address);
department = (EditText) findViewById(R.id.department);
title = (EditText) findViewById(R.id.title);


}


@Override
protected void onPause() {
super.onPause();

getContacts();
try{ helper.insert(firstName.getText().toString(),
secondName.getText().toString(),
phone_1.getText().toString(),
phone_2.getText().toString(),
email.getText().toString(),
address.getText().toString(),
department.getText().toString(),
title.getText().toString());


}

catch(SQLiteException e)

{
Toast.makeText(getBaseContext(), e.getMessage(), 50000).show();
}




}
}


here the xml file



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">
<EditText
android:layout_weight="50"
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/enter_first_name"
android:hint="Enter first name"
/>
<EditText
android:layout_weight="50"
android:paddingTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/enter_last_name"
android:hint="Enter last name"
/>
</LinearLayout>

<EditText
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/phone_1"
android:hint="Enter the first phone number"
/>
<EditText
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/phone_2"
android:hint="Enter the second phone number"
/>

<EditText
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/email"
android:hint="Enter Email Address"/>

<EditText
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="@+id/address"
android:hint="Enter Address"
/>

<EditText
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/department"
android:ems="10"
android:layout_gravity="center_horizontal"
android:hint="Enter the employee department"

/>

<EditText
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:ems="10"
android:layout_gravity="center_horizontal"
android:hint="Enter the employee title"
/>

</LinearLayout>
</ScrollView>
</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire