0

please i am trying to check if an entry exists in an sqlite database column. and i am using java. here is what my code looks like

                    String name =JTextField.getText();
                    String sql0 = "select * from Objects where Description like " + name;
                    pStmt = conn.prepareStatement(sql0);
                    rs = pStmt.executeQuery();

                    if (rs.next()) {//if there is such entry...
                       System.out.println("there is an entry");
                    } else {//no such entry add the asset normally...
                         System.out.println("there is no such entry");

                    }

the code is just a test code for testing my sqlite query... name is the the entry to search for in the Description Column. whenever i run this i get an error saying no such column as the value i have stored as name. please i really need help on this thanks.

1 Answers1

2

I am not really familiar with sql in java but propably you need to use quotes to escape the string

Change

String sql0 = "select * from Objects where Description like " + name;

To

String sql0 = "select * from Objects where Description like '" + name + "'";

Another way is to use prepared staments

SteveL
  • 3,331
  • 4
  • 32
  • 57
  • thanks it worked! funny enough, i have been writing my sql strings the way i used it in this question before now and it has been working fine for me. dont really know what made this one not to work! – Noble Nweze Mar 22 '15 at 22:37
  • @NobleNweze ,maybe the column was not a varchar,I think numbers don't require to be surrounded with quotes. – SteveL Mar 22 '15 at 22:42