Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error on tokens, variableDeclarator expected instead

SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
  cv.put(colDeptID, 1);
  cv.put(colDeptName, "Sales");
  db.insert(deptTable, colDeptID, cv);

  cv.put(colDeptID, 2);
  cv.put(colDeptName, "IT");
  db.insert(deptTable, colDeptID, cv);
                    db.close();

With this code, I am getting a red underline under the parenthesis after put in every line and getting this error: Syntax error on token "(", delete this token.

I also get an error that says: Syntax error on tokens, variableDeclarator expected instead. Any ideas? I've tried everything I know to do, and none of it worked.

Edit: I also get Syntax error on "close", identifier expected after this token

I copy and pasted this code from a tutorial and all the comments said it worked, but I've had quite a few problems out of it. Thanks for such quick replies.

like image 450
Hollis Scriber Avatar asked Jan 10 '13 20:01

Hollis Scriber


1 Answers

Only declarations, such as:

SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();

can be directly inside a class declaration. Non-declarations, such as:

  cv.put(colDeptID, 1);
  cv.put(colDeptName, "Sales");
  db.insert(deptTable, colDeptID, cv);

must be inside a method, constructor, initializer block, or static initializer block. In this case they should probably be in a constructor or method.

like image 144
Patricia Shanahan Avatar answered Oct 31 '22 17:10

Patricia Shanahan