我想从sqlite数据库的列中搜索一个数字

时间:2016-07-29 08:16:31

标签: android sqlite

我创建了一个包含3列的数据库,现在我想搜索一个我从EditText提供的号码,并在该列中显示该号码

这是我的数据库

public class DBHelper extends SQLiteOpenHelper {

    private final static String DATABASE_NAME = "Tracker";
    private final static int DATABASE_VERSION = 1;
    private final static String TABLE_NAME = "registration";
    private final static String COL_ID = "id";

    private final static String COL_name = "Name";
    private final static String COL_email = "Email_id";
    private static final String COL_contact = "ContactNo";

    private static final String createTableQuery = "CREATE TABLE " + TABLE_NAME + "(" +
            COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL_name + " TEXT," +
            COL_email + " TEXT," +
            COL_contact + " TEXT)";

    public DBHelper(Context context) {
        //1. Initialised database
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createTableQuery);
    }

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

        onCreate(db);
    }

    public long insert( User user) {
        // 1. init database object
        SQLiteDatabase DataBase = this.getWritableDatabase();

        // 2. init ContentValues object
        ContentValues row = new ContentValues();

        row.put(COL_name, user.getName1());
        row.put(COL_email, user.getMail1());
        row.put(COL_contact, user.getCon1());

       // row.put(KEY_ListItem,getListItem(cur));

        //3. insert row into table
        long id = DataBase.insert(TABLE_NAME, null, row);
        //4. disconnect database
        DataBase.close();
        if (id != 0) {
            return id;
        }
        return 0;
    } 

现在我想从这个活动中搜索一个数字

public class TestActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tv1;
    //    EditText conNo;

    private DBHelper databaseHelper;
    public final static String KEY = "position";
    TelephonyManager tel;

    String phoneNumber;
    ListView listview;
    ArrayList<String> arraylist= new ArrayList<String>();

    EditText conNo;
    Button Search;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    conNo= (EditText) findViewById(R.id.editText4);
    Search= (Button) findViewById(R.id.button2);
}}

当我在conNo中进行修改并按下搜索按钮后,应检查contactno列中的数字。

请建议我解决此问题的可能方法。

3 个答案:

答案 0 :(得分:2)

在sqlite DB中的列中搜索以下数字查询:

 String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE  " + ContactNo + " = '" + EDIT_TEXT_NUMBER +"'";

答案 1 :(得分:1)

在数据库类中创建一个方法,如

public static function getNumber(edit_text_number){
    String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE  " + ContactNo + " = '" + edit_text_number +"'";
    return db.execSQL(selectQuery);
}

这样的活动中调用此函数
EditText editText = findViewById(R.id.edit_text)
result = getNumber(editText.getText().toString())

答案 2 :(得分:1)

检查以下代码,

     public boolean ifNumberExists(String strNumber)
     {
         Cursor cursor = null;
         String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE  " + ContactNo + " = '" + strNumber +"'";
          cursor= db.rawQuery(selectQuery,null);
          boolean exists = (cursor.getCount() > 0);
          cursor.close();
          return exists;
     }

主要活动

    search.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) 
         {
            DBHelper db = new DBHelper(getApplicationContext());

             if(dbObject.ifNumberExists(conNo.getEditableText()))
              {
                  // Toast Number Exists
              }
              else
              {
                 // Number not Exists
              }
         }
    }