I am trying to store a user inputted date on SQLite, however all I have managed to figure out is how to add the current date as set by the android device. I want the user to be able a date for expirydate and recieveddate, as opposed to them being taken from the device settings.
Here is my code..
package com.example.bash1.sqlitediss;
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 {
// Database name
public static final String DATABASE_STOCKDB = "Stock.db";
// Collum names
public static final String TABLE_NAME = "stock_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "Name";
public static final String COL_3 = "Datereceived";
public static final String COL_4 = "Expirydate";
public DatabaseHelper(Context context) {
super(context, DATABASE_STOCKDB, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (" + COL_1 + " INTEGER PRIMARY KEY," + COL_2 + " TEXT," + COL_3 + " DATE," + COL_4 + " DATE" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String Name, String Datereceived, String Expirydate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, Name);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
contentValues.put(COL_3, dateFormat.format(new Date()));
// contentValues.put(COL_3, Datereceived);
contentValues.put(COL_4, dateFormat.format(new Date()));
long result = db.insert(TABLE_NAME,null,contentValues);
if (result == -1 )
return false;
else
return true;
}
// Calls all data for the View Database Button
public Cursor getAllData (){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
Aucun commentaire:
Enregistrer un commentaire