Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在数据库中搜索特定记录时出错_Android_Android Sqlite_Android Cursor - Fatal编程技术网

Android 在数据库中搜索特定记录时出错

Android 在数据库中搜索特定记录时出错,android,android-sqlite,android-cursor,Android,Android Sqlite,Android Cursor,我有一个名为table\u CONTENTS的SQLite数据库表,由类型为contact的对象组成。每个联系人都有两个变量:int-id和String-name。我想在SQLite数据库表中搜索特定id。为此,我在类DatabaseHandler中创建了isPresent()方法。它获取一个intid并搜索表中是否存在具有该id的对象,并相应地返回true或false 下面的代码创建了一个数据库,并向其中添加了两个对象,分别为ids1和2。然后它尝试使用isPresent()搜索id=3。这将

我有一个名为
table\u CONTENTS
的SQLite数据库表,由类型为
contact
的对象组成。每个
联系人
都有两个变量:
int-id
String-name
。我想在SQLite数据库表中搜索特定id。为此,我在类
DatabaseHandler
中创建了
isPresent()
方法。它获取一个
int
id并搜索表中是否存在具有该
id
的对象,并相应地返回
true
false

下面的代码创建了一个数据库,并向其中添加了两个对象,分别为
id
s
1
2
。然后它尝试使用
isPresent()
搜索
id=3
。这将返回
false
,导致文本
ID3不存在
显示。但是,它正在返回
true
,并且显示的文本是
ID3存在

这个代码有什么问题?
isPresent()的正确代码是什么

MainActivity.java

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DatabaseHandler db = new DatabaseHandler(this);

    // Inserting Contacts
    db.addContact(new Contact(1, "one"));
    db.addContact(new Contact(2, "two"));



    TextView text=(TextView) findViewById(R.id.text);
    text.setText(db.isPresent(3) ? "id 3 is present" : "id 3 is absent");


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

public class DatabaseHandler extends SQLiteOpenHelper
{
 private static final int DATABASE_VERSION=1;
 private static final String DATABASE_NAME="contactsManager";
 private static final String TABLE_CONTACTS="contacts";
 private static final String KEY_ID="id";
 private static final String KEY_NAME="name";

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

@Override
public void onCreate(SQLiteDatabase db)
{
    String CREATE_CONTACTS_TABLE="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT "+")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
 db.execSQL("DROP TABLE IF EXISTS"+TABLE_CONTACTS);
    onCreate(db);
}

void addContact(Contact contact)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.name);
    values.put(KEY_ID, contact.id);

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    //2nd argument is String containing nullColumnHack
    db.close(); // Closing database connection
}


// not working properly..always returning true
public boolean isPresent(int id)
{
    SQLiteDatabase sqldb = this.getReadableDatabase();
    String Query = "Select * from " + TABLE_CONTACTS ;
    Cursor cursor = sqldb.rawQuery(Query, null);
    cursor.moveToLast(); //if you not place this cursor.getCount() always give same integer (1) or current position of cursor.

    if(cursor.getCount()<=0)
    {
        return false;
    }

    return true;
}




Contact getContact(int id)
{
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                    KEY_NAME}, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1));
    // return contact
    return contact;
}

public int updateContact(Contact contact)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.name);


    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.id) });
}


}






}
DatabaseHandler.java

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DatabaseHandler db = new DatabaseHandler(this);

    // Inserting Contacts
    db.addContact(new Contact(1, "one"));
    db.addContact(new Contact(2, "two"));



    TextView text=(TextView) findViewById(R.id.text);
    text.setText(db.isPresent(3) ? "id 3 is present" : "id 3 is absent");


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

public class DatabaseHandler extends SQLiteOpenHelper
{
 private static final int DATABASE_VERSION=1;
 private static final String DATABASE_NAME="contactsManager";
 private static final String TABLE_CONTACTS="contacts";
 private static final String KEY_ID="id";
 private static final String KEY_NAME="name";

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

@Override
public void onCreate(SQLiteDatabase db)
{
    String CREATE_CONTACTS_TABLE="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT "+")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
 db.execSQL("DROP TABLE IF EXISTS"+TABLE_CONTACTS);
    onCreate(db);
}

void addContact(Contact contact)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.name);
    values.put(KEY_ID, contact.id);

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    //2nd argument is String containing nullColumnHack
    db.close(); // Closing database connection
}


// not working properly..always returning true
public boolean isPresent(int id)
{
    SQLiteDatabase sqldb = this.getReadableDatabase();
    String Query = "Select * from " + TABLE_CONTACTS ;
    Cursor cursor = sqldb.rawQuery(Query, null);
    cursor.moveToLast(); //if you not place this cursor.getCount() always give same integer (1) or current position of cursor.

    if(cursor.getCount()<=0)
    {
        return false;
    }

    return true;
}




Contact getContact(int id)
{
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                    KEY_NAME}, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1));
    // return contact
    return contact;
}

public int updateContact(Contact contact)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.name);


    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.id) });
}


}






}
试试看


正如我看到的,您没有添加任何条件来选择特定的行。所以,这就像是“获取全部”而不是“选择一个”。因此,cursor.getCount()您没有在查询中添加任何WHERE子句来根据id搜索记录,因此它总是从数据库中选择所有行,这些行将始终返回count>0,这就是代码不起作用的原因

您必须修改Select查询,以便在其中包含WHERE子句,如下所示

String yourQuery= "Select * from " + TABLE_CONTACTS + " WHERE " + KEY_ID + "='"+ id + "'";

鬼知道一切<代码>“从联系人中选择*,其中id=“+id
应修复此问题