SQLite Exception no such column when trying to select

前端 未结 5 975
广开言路
广开言路 2020-11-27 20:21

I have the following DB helper class:

public int studentExists(String studid) {
    Cursor dataCount = mDb.rawQuery(\"select count(*) from usertable where \"         


        
相关标签:
5条回答
  • 2020-11-27 20:33

    You need to quote 'pb3874' (single quotes) if you are including it in the SQL string.

    0 讨论(0)
  • 2020-11-27 20:35

    for STRING you should place that in between '' ('sasas')

    select * from bio where email = 'sasas'
    
    0 讨论(0)
  • 2020-11-27 20:46

    Error was in trigger being fired on database update

    0 讨论(0)
  • 2020-11-27 20:51

    What's happening here is that SQLite thinks that 'pb3874' is actually a column name, rather than a string/text literal.

    To specify that it's a text literal, you'll want to ensure your text value is wrapped in the appropriate single quotes:

    To prevent SQL injection attacks, whenever you're taking input from the user, use a parameterized query:

    ("select count(*) from usertable where " + KEY_STUDID + "=?", studid);
    

    Without parameterization (very much discouraged when taking user input):

    ("select count(*) from usertable where " + KEY_STUDID + "='" + studid + "'", null);  
    

    The reason your numeric values didn't produce this: SQLite converted those numeric literals for you.

    0 讨论(0)
  • 2020-11-27 20:55

    I think you got the answer to what's wrong from Antlersoft or p.campbell, to format the query correctly, I would suggest, you do it like this :

    mDb.rawQuery("select count(*) from usertable where " + KEY_STUDID + "=?", studid);
    

    That should (1) solve your problem and (2) protect you from SQL injections

    Also, I am not sure that

    dataCount.getInt(0);
    

    is the best thing to do... you should probably use getColumnIndex to make sure you are getting the right data...

    0 讨论(0)
提交回复
热议问题