简体   繁体   English

Android:从sqlite数据库检索数据

[英]Android: Retrieve data from sqlite Database

I currently I have my database set up like the following: 目前,我已经按照以下方式设置了数据库:

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LOCKERNUMBER
                    + " TEXT NOT NULL, " + KEY_STATUS + " INTEGER, " + KEY_PIN
                    + " INTEGER);");

And I am trying to write a method to get the pin code from the column for a specific locker number. 我正在尝试编写一种方法来从列中获取特定储物柜编号的密码。 Any ideas? 有任何想法吗? I am very new I would like think I would need to use the query function and a cursor. 我很新,我想我需要使用查询功能和游标。 I just one to get the integer value and store it into an int variable so I can compare the pin codes from what the user types in to the one in the database. 我只是获得整数值并将其存储到一个int变量中,所以我可以将用户输入的密码与数据库中的密码进行比较。

Queries to database returns in a Cursor object . 查询数据库返回的Cursor object You should use the db.query() method to get a row(s). 您应该使用db.query()方法来获取一行。 Pass the table name, an array of columns you want to get (or null if you want all of them), pass a selection string that should be like "id = ?" 传递表名(要获取的列的数组)(如果需要所有列,则为null),传递选择字符串,该字符串应类似于"id = ?" or "key > ?" "key > ?" , etc, then pass a String array containing the value for those ? 等,然后传递一个包含这些值的String数组? inside the previous string, and finally pass null for having , groupBy and orderBy unless you want to use them. 在上一个字符串中,最后为havinggroupByorderBy传递null,除非您要使用它们。

Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ROWID }, "id = ?", new String[] { Integer.toString(id) }, null, null, null);

After you get the Cursor , do cursor.moveToFirst() or cursor.moveToPosition(0) (can't remember the exact method, but the point is to move the cursor to the first retrieved row) 获得Cursor ,执行cursor.moveToFirst()cursor.moveToPosition(0) (不记得确切的方法,但是重点是将光标移到检索到的第一行)

then you're going to iterate through the cursor with 那么你会通过迭代cursor

while(cursor.moveToNext()) {
     int keyRowIdColumnIndex = cursor.getColumnIndex(KEY_ROWID);
     int yourValue = cursor.getInt(keyRowIdColumnIndex);

     int keyLockNumberColumnIndex = cursor.getColumnIndex(KEY_LOCKNUMBER);
     int pin = cursor.getInt(keyLockNumberColumnIndex);
}

This is a pretty straight forward task and there is a bunch of tutorials, examples , similar questions on SO : 这是一项非常直接的任务,并且在SO上有一堆教程,示例类似问题

How to perform an SQLite query within an Android application? 如何在Android应用程序中执行SQLite查询?

In general - you need to query the database passing your search arguments. 通常,您需要查询传递搜索参数的数据库。 See the documentation . 请参阅文档

It will return you a Cursor with the matching results, which you can then iterate over and manipulate as you wish . 它将为您返回一个具有匹配结果的游标 ,然后您可以根据需要进行迭代操作

Fyi - storing password in a database table is a bad idea. Fyi-将密码存储在数据库表中是个坏主意。 On Android databases can be accessed and easily read. 在Android上,可以访问并轻松读取数据库。 You either need to encrypt your data or think of another way to store it if it's important. 您可能需要加密数据,或者如果重要的话,请考虑另一种存储数据的方法。

         //Try This code 

         public String getLabeId(String LockNo)
     {

    ArrayList<String> Key_Pin_array = new ArrayList<String>();
    Cursor cur = db.rawQuery("SELECT * FROM  DATABASE_TABLE where KEY_LOCKERNUMBER = '" + LockNo + "'", null);
    try {
             while (cur.moveToNext()) 
              {
                    System.out.println("Key_Pin" + cur.getString(3));
                    Key_Pin_array.add(cur.getString(3));
              }
        } 
        catch (Exception e)
        {
                System.out.println("error in getLabelID in DB() :" + e);
        }
        finally 
        {
                cur.close();
        }
    return id;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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