我的DatabaseHandler类:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String LOG_TAG = "debugger";
//database version
private static final int DATABASE_VERSION = 1;
//database name;
private static final String DATABASE_NAME = "appdb";
private static final String TABLE_PROJECT = "projects";
//Project Table Columns
private static String PROJECT_ID = "Project_id";
private static final String PROJECT_NAME = "Name";
private static final String PROJECT_DESCRIPTION = "Description";
private static final String CLIENT_NAME = "Client_Name";
private static final String LOCATION = "Location";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(LOG_TAG, "Executing OnCreate");
createProjectTable(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void createProjectTable(SQLiteDatabase db){
String CREATE_PROJECT_TABLE = "CREATE TABLE " + TABLE_PROJECT + "("
+ PROJECT_ID + " INTEGER PRIMARY KEY, "
+ PROJECT_NAME + " TEXT, "
+ PROJECT_DESCRIPTION + " TEXT, "
+ CLIENT_NAME + " TEXT, "
+ LOCATION + " TEXT );";
Log.d(LOG_TAG, "Creating Project table --> " + CREATE_PROJECT_TABLE);
db.execSQL(CREATE_PROJECT_TABLE);
}
public void addProject(Project project){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PROJECT_NAME, project.getName()); // Contact Name
values.put(PROJECT_DESCRIPTION, project.getDescription()); // Contact Phone
values.put(CLIENT_NAME, project.getClient_name());
values.put(LOCATION, project.getLocation());
// Inserting Row
Log.d(LOG_TAG, "Inserting Project....");
db.insert(TABLE_PROJECT, null, values);
Log.d(LOG_TAG, "Inserted successfullyt....");
db.close(); // Closing database connection
}
public List<Project> getProjects(){
List<Project> projects = new ArrayList<Project>();
String selectQuery = "SELECT * FROM " + TABLE_PROJECT;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.getCount() != 0){
Log.d(LOG_TAG, "Projects found...");
if (cursor.moveToFirst()) {
do {
Project project = new Project();
project.setName(cursor.getString(1));
project.setClient_name(cursor.getString(2));
project.setDescription(cursor.getString(3));
project.setLocation(cursor.getString(4));
projects.add(project);
} while (cursor.moveToNext());
}
}
return projects;
}
public void updateProject(Project project){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PROJECT_NAME, project.getName()); // Contact Name
values.put(PROJECT_DESCRIPTION, project.getDescription()); // Contact Phone
values.put(CLIENT_NAME, project.getClient_name());
values.put(LOCATION, project.getLocation());
String where = PROJECT_ID + " = ? ";
String [] value = { String.valueOf(project.getProject_id()) };
try{
int count = db.update(TABLE_PROJECT, values, where , value);
Log.d(LOG_TAG, "Updated rows -> " + count);
}catch (Exception e) {
e.printStackTrace();
}
db.close();
}
}
在我的活动中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
dbHelper = new DatabaseHandler(this);
}
当用户点击更新按钮时,我会调用更新方法
dbHelper.updateProject(newProject);
在updateProject
中的dbHelper
方法中,count的值为0。
即使在update方法之后调用此方法,dbHelper.getProjects()
也会返回旧值。
我已经使用调试器检查了新值是否传递给updateProject
方法,但它们没有得到反映。方法addProject
运行正常。
Project class
public class Project implements Serializable{
private static final long serialVersionUID = 1L;
private int project_id;
private String name;
private String description;
private String client_name;
private String location;
public Project(){
}
public Project(int project_id, String name, String client_name,String description, String location){
this.project_id = project_id;
this.name = name;
this.description = description;
this.client_name = client_name;
this.location = location;
}
public int getProject_id() {
return project_id;
}
public void setProject_id(int project_id) {
this.project_id = project_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getClient_name() {
return client_name;
}
public void setClient_name(String client_name) {
this.client_name = client_name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
任何人都可以帮忙。
答案 0 :(得分:3)
试试这个......
String [] value = { Integer.toString(project.getProject_id()) };
答案 1 :(得分:1)
使用此 -
String[] value = new String []{ String.valueOf(project.getProject_id()) };
答案 2 :(得分:1)
项目标识属于int数据类型,因此您应该使用
int value = project.getProject_id();