如何从android

时间:2016-02-18 09:25:46

标签: android android-database

我想从SQLiteDataBase删除行,但我不能,而且我没有正确填写ID!当选择删除按钮显示"Data Not Deleted" Toast消息时。

注意:请不要让我失望,不要给我负面评价。我在网上搜索但找不到合适的方法,我在这里提出了问题。请指导我,因为我是业余爱好者。

我的DataProvider代码:

public class DataProvider implements Serializable {
    private String id;
    private String name;
    private String family;
    private String password;
    private String phone;
    private String email;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public DataProvider(String id, String  name, String family, String password, String phone, String email) {

        this.id = id;
        this.name = name;
        this.family = family;
        this.password = password;
        this.phone = phone;
        this.email = email;
    }
}

我的SQLiteOpenHelder代码和删除查询:

public class UserDbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "UserInfoDB.db";
    private static final int DATABASE_VERSION = 1;

    private static final String CREATE_QUERY = "CREATE TABLE " + UserContract.NewUserInfo.TABLE_NAME + "(" +
            UserContract.NewUserInfo.USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            UserContract.NewUserInfo.USER_NAME + " TEXT," +
            UserContract.NewUserInfo.USER_FAMILY + " TEXT," +
            UserContract.NewUserInfo.USER_PASSWORD + " TEXT," +
            UserContract.NewUserInfo.USER_PHONE + " TEXT," +
            UserContract.NewUserInfo.USER_EMAIL + " TEXT);";

    public UserDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e("DATABASE OPERATION : ", "Database Create / Open...");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_QUERY);
        Log.e("DATABASE OPERATION : ", "Table Create...");
    }


    public void AddUserInfo(String name, String family, String password, String phone, String email, SQLiteDatabase db) {

        ContentValues contentValues = new ContentValues();
        contentValues.put(UserContract.NewUserInfo.USER_NAME, name);
        contentValues.put(UserContract.NewUserInfo.USER_FAMILY, family);
        contentValues.put(UserContract.NewUserInfo.USER_PASSWORD, password);
        contentValues.put(UserContract.NewUserInfo.USER_PHONE, phone);
        contentValues.put(UserContract.NewUserInfo.USER_EMAIL, email);

        db.insert(UserContract.NewUserInfo.TABLE_NAME, null, contentValues);
        Log.e("DATABASE OPERATION : ", "One row inserted...");
    }

    public Cursor getUserInfo(SQLiteDatabase db) {

        Cursor cursor;
        String[] projections = {UserContract.NewUserInfo.USER_NAME, UserContract.NewUserInfo.USER_FAMILY,
                UserContract.NewUserInfo.USER_PASSWORD, UserContract.NewUserInfo.USER_PHONE,
                UserContract.NewUserInfo.USER_EMAIL};
        String list_sort = UserContract.NewUserInfo.USER_ID + " DESC";

        cursor = db.query(UserContract.NewUserInfo.TABLE_NAME, projections, null, null, null, null, list_sort);
        return cursor;
    }

    public Cursor searchContacts(String user_name, SQLiteDatabase db) {

        String[] projections = {UserContract.NewUserInfo.USER_FAMILY, UserContract.NewUserInfo.USER_PASSWORD,
                UserContract.NewUserInfo.USER_PHONE, UserContract.NewUserInfo.USER_EMAIL};
        String selection = UserContract.NewUserInfo.USER_NAME + " LIKE ?";
        String[] selection_args = {user_name};

        Cursor cursor = db.query(UserContract.NewUserInfo.TABLE_NAME, projections, selection, selection_args, null, null, null);

        return cursor;
    }

    public Integer deleteUserInfo(String id, SQLiteDatabase db) {

        return db.delete(UserContract.NewUserInfo.TABLE_NAME, UserContract.NewUserInfo.USER_ID + " = ?", new String[]{id});
    }

    public Boolean login(String username, String password, SQLiteDatabase db) throws SQLException {

        Cursor cursor = db.rawQuery("SELECT * FROM " + UserContract.NewUserInfo.TABLE_NAME +
                " WHERE " + UserContract.NewUserInfo.USER_NAME + "=? AND " +
                UserContract.NewUserInfo.USER_PASSWORD + "=?", new String[]{username, password});

        if (cursor != null) {
            if (cursor.getCount() > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

发送数据的主要活动代码:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        DataProvider dataprovider = (DataProvider) listDataAdapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("share_data", dataprovider);
        Intent intent = new Intent(MainPage.this, UserPage.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
});

删除第二个活动中的按钮代码:

delete_btn = (FloatingActionButton) findViewById(R.id.user_delete_button);
delete_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        userDbHelper = new UserDbHelper(getApplicationContext());
        DataProvider dataprovider = (DataProvider) getIntent().getSerializableExtra("share_data");
        sqLiteDatabase = userDbHelper.getWritableDatabase();
        String USER_id = dataprovider.getId();
        Integer deleteRaw = userDbHelper.deleteUserInfo(USER_id, sqLiteDatabase);

        if (deleteRaw > 0){
            Toast.makeText(getApplicationContext(), "Data Deleted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Data Not Deleted", Toast.LENGTH_SHORT).show();
        }
    }
});

LogCat代码:

02-18 13:03:20.732 6124-6124/? I/art: Late-enabling -Xcheck:jni
02-18 13:03:20.860 6124-6124/com.tellfa.nikandroid.mytestdbproject D/ContextHelper: convertTheme. context->name=com.tellfa.nikandroid.mytestdbproject themeResourceId=2131296386
02-18 13:03:20.899 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001
02-18 13:03:20.917 6124-6124/com.tellfa.nikandroid.mytestdbproject D/PhoneWindowEx: [PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000
02-18 13:03:20.917 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000
02-18 13:03:20.963 6124-6124/com.tellfa.nikandroid.mytestdbproject E/DATABASE OPERATION :: Database Create / Open...
02-18 13:03:20.964 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001
02-18 13:03:20.972 6124-6124/com.tellfa.nikandroid.mytestdbproject D/PhoneWindowEx: [PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000
02-18 13:03:20.972 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000
02-18 13:03:21.080 6124-6124/com.tellfa.nikandroid.mytestdbproject I/CliptrayUtils: CliptrayUtils created!! mTextView = android.support.v7.widget.AppCompatEditText{3d035869 VFED..C. ......ID 0,0-0,0 #7f0c007a app:id/login_user_text}
02-18 13:03:21.086 6124-6124/com.tellfa.nikandroid.mytestdbproject I/CliptrayUtils: CliptrayUtils created!! mTextView = android.support.v7.widget.AppCompatEditText{383e51c6 VFED..C. ......ID 0,0-0,0 #7f0c007d app:id/login_pass_text}
02-18 13:03:21.133 6124-6154/com.tellfa.nikandroid.mytestdbproject D/OpenGLRenderer: Render dirty regions requested: true
02-18 13:03:21.134 6124-6154/com.tellfa.nikandroid.mytestdbproject I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013_msm8974_LA.BF.1.1.1_RB1__release_AU ()
                                                                                 OpenGL ES Shader Compiler Version: E031.25.03.00
                                                                                 Build Date: 01/21/15 Wed
                                                                                 Local Branch: mybranch7061829
                                                                                 Remote Branch: quic/LA.BF.1.1.1_rb1.7
                                                                                 Local Patches: NONE
                                                                                 Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013 +  NOTHING
02-18 13:03:21.139 6124-6154/com.tellfa.nikandroid.mytestdbproject I/OpenGLRenderer: Initialized EGL, version 1.4
02-18 13:03:21.167 6124-6154/com.tellfa.nikandroid.mytestdbproject D/OpenGLRenderer: Enabling debug mode 0
02-18 13:03:21.205 6124-6124/com.tellfa.nikandroid.mytestdbproject D/Atlas: Validating map...
02-18 13:03:21.562 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.562 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.563 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.563 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.613 6124-6124/com.tellfa.nikandroid.mytestdbproject D/CliptrayUtils: setInputTypeforClipTray(): 0
02-18 13:03:21.744 6124-6124/com.tellfa.nikandroid.mytestdbproject D/CliptrayUtils: setInputTypeforClipTray(): 0
02-18 13:03:21.782 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.782 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.782 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.782 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:21.783 6124-6124/com.tellfa.nikandroid.mytestdbproject I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@e76a338 time:19996341
02-18 13:03:22.184 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:22.185 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:22.185 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:22.185 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:26.818 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
02-18 13:03:26.846 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
02-18 13:03:27.042 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.042 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.042 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.042 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.097 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.097 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.097 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.097 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.868 6124-6124/com.tellfa.nikandroid.mytestdbproject I/art: Thread[1,tid=6124,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75cbeda8,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
02-18 13:03:27.880 6124-6124/com.tellfa.nikandroid.mytestdbproject E/MediaProfilesEx-JNI: register_com_lge_media_MediaProfilesEx
02-18 13:03:27.880 6124-6124/com.tellfa.nikandroid.mytestdbproject E/MediaRecorderEx-JNI: register_com_lge_media_MediaRecorderEx
02-18 13:03:27.881 6124-6124/com.tellfa.nikandroid.mytestdbproject D/AudioSystemEx: register_com_lge_media_LGAudioSystem
02-18 13:03:27.881 6124-6124/com.tellfa.nikandroid.mytestdbproject E/SurfaceControlEx: register_com_lge_view_SurfaceControlEx
02-18 13:03:27.881 6124-6124/com.tellfa.nikandroid.mytestdbproject I/art: Thread[1,tid=6124,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75cbeda8,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
02-18 13:03:27.881 6124-6124/com.tellfa.nikandroid.mytestdbproject D/LGMtpDatabaseJNI: register_android_mtp_LGMtpDatabase
02-18 13:03:27.882 6124-6124/com.tellfa.nikandroid.mytestdbproject I/art: Thread[1,tid=6124,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75cbeda8,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
02-18 13:03:27.883 6124-6124/com.tellfa.nikandroid.mytestdbproject D/LGMtpServerJNI: register_android_mtp_LGMtpServer
02-18 13:03:27.886 6124-6124/com.tellfa.nikandroid.mytestdbproject I/art: Thread[1,tid=6124,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75cbeda8,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
02-18 13:03:27.886 6124-6124/com.tellfa.nikandroid.mytestdbproject E/MediaPlayerEx-jni: register_com_lge_view_MediaPlayerEx
02-18 13:03:27.887 6124-6124/com.tellfa.nikandroid.mytestdbproject I/art: Thread[1,tid=6124,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75cbeda8,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
02-18 13:03:27.921 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:27.921 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:29.390 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
02-18 13:03:29.451 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
02-18 13:03:29.454 6124-6124/com.tellfa.nikandroid.mytestdbproject D/CliptrayUtils: setInputTypeforClipTray(): 0
02-18 13:03:30.545 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:30.545 6124-6124/com.tellfa.nikandroid.mytestdbproject D/BubblePopupHelper: isShowingBubblePopup : false
02-18 13:03:31.558 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
02-18 13:03:31.651 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
02-18 13:03:31.667 6124-6124/com.tellfa.nikandroid.mytestdbproject I/Timeline: Timeline: Activity_launch_request id:com.tellfa.nikandroid.mytestdbproject time:20006225
02-18 13:03:31.680 6124-6124/com.tellfa.nikandroid.mytestdbproject E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
02-18 13:03:31.681 6124-6124/com.tellfa.nikandroid.mytestdbproject D/ContextHelper: convertTheme. context->name=com.tellfa.nikandroid.mytestdbproject themeResourceId=2131296386
02-18 13:03:31.682 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001
02-18 13:03:31.684 6124-6124/com.tellfa.nikandroid.mytestdbproject D/PhoneWindowEx: [PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000
02-18 13:03:31.684 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000
02-18 13:03:31.704 6124-6124/com.tellfa.nikandroid.mytestdbproject E/DATABASE OPERATION :: Database Create / Open...
02-18 13:03:32.119 6124-6124/com.tellfa.nikandroid.mytestdbproject I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3443294b time:20006677
02-18 13:03:34.733 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
02-18 13:03:34.793 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
02-18 13:03:34.869 6124-6124/com.tellfa.nikandroid.mytestdbproject I/Timeline: Timeline: Activity_launch_request id:com.tellfa.nikandroid.mytestdbproject time:20009427
02-18 13:03:34.968 6124-6124/com.tellfa.nikandroid.mytestdbproject D/ContextHelper: convertTheme. context->name=com.tellfa.nikandroid.mytestdbproject themeResourceId=2131296386
02-18 13:03:34.996 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001
02-18 13:03:35.002 6124-6124/com.tellfa.nikandroid.mytestdbproject D/PhoneWindowEx: [PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000
02-18 13:03:35.002 6124-6124/com.tellfa.nikandroid.mytestdbproject I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000
02-18 13:03:35.241 6124-6124/com.tellfa.nikandroid.mytestdbproject E/DATABASE OPERATION :: Database Create / Open...
02-18 13:03:35.836 6124-6154/com.tellfa.nikandroid.mytestdbproject V/RenderScript: Application requested CPU execution
02-18 13:03:35.845 6124-6154/com.tellfa.nikandroid.mytestdbproject V/RenderScript: 0xaf412c00 Launching thread(s), CPUs 4
02-18 13:03:35.920 6124-6124/com.tellfa.nikandroid.mytestdbproject I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@16dd805c time:20010477
02-18 13:03:38.727 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
02-18 13:03:38.804 6124-6124/com.tellfa.nikandroid.mytestdbproject I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
02-18 13:03:38.805 6124-6124/com.tellfa.nikandroid.mytestdbproject E/DATABASE OPERATION :: Database Create / Open...
02-18 13:04:17.593 6124-6134/com.tellfa.nikandroid.mytestdbproject W/art: Suspending all threads took: 13.071ms

请帮帮我,因为我需要这个教程。 tnx适用于所有亲爱的&lt; 3

2 个答案:

答案 0 :(得分:1)

您没有从数据库中选择Cursor。因此,当您尝试从null获取id时,它将返回{{1}},然后您将该值传递给您的删除函数,并尝试删除ID为null的记录,并且没有任何记录,其id为null。所以什么都不会被删除。

当您从数据库中选择数据时,

将{{1}}添加到投影列表中,您就可以了。

答案 1 :(得分:0)

您可以传递参数@name来删除所需的行。

public synchronized static void  saveOrUpdate(MyMessage msg)
{
    if(msg.getId()==0)
        save(msg);
    else{
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = null;
        MyMessage temp=null;
        try{
            while(em.find(MyMessage.class, msg.getId())==null){
                System.out.println("-----------Object not found----------");
                Thread.sleep(3000);
            }
        tx=em.getTransaction();
        tx.begin();
        temp = em.merge(msg);
        tx.commit();
        em.close();

        msg.setClientId(temp.getClientId());
        msg.setId(temp.getId());
        msg.setMessage(temp.getMessage());
        msg.setReceiverId(temp.getReceiverId());
        msg.setSenderId(temp.getSenderId());
        msg.setStatus(temp.getStatus());
        System.out.println("Sucessfully saved/updated @ server "+temp);

        }catch(Exception e){System.out.println(e);if(tx!=null) tx.rollback();if(em!=null) em.close();}
    }      
}

public static void  save(MyMessage msg)
{
    EntityManager em = factory.createEntityManager();
    EntityTransaction tx = null;
       try{
       tx=em.getTransaction();
       tx.begin();
       em.persist(msg);
       tx.commit();
       em.close();
      System.out.println("Sucessfully saved @ server "+msg);
        }catch(Exception e){System.out.println(e);if(tx!=null) tx.rollback();if(em!=null) em.close();}
}