SQLite 数据库未更新
SQLite Database not getting updated
所以我创建了一个应用程序,用户可以在其中输入有关他看过的电影的详细信息,例如名称、演员表、评分...等等,这些详细信息存储在 table 名称中的数据库中在 DataBaseHelper class 中初始化为
public static final String TABLE_NAME = "table1";
在下面的代码段中,我创建了一个列表视图并显示了电影名称,每个名称前面都有一个复选框。如果勾选了复选框,则表示它是最喜欢的,否则不是最喜欢的...最初,如果电影是最喜欢的,则 table 中的列设置为“否”
当勾选并按下按钮时,我希望所有带有勾选的电影名称在数据库中更新为“是”。
DisplayActivity class 与列表视图
public class DisplayActivity extends AppCompatActivity {
DataBaseHelper myDb;
ListView movieNList;
Button addFavoritesB,button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
movieNList =(ListView) findViewById(R.id.moviesLV);
myDb=new DataBaseHelper(this);
addFavoritesB=(Button) findViewById(R.id.addButton);
button=(Button) findViewById(R.id.button);
ArrayList<String> theList=new ArrayList<>();
Cursor data=myDb.getData();
if (data.getCount()==0){
Toast.makeText(DisplayActivity.this,"The Database is empty",Toast.LENGTH_SHORT).show();
}else{
//Adds data to the list view
while(data.moveToNext()){
theList.add(data.getString(1));
Collections.sort(theList);
ListAdapter listAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice,theList);
movieNList.setAdapter(listAdapter);
}
}
buttonAction();
}
public void buttonAction(){
myDb.getWritableDatabase();
addFavoritesB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String itemSelected="Selected items : \n";
for (int i=0;i<movieNList.getCount();i++){
if (movieNList.isItemChecked(i)){
itemSelected += movieNList.getItemAtPosition(i)+"\n";
System.out.println(itemSelected);
myDb.updateFavorites(itemSelected,"yes");
}
}
}
});
}
DataBaseHelper中的方法class更新收藏夹栏
public boolean updateFavorites(String name,String favorites) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("FAVORITES", favorites);
Cursor res = db.rawQuery("select * from table1 where name=? ", new String[]{name});
if (res.getCount() > 0) {
long result = db.update(TABLE_NAME, contentValues, "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
当我这样尝试时,列不会更新....请帮助
假设 itemSelected
在 onClick()
侦听器中正确创建,我建议您使用像 "|"
这样的字符作为电影的分隔符而不是 "\n"
标题并从 itemSelected
.
的开头删除 "Selected items : \n"
同时将 myDb.updateFavorites(itemSelected,"yes");
移出 for
循环,这样 updateFavorites()
对所有选定的电影只调用一次:
public void buttonAction(){
myDb.getWritableDatabase();
addFavoritesB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String itemSelected="|";
for (int i=0;i<movieNList.getCount();i++){
if (movieNList.isItemChecked(i)){
itemSelected += movieNList.getItemAtPosition(i)+"|";
}
}
itemSelected += "|";
myDb.updateFavorites(itemSelected,"yes");
}
});
}
然后使用update()
方法在WHERE
子句中用运算符LIKE
更新table:
public int updateFavorites(String name, String favorites) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("FAVORITES", favorites);
return db.update(TABLE_NAME, contentValues, "? LIKE '%|' || name || '|%'", new String[] {name});
}
请注意,我将 updateFavorites()
的 return 类型从 boolean
更改为 int()
因为 db.update()
returns 更新的数量行。
所以我创建了一个应用程序,用户可以在其中输入有关他看过的电影的详细信息,例如名称、演员表、评分...等等,这些详细信息存储在 table 名称中的数据库中在 DataBaseHelper class 中初始化为
public static final String TABLE_NAME = "table1";
在下面的代码段中,我创建了一个列表视图并显示了电影名称,每个名称前面都有一个复选框。如果勾选了复选框,则表示它是最喜欢的,否则不是最喜欢的...最初,如果电影是最喜欢的,则 table 中的列设置为“否” 当勾选并按下按钮时,我希望所有带有勾选的电影名称在数据库中更新为“是”。
DisplayActivity class 与列表视图
public class DisplayActivity extends AppCompatActivity {
DataBaseHelper myDb;
ListView movieNList;
Button addFavoritesB,button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
movieNList =(ListView) findViewById(R.id.moviesLV);
myDb=new DataBaseHelper(this);
addFavoritesB=(Button) findViewById(R.id.addButton);
button=(Button) findViewById(R.id.button);
ArrayList<String> theList=new ArrayList<>();
Cursor data=myDb.getData();
if (data.getCount()==0){
Toast.makeText(DisplayActivity.this,"The Database is empty",Toast.LENGTH_SHORT).show();
}else{
//Adds data to the list view
while(data.moveToNext()){
theList.add(data.getString(1));
Collections.sort(theList);
ListAdapter listAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice,theList);
movieNList.setAdapter(listAdapter);
}
}
buttonAction();
}
public void buttonAction(){
myDb.getWritableDatabase();
addFavoritesB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String itemSelected="Selected items : \n";
for (int i=0;i<movieNList.getCount();i++){
if (movieNList.isItemChecked(i)){
itemSelected += movieNList.getItemAtPosition(i)+"\n";
System.out.println(itemSelected);
myDb.updateFavorites(itemSelected,"yes");
}
}
}
});
}
DataBaseHelper中的方法class更新收藏夹栏
public boolean updateFavorites(String name,String favorites) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("FAVORITES", favorites);
Cursor res = db.rawQuery("select * from table1 where name=? ", new String[]{name});
if (res.getCount() > 0) {
long result = db.update(TABLE_NAME, contentValues, "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
当我这样尝试时,列不会更新....请帮助
假设 itemSelected
在 onClick()
侦听器中正确创建,我建议您使用像 "|"
这样的字符作为电影的分隔符而不是 "\n"
标题并从 itemSelected
.
的开头删除 "Selected items : \n"
同时将 myDb.updateFavorites(itemSelected,"yes");
移出 for
循环,这样 updateFavorites()
对所有选定的电影只调用一次:
public void buttonAction(){
myDb.getWritableDatabase();
addFavoritesB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String itemSelected="|";
for (int i=0;i<movieNList.getCount();i++){
if (movieNList.isItemChecked(i)){
itemSelected += movieNList.getItemAtPosition(i)+"|";
}
}
itemSelected += "|";
myDb.updateFavorites(itemSelected,"yes");
}
});
}
然后使用update()
方法在WHERE
子句中用运算符LIKE
更新table:
public int updateFavorites(String name, String favorites) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("FAVORITES", favorites);
return db.update(TABLE_NAME, contentValues, "? LIKE '%|' || name || '|%'", new String[] {name});
}
请注意,我将 updateFavorites()
的 return 类型从 boolean
更改为 int()
因为 db.update()
returns 更新的数量行。