如何从android获取数据库中的单个值?

时间:2013-09-13 08:31:15

标签: android database

我有一个员工详细信息的数据库,我想从database.am获取密码试图获取值但面临问题。如何从数据库中检索密码....我有列keyid,姓名,姓氏,手机,发布日期,电子邮件,性别,个人ID,加入日期,安全问题,答案,用户名,密码。

需要检索密码值

    public List<String>  credientials(String eid) {

        List<String> labels = new ArrayList<String>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_EMPLOY, new String[] { 
                   KEY_PASSWORD,

        }, KEY_USERNAME + " = ?", new String[] { String.valueOf(eid) }, null, null, null, null);
        if (cursor != null){
            do{
            cursor.moveToFirst();
            labels.add(cursor.getString(12));
            } while (cursor.moveToNext());
        }cursor.close();
        db.close();
        return labels;
    }

我的错误日志Cat

09-13 13:24:00.354: E/AndroidRuntime(28252): FATAL EXCEPTION: main
09-13 13:24:00.354: E/AndroidRuntime(28252): java.lang.OutOfMemoryError: [memory exhausted]
09-13 13:24:00.354: E/AndroidRuntime(28252):    at dalvik.system.NativeStart.main(Native Method)
09-13 13:24:00.373: E/SQLiteDatabase(28252): close() was never explicitly called on database '/data/data/com.example.analyticbeans/databases/jk' 
09-13 13:24:00.373: E/SQLiteDatabase(28252): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at com.example.analyticbeans.dataBaseHelper.credientials(dataBaseHelper.java:129)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at com.example.analyticbeans.existinguserActivity.getData(existinguserActivity.java:86)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at com.example.analyticbeans.existinguserActivity.login(existinguserActivity.java:80)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at com.example.analyticbeans.existinguserActivity$1.onClick(existinguserActivity.java:55)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.view.View.performClick(View.java:3511)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.view.View$PerformClick.run(View.java:14105)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.os.Handler.handleCallback(Handler.java:605)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.os.Looper.loop(Looper.java:137)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at java.lang.reflect.Method.invoke(Method.java:511)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-13 13:24:00.373: E/SQLiteDatabase(28252):    at dalvik.system.NativeStart.main(Native Method)
09-13 13:24:00.484: E/System(28252): Uncaught exception thrown by finalizer
09-13 13:24:00.573: E/System(28252): java.lang.IllegalStateException: Don't have database lock!
09-13 13:24:00.573: E/System(28252):    at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
09-13 13:24:00.573: E/System(28252):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
09-13 13:24:00.573: E/System(28252):    at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
09-13 13:24:00.573: E/System(28252):    at android.util.LruCache.trimToSize(LruCache.java:197)
09-13 13:24:00.573: E/System(28252):    at android.util.LruCache.evictAll(LruCache.java:285)
09-13 13:24:00.573: E/System(28252):    at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
09-13 13:24:00.573: E/System(28252):    at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
09-13 13:24:00.573: E/System(28252):    at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
09-13 13:24:00.573: E/System(28252):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
09-13 13:24:00.573: E/System(28252):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
09-13 13:24:00.573: E/System(28252):    at java.lang.Thread.run(Thread.java:856)

更新错误

09-13 14:08:19.173: E/AndroidRuntime(13156): FATAL EXCEPTION: main
09-13 14:08:19.173: E/AndroidRuntime(13156): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at com.example.analyticbeans.dataBaseHelper.credientials(dataBaseHelper.java:136)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at com.example.analyticbeans.existinguserActivity.getData(existinguserActivity.java:86)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at com.example.analyticbeans.existinguserActivity.login(existinguserActivity.java:80)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at com.example.analyticbeans.existinguserActivity$1.onClick(existinguserActivity.java:55)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.view.View.performClick(View.java:3511)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.view.View$PerformClick.run(View.java:14105)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.os.Handler.handleCallback(Handler.java:605)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.os.Looper.loop(Looper.java:137)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at java.lang.reflect.Method.invoke(Method.java:511)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-13 14:08:19.173: E/AndroidRuntime(13156):    at dalvik.system.NativeStart.main(Native Method)

更新我的完整代码

package com.example.analyticbeans;


import java.util.ArrayList;
 import java.util.List;

    import android.content.ContentValues;
    import android.content.Context;
 import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
     import android.database.sqlite.SQLiteOpenHelper;

public class dataBaseHelper extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "jk";

// employee table name
private static final String TABLE_EMPLOY = "employeeTable";


// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FIRST_NAME ="firstName"; 
private static final String KEY_LAST_NAME ="lastName";
private static final String KEY_BIRTH ="birth";
private static final String KEY_MOBILENO="MobileNo";
private static final String KEY_GENDER="Gender";
private static final String KEY_PERSONALMAIL="PersonalMail";
private static final String KEY_ADDRESS="Address";
private static final String KEY_JOINING="Joining";
private static final String KEY_OFFICIALMAIL="OfficialMail";
private static final String KEY_QUESTION="Question";
private static final String KEY_ANSWER="Answer";
private static final String KEY_USERNAME="userName";
private static final String KEY_PASSWORD="password";

private String labels;




public dataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

   // Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_EMP_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EMPLOY + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FIRST_NAME + " TEXT ," 
            + KEY_LAST_NAME + " TEXT ," + KEY_BIRTH + " TEXT ,"+ KEY_MOBILENO + " NUMBER ,"
            + KEY_GENDER + " TEXT ,"+ KEY_PERSONALMAIL + " TEXT ,"
            + KEY_ADDRESS + " TEXT ,"+ KEY_JOINING + " TEXT ,"
            + KEY_OFFICIALMAIL + " TEXT ,"+ KEY_QUESTION + " TEXT ,"
            + KEY_ANSWER + " TEXT ,"
            + KEY_USERNAME + " TEXT ," + KEY_PASSWORD + " TEXT "+ ")";

    db.execSQL(CREATE_EMP_TABLE);
}
public void insertData(String firstname, String lastname,String birth,String mobile ,String spingender,
        String personalmail, String address,String joining,String official,String question,String answer,String username,
        String password) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_FIRST_NAME, firstname); 
    values.put(KEY_LAST_NAME, lastname);
    values.put(KEY_BIRTH, birth);
    values.put(KEY_MOBILENO,mobile);
    values.put(KEY_GENDER,spingender);
    values.put(KEY_PERSONALMAIL,personalmail);
    values.put(KEY_ADDRESS, address);
    values.put(KEY_JOINING, joining);
    values.put(KEY_OFFICIALMAIL,official);
    values.put(KEY_QUESTION, question);
    values.put(KEY_ANSWER, answer);
    values.put(KEY_USERNAME, username);
    values.put(KEY_PASSWORD, password);

    db.insert(TABLE_EMPLOY, null, values);
    db.close();

}
public List<String> getContact(String eid){
SQLiteDatabase db = this.getReadableDatabase();
List<String> labels = new ArrayList<String>();

    Cursor cursor = db.query(TABLE_EMPLOY, new String[] { 
            KEY_ID,
            KEY_FIRST_NAME,
            KEY_LAST_NAME,KEY_BIRTH,KEY_MOBILENO,KEY_GENDER,KEY_PERSONALMAIL,KEY_JOINING,
            KEY_OFFICIALMAIL,KEY_QUESTION,KEY_ANSWER,KEY_PASSWORD
 }, KEY_USERNAME + " = ?", new String[] { String.valueOf(eid) }, null, null, null, null);
    if (cursor != null){
        do{
        cursor.moveToFirst();
        labels.add(cursor.getString(0));
        labels.add(cursor.getString(1));
        labels.add(cursor.getString(2));
        labels.add(cursor.getString(3));
        labels.add(cursor.getString(4));
        labels.add(cursor.getString(5));
        labels.add(cursor.getString(6));
        labels.add(cursor.getString(7));
        labels.add(cursor.getString(8));
        labels.add(cursor.getString(9));
        labels.add(cursor.getString(10));
        labels.add(cursor.getString(11));
        labels.add(cursor.getString(13));


        } while (cursor.moveToNext());
    }cursor.close();
    db.close();

    // returning lables
    return labels;

}


    public List<String>  credientials(String eid) {

        List<String> labels = new ArrayList<String>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_EMPLOY, new String[] { 
                   KEY_PASSWORD,

        }, KEY_USERNAME + " = ?", new String[] { String.valueOf(eid) }, null, null, null, null);
        if (cursor.getCount()>0){
            cursor.moveToFirst();
            do{

            labels.add(cursor.getString(13));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return labels;
    }
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

}

}

3 个答案:

答案 0 :(得分:0)

我认为你的问题不是数据库,而是do-while循环没有正确实现:

do {
   cursor.moveToFirst();
   labels.add(cursor.getString(12));
} while (cursor.moveToNext());

将其更改为:

if (cursor != null && cursor.moveToFirst()) {
   do {
      labels.add(cursor.getString(12)); // use rather getColumnIndex(<colname>)
   } while (cursor.moveToNext());
}

在您的原始情况下,您创建了无限循环(first-&gt; next-&gt; first-&gt; next)。这导致:

java.lang.OutOfMemoryError

答案 1 :(得分:0)

问题是你在这里创建一个无限循环:

do{
            cursor.moveToFirst();
            labels.add(cursor.getString(12));
            } while (cursor.moveToNext());

执行cursor.moveToFirst();时,将光标置于第一个位置,最后一条指令为cursor.moveToNext(),使光标向前移动

您只需要在之前移动cursor.moveToFirst();

答案 2 :(得分:0)

您刚查询了一列。你的索引是错误的。不要将moveToFirst置于循环中。请记住抓住异常。

public List<String>  credientials(String eid) {

            List<String> labels = new ArrayList<String>();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.query(TABLE_EMPLOY, new String[] { 
                       KEY_PASSWORD,

            }, KEY_USERNAME + " = ?", new String[] { String.valueOf(eid) }, null, null, null, null);

            try {
                if (cursor != null && cursor.moveToFirst()){
                    do{
                        labels.add(cursor.getString(0)); // KEY_PASSWORD
                    } while (cursor.moveToNext());
                }
            }
            finally {
                if (cursor != null) {
                    cursor.close();
                }

                if (db != null) {
                    db.close();
                }
            }


            return labels;
        }