我想从数据库中检索所有事件名称,并希望将其添加到另一个活动中定义的Listview中。我有这个代码,但它是用于根据特定事件名称搜索数据。我想要检索所有这些
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "eventDB.db";
private static final String TABLE_EVENTS = "events";
public static final String COLUMN_ID = "id";
public static final String COLUMN_EVENTNAME = "eventname";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_QUANTITY = "quantity";
public static final String COLUMN_PRICE ="price";
public static final String COLUMN_DATE ="date";
public static final String COLUMN_TIME = "time";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EVENT_TABLE = "CREATE TABLE " +
TABLE_EVENTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_EVENTNAME
+ " TEXT," + COLUMN_TYPE + " TEXT,"+COLUMN_QUANTITY+" TEXT," +COLUMN_PRICE+ " TEXT,"+COLUMN_DATE + "TEXT," + COLUMN_TIME + "TEXT" + ")";
db.execSQL(CREATE_EVENT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
onCreate(db);
}
public void add_events(Event_Data event_data)
{
ContentValues values = new ContentValues();
values.put(COLUMN_EVENTNAME, event_data.getEventName());
values.put(COLUMN_TYPE, event_data.getType());
values.put(COLUMN_QUANTITY, event_data.getQuantity());
values.put(COLUMN_PRICE, event_data.getPrice());
values.put(COLUMN_DATE, event_data.getDate());
values.put(COLUMN_TIME, event_data.getTim());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_EVENTS, null, values);
db.close();
}
public Event_Data findevent(String eventname)
{
String query = "Select * FROM " + TABLE_EVENTS + " WHERE " + COLUMN_EVENTNAME + " = \"" + eventname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Event_Data event = new Event_Data();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
event.setID(Integer.parseInt(cursor.getString(0)));
event.setEventName(cursor.getString(1));
event.setType(cursor.getString(2));
event.setQuantity(cursor.getString(3));
event.setPrice(cursor.getString(4));
event.setDate(cursor.getString(5));
event.setTim(cursor.getString(6));
cursor.close();
} else {
event = null;
}
db.close();
return event;
}
public boolean deleteevent(String eventname)
{
boolean result = false;
String query = "Select * FROM " + TABLE_EVENTS + " WHERE " + COLUMN_EVENTNAME + " = \"" + eventname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Event_Data event = new Event_Data();
if (cursor.moveToFirst()) {
event.setID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_EVENTS, COLUMN_ID + " = ?",
new String[] { String.valueOf(event.getID()) });
cursor.close();
result = true;
}
db.close();
return result;
}
}
enter code here
public class Event_Data {
private int id;
private String name;
private String type;
private String price;
private String qunt;
private String dat;
private String tim;
public Event_Data() {
}
public Event_Data(int vid, String vname, String vtype, String vprice, String vqunat, String vdat, String vtim) {
this.id = vid;
this.name=vname;
this.type=vtype;
this.price=vprice;
this.qunt=vqunat;
this.dat=vdat;
this.tim=vtim;
}
public Event_Data(String vname, String vtype, String vprice, String vqunat, String vdat, String vtim) {
this.name=vname;
this.type=vtype;
this.price=vprice;
this.qunt=vqunat;
this.dat=vdat;
this.tim=vtim;
}
public void setID(int vid) {
this.id = vid;
}
public int getID() {
return this.id;
}
public void setEventName(String vname) {
this.name = vname;
}
public String getEventName() {
return this.name;
}
public void setQuantity(String vquant) {
this.qunt = vquant;
}
public String getQuantity() {
return this.qunt;
}
public void setType(String vtype)
{
this.type= vtype;
}
public String getType()
{
return this.type;
}
public void setPrice(String vprice)
{
this.price= vprice;
}
public String getPrice()
{
return this.price;
}
public void setDate(String vdate)
{
this.dat= vdate;
}
public String getDate()
{
return this.dat;
}
public void setTim(String vtime)
{
this.tim = vtime;
}
public String getTim()
{
return this.tim;
}
}
答案 0 :(得分:2)
String query = "Select * FROM " + TABLE_EVENTS + " WHERE " + COLUMN_EVENTNAME + " = \"" + eventname + "\"";
因为你使用双引号而不是使用单引号作为
String query = "Select * FROM " + TABLE_EVENTS + " WHERE " + COLUMN_EVENTNAME + " ='" + eventname + "'";
或使用android在SqliteDatabase类中具有的查询方法
db.query(TABLE_EVENTS ,null,COLUMN_EVENTNAME + " =?",new String[]{eventname },null,null,null);
if (cursor.getCount()>0&&cursor.moveToFirst()) {
event.setID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_ID)));
event.setEventName(cursor.getString(cursor.getColumnIndex(COLUMN_EVENTNAME)));
event.setType(cursor.getString(cursor.getColumnIndex(COLUMN_TYPE )));
event.setQuantity(cursor.getString(cursor.getColumnIndex(COLUMN_QUANTITY)));
event.setPrice(cursor.getString(cursor.getColumnIndex(COLUMN_PRICE)));
event.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE )));
event.setTim(cursor.getString(cursor.getColumnIndex(COLUMN_TIME )));
cursor.close();
}