当我单击“回收者列表”中的“特定列表项”时,我想从sqlite获取并更新数据。当我单击列表项并尝试更新打开的数据以及更新它时,问题就来了;它将列表中的先前数据和更新后的数据保留为空,例如[![更新后的图像] [1]] [1]
执行更新后,从sqlite删除数据无效;当我删除数据并按回去并再次打开时,数据仍然存在;但是当我在更新之前删除数据时,从列表以及从列表中删除的数据
这是我的更新活动
public class UpdateIMEIRec extends AppCompatActivity {
EditText IMEINoUpdate,IMEIIDUpdate;
Button BtnIMEIRecUpdate;
DatabaseHelper dbHelper;
String receivedIMEIId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_imeirec);
IMEIIDUpdate= (EditText) findViewById(R.id.imeiidupdate);
IMEINoUpdate= (EditText) findViewById(R.id.imeinumberupdate);
BtnIMEIRecUpdate= (Button) findViewById(R.id.imeibtnupdate);
dbHelper = new DatabaseHelper(this);
try {
//get intent to get person id
receivedIMEIId= getIntent().getStringExtra("IMEIID");
} catch (Exception e) {
e.printStackTrace();
}
User user= dbHelper.getIMEI(receivedIMEIId);
IMEIIDUpdate.setText(user.getImeiid());
IMEINoUpdate.setText(user.getImei());
BtnIMEIRecUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateIMEIFunction();
}
});
}
private void updateIMEIFunction() {
String imeiidupdate = IMEIIDUpdate.getText().toString().trim();
String imeinumberupdate = IMEINoUpdate.getText().toString().trim();
if(imeiidupdate.isEmpty()){
//error name is empty
Toast.makeText(this, "Enter IMEI ID", Toast.LENGTH_LONG).show();
}
if(imeinumberupdate.isEmpty()){
//error name is empty
Toast.makeText(this, "Enter IMEI Number", Toast.LENGTH_LONG).show();
}
/*User user= new User(imeiidupdate,imeinumberupdate);*/
User user= new User();
dbHelper.updateIMEI(receivedIMEIId, this, user);
FuncMoveToNextActivity();
}
private void FuncMoveToNextActivity() {
startActivity(new Intent(UpdateIMEIRec.this,IMEIListActivity.class));
}
}
这是我的数据库助手类
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 2;
Context context;
// Database Name
private static final String DATABASE_NAME = "DynamicERP.db";
public static final String table_imei = "IMEITABLE";
public static final String table_login= "USERLOGIN";
// User Table Columns names
public static final String imeiid = "IMEIID";
public static final String imei = "IMEI";
public static final String userid = "USERID";
public static final String username = "USERNAME";
public static final String password = "PASSWORD";
public static final String userrole = "USERROLE";
// create table sql query
private static final String DATABASE_CIMEI = "CREATE TABLE " + table_imei + "("
+ imeiid + " INTEGER, " + imei + " VARCHAR );" ;
private static final String DATABASE_CUSER = "CREATE TABLE " + table_login + "("
+ userid + " INTEGER, " + username + " VARCHAR, " + password + " INTEGER, " + userrole + " VARCHAR );" ;
// drop table sql query
private String DROP_IMEI_TABLE = "DROP TABLE IF EXISTS " + table_imei;
private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + table_login;
/**
* Constructor
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CIMEI);
db.execSQL(DATABASE_CUSER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop User Table if exist
db.execSQL(DROP_USER_TABLE);
// Create tables again
onCreate(db);
}
/**
* This method is to create user record
*
* @param user
*/
public void addUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(userid,user.getUserid());
values.put(username, user.getName());
values.put(password, user.getPassword());
values.put(userrole, user.getRole());
// Inserting Row
db.insert(table_login, null, values);
db.close();
}
public void addIMEI(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(imei,user.getImei());
values.put(imeiid, user.getImeiid());
// Inserting Row
db.insert(table_imei, null, values);
db.close();
}
/**
* This method is to fetch all user and return the list of user records
*
* @return list
*/
public List<User> getAllUser() {
// array of columns to fetch
String[] columns = {
userid,
username,
password,
userrole
};
// sorting orders
String sortOrder =
userid + " ASC";
List<User> userList = new ArrayList<User>();
SQLiteDatabase db = this.getReadableDatabase();
// query the user table
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id,user_name,user_email,user_password FROM user ORDER BY user_name;
*/
Cursor cursor = db.query(table_login, //Table to query
columns, //columns to return
null, //columns for the WHERE clause
null, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
sortOrder); //The sort order
// Traversing through all rows and adding to list
if (cursor.moveToFirst()) {
do {
User user = new User();
//user.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(userid))));
user.setUserid(cursor.getString(cursor.getColumnIndex(userid)));
user.setName(cursor.getString(cursor.getColumnIndex(username)));
user.setPassword(cursor.getString(cursor.getColumnIndex(password)));
user.setRole(cursor.getString(cursor.getColumnIndex(userrole)));
// Adding user record to list
userList.add(user);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
// return user list
return userList;
}
/**
* This method is to fetch all user and return the list of user records
*
* @return list
*/
public List<User> getAllImei() {
// array of columns to fetch
String[] columns = {
imeiid,
imei,
};
// sorting orders
String sortOrder =
imeiid + " ASC";
List<User> userList = new ArrayList<User>();
SQLiteDatabase db = this.getReadableDatabase();
// query the user table
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id,user_name,user_email,user_password FROM user ORDER BY user_name;
*/
Cursor cursor = db.query(table_imei, //Table to query
columns, //columns to return
null, //columns for the WHERE clause
null, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
sortOrder); //The sort order
// Traversing through all rows and adding to list
if (cursor.moveToFirst()) {
do {
User user = new User();
//user.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(userid))));
user.setImei(cursor.getString(cursor.getColumnIndex(imei)));
user.setImeiid(cursor.getString(cursor.getColumnIndex(imeiid)));
// Adding user record to list
userList.add(user);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
// return user list
return userList;
}
/**
* This method to check user exist or not
*
@param userid
* @return true/false
*/
public boolean checkUser(String userid) {
// array of columns to fetch
String[] columns = {
userid
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = userid + " = ?";
// selection argument
String[] selectionArgs = {userid};
// query user table with condition
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id FROM user WHERE imei = 'dynamic@imei.com';
*/
Cursor cursor = db.query(table_login, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}
/**
* This method to check user exist or not
*
* @param email
* @param password
* @return true/false
*/
public boolean checkUser(String email, String password) {
// array of columns to fetch
String[] columns = {
userid
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = userid + " = ?" + " AND " + password + " = ?";
// selection arguments
String[] selectionArgs = {email, password};
// query user table with conditions
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
*/
Cursor cursor = db.query(table_login, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}
/**
* This method to check user exist or not
*
* @param userid
* @param username
* @return true/false
*/
public boolean checkUserData(String userid, String username) {
// array of columns to fetch
String[] columns = {
userid
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = userid + " = ?" + " AND " + username + " = ?";
// selection arguments
String[] selectionArgs = {userid, username};
// query user table with conditions
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
*/
Cursor cursor = db.query(table_login, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}
public boolean checkUserData(String userid) {
// array of columns to fetch
String[] columns = {
userid
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = userid + " = ?";
// selection argument
String[] selectionArgs = {userid};
// query user table with condition
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id FROM user WHERE user_email = 'dynamic@data.com';
*/
Cursor cursor = db.query(table_login, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}
public User getUser(String id){
SQLiteDatabase db = this.getWritableDatabase();
String query= "SELECT * FROM "+table_login+ " WHERE userid="+ id;
//String query= "SELECT * FROM "+table_login+" WHERE "+imeiid +"='"+userid+"'";
Cursor cursor = db.rawQuery(query, null);
User userrecived = new User();
if(cursor.getCount() > 0) {
cursor.moveToFirst();
userrecived.setUserid(cursor.getString(cursor.getColumnIndex(userid)));
userrecived.setName(cursor.getString(cursor.getColumnIndex(username)));
userrecived.setPassword(cursor.getString(cursor.getColumnIndex(password)));
userrecived.setRole(cursor.getString(cursor.getColumnIndex(userrole)));
}
return userrecived;
}
public User getIMEI(String receivedIMEIId) {
SQLiteDatabase db = this.getWritableDatabase();
String query= "SELECT * FROM "+table_imei+ " WHERE imeiid="+ receivedIMEIId;
Cursor cursor = db.rawQuery(query, null);
User imeiuser = new User();
if(cursor.getCount() > 0) {
cursor.moveToFirst();
imeiuser.setImeiid(cursor.getString(cursor.getColumnIndex(imeiid)));
imeiuser.setImei(cursor.getString(cursor.getColumnIndex(imei)));
}
return imeiuser;
}
public void deletePersonRecord(String useridValue, Context mContext) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(table_login, userid + "=" + useridValue, null);
Toast.makeText(mContext, "Deleted successfully.", Toast.LENGTH_SHORT).show();
db.close();
}
public void deleteIMEIRecord(String imeiidValue, Context mContext) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM "+table_imei+" WHERE "+imeiid +"='"+imeiidValue+"'");
Toast.makeText(mContext, "Deleted successfully.", Toast.LENGTH_SHORT).show();
}
public void updateIMEI(String receivedIMEIId, UpdateIMEIRec updateIMEIRec, User user) {
SQLiteDatabase db = this.getWritableDatabase();
String strSQL = "UPDATE "+table_imei+ " SET "+imei+" = "+user.getImei()+"," +
" "+imeiid+" = "+user.getImeiid()+
" WHERE "+imeiid+" = "+receivedIMEIId;
db.execSQL(strSQL);
db.close();
}
public void updateUser(String receivedUSERId, UpdateUserRec updateUserRec, User user) {
SQLiteDatabase db = this.getWritableDatabase();
String strSQL = "UPDATE "+table_login+ " SET "+username+" = "+user.getName()+"," +
" "+password+" = "+user.getPassword()+","+userrole+" = "+user.getRole()+"" +
" WHERE "+userid+" = "+receivedUSERId;
db.execSQL(strSQL);
db.close();
}
}
这是我的模特班
public class User {
private int id;
private String userid;
private String name;
private String email;
private String password;
private String role;
private String imeiid;
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
private String imei;
public String getImeiid() {
return imeiid;
}
public void setImeiid(String imeiid) {
this.imeiid = imeiid;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
[1]: https://i.stack.imgur.com/UkMxO.png
答案 0 :(得分:1)
您可以使用以下方法更新数据库中的数据并获取返回状态。
public boolean updateUser(String receivedUSERId, UpdateUserRec updateUserRec, User user) {
if( user!=null && receivedUSERId!=null){
if(!TextUtils.isEmpty(user.getName())&&!TextUtils.isEmpty(user.getPassword())&&
!TextUtils.isEmpty(user.getRole())){
ContentValues cv = new ContentValues();
cv.put(username,user.getName());
cv.put(password,user.getPassword());
cv.put(userrole,user.getRole());
try{
SQLiteDatabase db = this.getWritableDatabase();
myDB.update(table_login, cv, userid+"="+receivedUSERId,null);
myDB.commit();
myDB.close();
return true;
} catch (Exception e) {
Log.e("Exception in update query", e.toString());
}
}
}
Log.e(TAG, "Invalid Data, Check your data first");
return false;
}