简体   繁体   中英

ios sqlite connect to database and retrieve value

I have an SQLite database which contains three columns: ID , Name , Dept . I have one main ViewController in which I have three fields: enter mobileNumber (a textField), Name (a label), and Dept (a label). When i enter first 4 digit of any number in the textfield, the two labels automatically show the relevant value from the database. i am getting output fine when i give a default value in database. what i need is without giving any default value in database i want to run my app directly when i enter any number it should show the value of Name and Dept to the label. My database contains more than 3000 records.

i am using this query to get data from database:

sqlStatement = [[NSString stringWithFormat:@"SELECT _id,Name,Dept FROM Codes
    WHERE _id=\'%@\'",idString.text]UTF8String];

I Assume

  1. You are running a query with the first 4 digits
  2. In case of more than a single match to the database, you are selecting one of the rows returned by the query

Things to try to debug this:

  1. Debug the query: Programmatically, write a query using 4 digits of a number that returns only one row. Check that the query returns what you expected according to your data source.
  2. Debug displaying information: Hardcode that query and show that you can display the Name and Dept. information.

Use LIKE Clause which will return relative outputs.

 sqlStatement = [[NSString stringWithFormat:@"SELECT _id,Name,Dept FROM Codes WHERE _id like '%%@'",idString.text]UTF8String];

It will return you matching results as per your id.

For more clarity on LIKE Clause please check this Link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM