【问题标题】:How to Use Select Query in SQlite Android如何在 SQlite Android 中使用选择查询
【发布时间】:2016-09-09 14:16:49
【问题描述】:

我的应用程序中有一个数据库,我想根据当前日期从数据库中检索数据。我使用过此查询,但是当我单击检索按钮时,listView 中没有显示任何内容

Select * from BluetoothDevice where Date = current_date

public class UserDbHelper extends SQLiteOpenHelper {
public static final String DATABASENAME = "DATABASENAME2";
public static final int DB_VERSION = 1;


public UserDbHelper(Context context)
{
    super(context,DATABASENAME,null,DB_VERSION);


}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS BluetoothDevice ( Device VARCHAR , Address VARCHAR , Date VARCHAR);");

}
public List<BluetoothDevice>  getItemFromDatabase(SQLiteDatabase sqLiteDatabase) {
    List<BluetoothDevice> result = new ArrayList<>();
    // query database elements
    Cursor c = sqLiteDatabase.rawQuery("Select * from BluetoothDevice where Date = current_date;", null);

    while (c.moveToNext()) {
        Date date = new Date();
        date.setTime(Long.valueOf(c.getString(c.getColumnIndex("Date"))));
        result.add(
                new BluetoothDevice(
                        c.getString(c.getColumnIndex("Device")),
                        c.getString(c.getColumnIndex("Address")),
                        date

                )
        );
    }
    c.close();
    return result;
}

public void store(List<BluetoothDevice> data,SQLiteDatabase sqLiteDatabase) {
    for (BluetoothDevice value : data) {
        String s = String.valueOf(new Date().getTime());
        //insert in database
        sqLiteDatabase.execSQL("INSERT INTO  BluetoothDevice VALUES(?,?,?,?);", new String[]{value.name, value.address, s});

    }
}


@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

【问题讨论】:

标签: android sqlite android-sqlite


【解决方案1】:

您肯定需要将创建的表放入onUpgrade 方法中。

//Checks wether the old table has to be removed
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE BluetoothDevice");
    onCreate(db);
}

此外,您需要在对数据库进行的每次更改时增加您的 DB_VERSION

【讨论】:

  • 如何增加 DB_Version
  • 如果您的实际版本是1,则将其更改为2。以此类推
猜你喜欢
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
相关资源
最近更新 更多