'Error while inserting date and time in sql database table. I am getting error while trying to insert date and time in android studio sql database
Mainly the application I am buiding should be able to insert the title, description time and date the title is created. This is the database table I have created for inserting title, content, date and time for an journal application. While I was executing " the error as journal table column does not have a date " error occurred.
public class JournalDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "JOURNal_db";
private static final String DATABASE_TABLE = "Journal_table";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_CONTENT = "content";
private static final String KEY_DATE = "date";
private static final String KEY_TIME= "time";
public JournalDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String create = " CREATE TABLE "+DATABASE_TABLE+"("+KEY_ID+"INTEGER PRIMARY KEY,"+
KEY_TITLE+"TEXT,"+
KEY_CONTENT+"TEXT,"+
KEY_DATE+"TEXT,"+
KEY_TIME+"TEXT" +")";
Log.d("dbHarry","Query being run is : "+ create);
db.execSQL(create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
if (i >= i1)
return;
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
onCreate(db);
}
public void addJournal(Journal journal){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_TITLE,journal.getTitle());
contentValues.put(KEY_CONTENT,journal.getContent());
contentValues.put(KEY_DATE,journal.getDate());
contentValues.put(KEY_TIME,journal.getTime());
db.insert(DATABASE_TABLE,null,contentValues);
db.close();
}
public Journal getJournal(long id){
// select * from the databaseTable where id = 1
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DATABASE_TABLE,new String[]{KEY_ID,KEY_TITLE,KEY_CONTENT,KEY_DATE,KEY_TIME},KEY_ID+"?",
new String[]{String.valueOf(id)},null,null,null);
if(null != cursor) {
cursor.moveToFirst();
}
return new Journal(cursor.getLong(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
}
public List<Journal> getJournals(){
SQLiteDatabase db = this.getReadableDatabase();
List<Journal> allJournals = new ArrayList<>();
// SELECT * FROM DATABASE NAME
String query = " SELECT * FROM " + DATABASE_TABLE;
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()) {
do {
Journal journal = new Journal();
journal.setID(cursor.getLong(0));
journal.setTitle(cursor.getString(1));
journal.setContent(cursor.getString(2));
journal.setDate(cursor.getString(3));
journal.setTime(cursor.getString(4));
allJournals.add(journal);
} while (cursor.moveToNext());
}
return allJournals;
}
}
Blockquote
Blockquote
Solution 1:[1]
According to your log, there is no column named "date" in your table, check it please.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
