0

i try to retrieve data from sqlite with this code:

String sql = "select * from "+Table +" where "+C_LoginName+"=" +user; 
Log.d("CREATE DATABASE", "SQL: " + sql);   
Cursor c = db.rawQuery(sql, null);  
c.moveToFirst();  
Log.d("Password is", c.getString(c.getColumnIndex(C_Password)));  

but this error shown to me ("no such column....") i even use single quot(') side every string but not different.but when i use select * from table the loginName is there. how can solve that? tnx

4

2 回答 2

2

Quotes missing around user. Also, make sure C_LoginName contains a valid column name.

String sql = "select * from "+Table +" where "+C_LoginName+"='" +user + "'";
于 2013-08-03T15:54:34.270 回答
0

no such column in sqlite android java

This kind of error is usually thrown:

  • When you forgot to wrap column's value into single quotes (if you are not using parametrized statements)
  • You specified icorrect column name(check your database schema)

Recommendation:

I don't like your approach. It's dirty, not very human-readable and very unsafe. I suggest you to use parametrized statements with placeholders where each placeholder will be replacted with value from specified array.

Example:

String query = "select * from tablename where name = ?";
db.rawQuery(query, new String[] {name}); // ? will be replaced with name value

Note:

Usually if you are dealing with database it's very good and handy practise to create some class for example called Const that will hold your column names as final static variables.

This provides more efficient and cleaner dealing with database and you'll avoid typo problems (which are too hard to find out).

于 2013-08-03T15:59:36.743 回答