mysql - SQL Syntax Problems -


im trying this:

string insertquery=" delete accounts username= " + username + ";"; 

but im getting error:

com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: unknown column 'sam' in 'where clause' 

its getting right username etc know testing, assume syntax wrong im getting no syntax errors?

the table called accounts. coloums username & password,

you missing single quotes. in case(it's string) variable need wrapped in them or it'll interpreted column.

string insertquery = "delete accounts username = '" + username + "'"; 


recommendation:

hence recommend use placeholders avoid kind of problem. don't forget care security(sql injection instance). it's worth parametrized statements more human-readable, safer , faster well.

i don't "hardcoded" queries. let's imagine scenario if had table ten columns , imagine how query in case: absolutely human-unreadable.

an usage of parametrized statements efficient , comfortable practise. code looks , becomes human-readable , "main" solution more safer , cleaner.

have @ preparedstatements. basic example:

private final string deletequery = "delete accounts username = ?";  public boolean deleteobject(string username) {    connection c = null;    preparedstatement ps = null;    try {       c = datasource.getconnection();       ps = c.preparestatement(deletequery);       ps.setstring(1, username); // numbering starts 1 not 0!       return ps.executeupdate() > 0;    }    catch (sqlexception ex) {       system.out.println("error in deleteobject() method: " + ex.getmessage());       return false;    }    {       if (c != null) {          try {             c.close();          }          catch (sqlexception ex) {             system.out.println("error in closing conn: " + ex.getmessage());          }       }    } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -