如何从android中的sqlite数据库更新数据库中的行

时间:2017-11-08 06:39:43

标签: android mysql sqlite

我是Android开发的新手..我有一个登录和注册页面,通过它我收集用户的信息,如姓名电子邮件和手机号码,注册活动传递到finalsuccess后显示所有信息...我想更新3已经从数据库获取到finalsuccess活动的移动号码...在该活动中,我希望已经提取的号码再次更新到database..plz help..thanks提前..

我在SQliteDBhelperclass中的表

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

    public static final String TABLE_NAME = "profile";
    public static final String COLUMN_ID = "userid";
    public static final String COLUMN_FULLNAME = "fullname";
    public static final String COLUMN_EMAIL = "email";
    public static final String COLUMN_PASSWORD = "password";
    public static final String COLUMN_MOBILE = "mobile";
    public static final String COLUMN_RELATIVE_MOBILE = "mobile2";
    public static final String COLUMN_RELATIVE_MOBILE3 = "mobile3";
    public static final String COLUMN_RELATIVE_MOBILE4 = "mobile4";
    public static final String COLUMN_ADDRESS = "address";

    private static final String CREATE_TABLE_QUERY =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_FULLNAME + " TEXT, " +
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT, " +
                    COLUMN_RELATIVE_MOBILE + " TEXT, " +
                    COLUMN_RELATIVE_MOBILE3 + " TEXT, " +
                    COLUMN_RELATIVE_MOBILE4 + " TEXT, " +
                    COLUMN_ADDRESS + " TEXT, " +
                    COLUMN_MOBILE + " TEXT " + ")";

我的更新方法无效

 public StringBuffer getData(){

        String query="SELECT * FROM "+TABLE_NAME;

        SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
        Cursor cursor=sqLiteDatabase.rawQuery(query,null);

        StringBuffer stringBuffer=new StringBuffer();

        if (cursor!=null)
        {
            cursor.moveToFirst();
            do {

                String mobile=cursor.getString(cursor.getColumnIndex("mobile2"));
                stringBuffer.append(" "+mobile+ "\n");


            }while (cursor.moveToNext());
            cursor.close();
        }
        return stringBuffer;
    }


    public void updatedata(String mob) {
        SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put(COLUMN_RELATIVE_MOBILE,mob);


        sqLiteDatabase.update("TABLE_NAME",contentValues,"userid='5' and RELATIVE_MOBILE='mobile2'",null);


    }

这是onclick更新按钮

enter code here
     sqLiteDBHelper.updatedata(editText.getText().toString());
            StringBuffer stringBuffer=sqLiteDBHelper.getData();
            tvshow.setText(stringBuffer.toString());
            String op = tvshow.toString();



            Toast.makeText(LoginSuccessActivity.this, "Contacts updated", Toast.LENGTH_SHORT).show();


        }
    });

3 个答案:

答案 0 :(得分:0)

首先制作一个ContentValues cv = new ContentValues(); cv.put("Field1","Bob"); //These Fields should be your String values of actual column names cv.put("Field2","19"); cv.put("Field2","Male"); 对象:

sqLiteDatabase.update(TableName, cv, "_id="+id, null);

然后使用更新方法,现在应该可以使用:

ServletContextListener

答案 1 :(得分:0)

SQLiteDatabase db = this.getWritableDatabase();
 db.update("Put your table name here", contentValues, "put your unique column name" + "='" + id + "'", null);

在你的情况下语法是:

  ContentValues cv = new ContentValues();
  cv.put("COLUMN_MOBILE","Bob");
  db.update(TABLE_NAME, cv, COLUMN_ID + "='" + id + "'", null);

其中id是您传递的参数值。

答案 2 :(得分:0)

请使用以下代码替换您的代码

sqLiteDatabase.update(TABLE_NAME,contentValues,“userid ='5'和RELATIVE_MOBILE ='mobile2'”,null);