3

I am learning how to create and insert values into SQLite DB. the application has a button and chronometer. clicking the button starts the timer and clicking it again stops it. On stop, the values are to be stored into the DB. I can see from the file explorer in Android Studio that the DB and the table is getting created. There is evidence in the Log to suggest the start and stop time is also getting captured, but the values are not getting inserted into the DB. I am pasting snippet of the code used to create the DB and then to insert the record. I am also pasing snippet of the log where the error is output. Please help.

   import android.content.ContentValues; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.content.Context; import android.util.Log; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class DBHelper extends SQLiteOpenHelper {

        public static final int DATABASE_VERSION = 1;
        public static final String DATABASE_NAME = "Feeds.db";
        public static String TIMESTAMP_START;
        public static String   TIMESTAMP_END;
        public static int _ID = 0;
        public static final String DATABASE_TABLE = "T_FEEDRECORDS";
        private static final String SQL_CREATE_ENTRIES = "CREATE TABLE T_FEEDRECORDS ( _ID  INTEGER primary key autoincrement,  TIMESTAMP_START TEXT, TIMESTAMP_END TEXT)" ;

        public DBHelper(Context context)
        {
            super(context,DATABASE_NAME, null, DATABASE_VERSION );
        }


        public void onCreate(SQLiteDatabase db)
        {
            Log.d("onCreate", "before DB Create");
            try {
                db.execSQL(SQL_CREATE_ENTRIES);
            } catch(SQLException ex){ Log.d("onCreate", "DB creation exception:"+ ex.getMessage()); }
            Log.d("onCreate", "after DB Create" + db.toString());
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {

            db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_TABLE);
            Log.d("onUpgrade", "dropping table");
             onCreate(db);
        }

        public void createFeedRecord (FeedRecords feedRecord){
            ContentValues feedContent = new ContentValues();
            feedContent.put(TIMESTAMP_START,  feedRecord.getStartTime());
            feedContent.put(TIMESTAMP_END,  feedRecord.getEndTime());
            try {
                SQLiteDatabase db = this.getWritableDatabase();
                Log.d("createFeedRecord values before insert", "start time : " + feedRecord.getStartTime() + " Timer end:" + feedRecord.getEndTime());
                db.insertOrThrow(DATABASE_TABLE,null, feedContent );
                db.close();
            } catch(SQLException ex) {Log.d("SQL Exdception in create feed record", "" + ex.getLocalizedMessage());}
            Log.d("createFeedRecord", "after content values");
        } 
}

LOG SNIPPET

02-04 09:57:43.287    2213-2213/? D/onClick......﹕ Timer Start time: 150204_095743
....
02-04 10:01:25.738    2213-2213/? D/onClick......﹕ Timer Stop time: 150204_100125
02-04 10:01:25.738    2213-2213/? D/inster Time Record﹕ after create Feed
02-04 10:01:25.754    1239-1293/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client

02-04 10:01:25.763    2213-2213/? D/createFeedRecord values before insert﹕ start time : 150204_095743 Timer end:150204_100125
02-04 10:01:25.763    2213-2213/? E/SQLiteLog﹕ (1) near "null": syntax error
02-04 10:01:25.763    2213-2213/? D/SQL Exdception in create feed record﹕ near "null": syntax error (code 1): , while compiling: INSERT INTO T_FEEDRECORDS(null) VALUES (?)
02-04 10:01:25.763    2213-2213/? D/createFeedRecord﹕ after content values

1 Answer 1

2

You need to assign

  public static String TIMESTAMP_START="TIMESTAMP_START";
  public static String TIMESTAMP_END="TIMESTAMP_END";

OR

Directly provide column name

  ContentValues feedContent = new ContentValues();
  feedContent.put("TIMESTAMP_START",  feedRecord.getStartTime());
  feedContent.put("TIMESTAMP_END",  feedRecord.getEndTime());
Sign up to request clarification or add additional context in comments.

2 Comments

duh!!!! Yes ofcourse!!! Removed the variables and directly provided the column names and it worked. Many thanks for your quick response!!!
Sorry. just learning how to use the tools on this site... I have accepted it now!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.