Android插入重复的SQLite条目

时间:2015-01-02 19:53:11

标签: android database sqlite duplicates

我的android将重复的条目插入到我插入的每个条目的两行中,这使得每个条目都有两个ID进入数据库

数据库处理程序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();    
    }     
}

这里是通过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();
           }




       }
        }

这里是xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>

2 个答案:

答案 0 :(得分:3)

看起来你的插入方法正在向数据库插入两次:

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);
    // remove or comment out this line here : getWritableDatabase().insert(TABLE_NAME,FIRST_NAME, values );
    db2.insert(TABLE_NAME, null, values);
    db2.close();    
}

答案 1 :(得分:1)

在处理程序的insert()方法中,您正在进行两次插入:

getWritableDatabase()。insert(TABLE_NAME,FIRST_NAME,values); db2.insert(TABLE_NAME,null,values);

选择一个并删除另一个。