I wanted to update/delete a user specified field for a given table set up in sqlite3 via JDBC. I have used PreparedStatements for this purpose.
Now the case 3: delete sql query has no syntax error but it doesn't delete the row.
And the case 2: update sql query has syntax error - I want it to accept a field, update value and the condition.
What am I doing wrong here?
[Edit] Here's the code segment: http://pastebin.com/8naiXZ2v
String temp;
switch (n){
case 1: // Insert
System.out.println("Okay!");
PreparedStatement inPst = con.prepareStatement("insert into " + dob.tbName + " (roll, branch, fname, lname, cgpa)" + " values(?,?,?,?,?)");
System.out.print("Roll: ");
inPst.setString(1, in.nextLine());
System.out.print("Branch: ");
inPst.setString(2, in.nextLine());
System.out.print("Name: ");
inPst.setString(3, in.nextLine());
System.out.print("Surname: ");
inPst.setString(4, in.nextLine());
System.out.print("CGPA: ");
inPst.setString(5, in.nextLine());
inPst.executeUpdate();
break;
case 2: // Update
System.out.println("Okay!");
System.out.println("Fields: roll\tbranch\tfname\tlname\tcgpa");
PreparedStatement upPst = con.prepareStatement("update " + dob.tbName + " set ? = ? where ? ;");
System.out.print("Enter field name: ");
temp = in.nextLine();
upPst.setString(1, temp);
System.out.print("Enter field value: ");
temp = in.nextLine();
temp = "'" + temp + "'";
upPst.setString(2, temp);
System.out.print("Enter condition: ");
temp = in.nextLine();
upPst.setString(3, temp);
upPst.executeUpdate();
break;
case 3: //Delete
System.out.println("Okay!");
System.out.println("Fields: roll\tbranch\tfname\tlname\tcgpa");
PreparedStatement delPst = con.prepareStatement("delete from " + dob.tbName + " where ? = ? ;");
System.out.print("Enter key field: ");
temp = in.nextLine();
delPst.setString(1, temp);
System.out.print("Enter key value: ");
temp = in.nextLine();
temp = "'" + temp + "'";
delPst.setString(2, temp);
delPst.executeUpdate();
System.out.println("Deleted!");
break;