Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve error code = 1, msg = near ",": syntax error in sqlite in android

I am m making a timesheet application using Android. I am a beginner and don't have much knowledge about database programming.

Below is my code for the database I'm using:

public class TsData{

    private static final String TS_DATABASE = "timesheetdb";
    private static final int DATABASE_VERSION = 1;
    private static final String TS_TABLE = "database";

    public static final String ROW_ID = BaseColumns._ID;
    public static final String PROJ_NAME = "projname";
    public static final String START_TIME = "stime";
    public static final String DATE = "date";
    public static final String STOP_TIME="";
    public static final String ACT_NAME = "actname";
    public static final String TOTAL_TIME = "ttime";

    private DBhelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDataBase;

    private static class DBhelper extends SQLiteOpenHelper{
        public DBhelper(Context context) {
            super(context, TS_DATABASE, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "CREATE TABLE " + TS_TABLE + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + ACT_NAME + " TEXT NOT NULL, "
                + DATE + " TEXT NOT NULL, " 
                + START_TIME + " TEXT NOT NULL, "
                + STOP_TIME + " TEXT NOT NULL" + ");";
            db.execSQL(sql);
        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TS_TABLE);
            onCreate(db);
        }
    }

    public TsData(Context c){
        ourContext=c;
    }

    public TsData open()throws SQLException{
        ourHelper = new DBhelper(ourContext);
        ourDataBase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourHelper.close();
    }

    public long insertEntry(String act_name2, String startDate, String startTime, String stopTime) {
        ContentValues values = new ContentValues();
        values.put(ACT_NAME, act_name2);
        values.put(DATE, startDate);
        values.put(START_TIME, startTime);
        values.put(STOP_TIME, stopTime);
        System.out.println("record inserted");
        return ourDataBase.insert(TS_TABLE, null, values);
    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ROW_ID, ACT_NAME, DATE, START_TIME, STOP_TIME};
        Cursor cr = ourDataBase.query(TS_TABLE, columns, null, null, null, null, null);
        String result="";

        int iRow=cr.getColumnIndex(ROW_ID);
        int iName=cr.getColumnIndex(ACT_NAME);
        int istartTime=cr.getColumnIndex(START_TIME);
        int istopTime=cr.getColumnIndex(STOP_TIME);
        int iDate=cr.getColumnIndex(DATE);

        for(cr.moveToFirst();!cr.isAfterLast();cr.moveToNext()) {
            result=result+cr.getString(iRow)+"  "
                +cr.getString(iName)+"  "
                +cr.getString(iDate)+"  "
                +cr.getString(istartTime)+"  "
                +cr.getString(istopTime)+"\n";
        }
        return result;
    }

    public void deleteEntry(long rowNo) {
        // TODO Auto-generated method stub
        ourDataBase.delete(TS_TABLE, ROW_ID + "=" + rowNo, null);
    }
}

but I am getting the following error when I insert the record in the table:

10-21 20:33:11.044: I/Database(6597): sqlite returned: error code = 1, msg = near ",": syntax error

I have double checked all the syntax but not getting any solution.

like image 946
user1763193 Avatar asked Jul 22 '26 16:07

user1763193


1 Answers

public static final String STOP_TIME=""; ??? Use some value for STOP_TIME.

or

CREATE TABLE   TS_TABLE  (  ROW_ID   INTEGER PRIMARY KEY AUTOINCREMENT, 
         ACT_NAME   TEXT NOT NULL, 
         DATE   TEXT NOT NULL,  
         START_TIME   TEXT NOT NULL );
like image 198
rahul Avatar answered Jul 25 '26 07:07

rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!