java - 每次单击查看按钮时,Android Studio SQLite 表都会重复
问题描述
我在数据库中添加了 2 个条目,如果我第一次单击查看按钮,它通常会显示 2 个条目。但是,如果我再次单击查看按钮,将有 4 个条目,然后是 6、8、10 等。即使查看时有 4、6、8、10、12 个条目,我使用了一个getCount
命令,它显示2这是正确的。所以数据库是正确的,所以这里的错误在某处,viewbtn
但我不知道它是什么。我该如何解决?
MainActivity.java
viewbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
res = myDb.getAllData();
if(res.getCount()==0){ //when there is no data show message..error message is depend on "showMessage"method
//show message
showMessage("Oops!","No entry, add an entry to see the list.");
return;
}
while(res.moveToNext()) {
// read and collect data in database in column
buffer.append("\n\n");
buffer.append("Date: " + res.getString(0) + "\n");
buffer.append("Time: " + res.getString(1) + "\n");
buffer.append("Name: " + res.getString(2) + "\n");
buffer.append("Surname: " + res.getString(3) + "\n");
buffer.append("ID: " + res.getString(4) + "\n");
buffer.append("--------------------------------------------------------------");
}
//show all data
showMessage("Entries",buffer.toString()); //show all data in list.list is depnd on "showMessage"method
}
});
数据库助手.java
package com.example.AppDraft3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatabaseHelper extends SQLiteOpenHelper {
private Context context;
public static final String DATABASE_NAME="Attendance.db";
public static final String TABLE_NAME="Attendance_table";
public static final String COL_1="DATE";
public static final String COL_2="TIME";
public static final String COL_3="NAME";
public static final String COL_4="SURNAME";
public static final String COL_5="ID";
private SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (DATE TEXT,TIME TEXT,NAME TEXT,SURNAME TEXT,ID INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
}
public Boolean verifyData(String TABLE_NAME, String id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE id = '" + id + "'";
Cursor res = db.rawQuery(query, null);
if (res.moveToFirst()){
res.close();
return true;
} else {
res.close();
return false;
}
}
public long insertData(String name,String surname,String id){
if (verifyData(TABLE_NAME, id)){
return 0;
}
// set the format to sql date time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
Date time = new Date();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, dateFormat.format(date));
contentValues.put(COL_2, timeFormat.format(time));
contentValues.put(COL_3, name);
contentValues.put(COL_4, surname);
contentValues.put(COL_5, id);
long result=db.insert(TABLE_NAME, null, contentValues);
return result;
}
public boolean verifyExist(String id){
if (verifyData(TABLE_NAME, id)){
return true;
} else {
return false;
}
}
public Cursor getAllData(){
//get all data
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select*from "+TABLE_NAME, null);
return res;
}
public Integer deleteData(){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME,null,null);
}
public Boolean singleDeleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res =db.rawQuery("Select * from " +TABLE_NAME+ " where id = ?", new String[] {id});
if (res.getCount() > 0) {
long resultDel = db.delete(TABLE_NAME, "id=?", new String[] {id});
if (resultDel == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
}
解决方案
缓冲区需要清空,否则您只是附加到它。我假设这是一个StringBuffer
public void onClick(View v) {
res = myDb.getAllData();
buffer = new StringBuffer();
:
:
}
推荐阅读
- npm - 如何使用纱线将全局安装的节点包链接到项目?
- r - DPLYR 无法在 Mac 上编译
- ckeditor - 我可以在 contenteditable=true 的元素上禁用 CKeditor 初始化吗?
- r - 抓取所需网页时,所有.txt文件的内容都是我选择的节点
- javascript - useEffect 获取 Firebase 数据
- matrix - 如何有效地创建具有向量元素对平均值的矩阵
- angular - 最后来自一堆无限的 Observable
- javascript - 如何等待 firebase 实时数据库读取的值?
- arrays - 如何在过程中使用数组
- c# - 尝试从“此处”REST API 获取令牌时客户端授权标头无效