0

i create an alarm service and call it in main activity, but get this error that: "unable to start service"............. i create table in db.java and set variable by InsertMagnt in alarmservice.

DB DataBase = new DB();
DataBase.InsertMagnt(Acce_x, Acce_y, Acce_z, "", "", 1);
    DataBase.InsertMagnt(Meg_x, Meg_y, Meg_z, datetime, "", 2);
    DataBase.InsertMagnt(Tilt_x, Tilt_y, Tilt_z, datetime, Direct, 3);
    DataBase.InsertMagnt(lon, lat, 0, datetime, "", 4);

and main activity is:

Intent AlarmIntent = new Intent(MainActivity.this, AlarmService.class);
    pending = PendingIntent.getService(MainActivity.this, 0, AlarmIntent, 0);
    alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

    btnStart.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10 * 1000, pending);
            Toast.makeText(MainActivity.this, "Service Start", Toast.LENGTH_SHORT).show();
        }
    });

i get this errors:

08-08 13:24:21.334: E/AndroidRuntime(9000): java.lang.RuntimeException: Unable to start service com.example.farzaneh.AlarmService@417c9cd8 with Intent { flg=0x4 cmp=com.example.farzaneh/.AlarmService (has extras) }: android.database.sqlite.SQLiteException: unrecognized token: "')" (code 1): , while compiling: INSERT INTO Acce_Log (X,Y,Z,Date) VALUES (0.0,0.0,0.0')
nobalG
  • 4,544
  • 3
  • 34
  • 72

2 Answers2

0

What's going on with this insert statement

INSERT INTO Acce_Log (X,Y,Z,Date) VALUES (0.0,0.0,0.0')//date is not inserted and the '

You have specified that four values will be inserted but inserting only three,and that '.Remove it,and pass the date as the last argument

nobalG
  • 4,544
  • 3
  • 34
  • 72
0

Salam!

Your error is because of your mistakes about inserting a row into SQLite table (Look at your given error again!)

INSERT INTO Acce_Log (X, Y, Z, Date) VALUES (0.0, 0.0, 0.0 ')
                                    unrecognized token ----^

Please rectify your insert statement. Also note that SQLite does not provide any data type for storing date, you should store a date in either string or long format.

I recommend you to use long! use a Calendar object to convert your date into milliseconds since the epoch (in UTC timezone) and now its ready to be stored in SQLite

UPDATE #1

Suppose I have the following table in the SQLite database

column name      type
-------------------------
   GID          INTEGER
  Name           TEXT
  Quan          INTEGER
 Surname         TEXT

Now, I want to populate this table with some sample data, For this purpose I'll use the following function.

public void addRow() {
    ContentValues contentValues = new ContentValues();
    contentValues.put("GID", 123);
    contentValues.put("Name", "Edward");
    contentValues.put("Quan", 12);
    contentValues.put("Surname", "Kenway");

    mDataBase.insert("Your table name", null, contentValues);
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • salam, i created date in acce_log by text:'database.execSQL("CREATE TABLE IF NOT EXISTS Acce_Log (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," + "X DOUBLE, Y DOUBLE, Z DOUBLE,Date TEXT)");' and insert variable by database.execSQL("INSERT INTO Acce_Log (X,Y,Z,Date) VALUES" + " (" + X + "," + Y + "," + Z + ",'" + date + "')"); – farzaneh abolqasemi Aug 08 '14 at 10:22
  • @farzanehabolqasemi Please tell me what's type of `date` object ? – frogatto Aug 08 '14 at 10:27
  • @farzanehabolqasemi Why didn't you use `ContentValues` ? Its very straightforward also its much better that concatenating string together. If you okay with it tell me to add some sample codes to my answer – frogatto Aug 08 '14 at 10:32
  • should i write contentvalues instead TEXT? i do not know about this!! – farzaneh abolqasemi Aug 08 '14 at 10:36
  • @farzanehabolqasemi don't worry,keep calm !!! please have a look at http://stackoverflow.com/questions/15384550/android-sqlite-contentvalues-not-inserting or http://stackoverflow.com/questions/18971434/how-to-insert-value-in-data-base-using-sqlite-in-android – frogatto Aug 08 '14 at 10:38
  • @farzanehabolqasemi If you found that links messed-up tell me to put some simple examples – frogatto Aug 08 '14 at 10:43
  • i dont understand :(, i writed according to this sample but get error again, can you get me any example? thanks for your responses – farzaneh abolqasemi Aug 08 '14 at 10:54
  • @farzanehabolqasemi Hold on, I'll added some examples – frogatto Aug 08 '14 at 10:58