如何使用SQL来搜索的价值? [英] How to use SQL to search for a value?

查看:121
本文介绍了如何使用SQL来搜索的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何在Android中使用的数据库,到目前为止,我已经学会了如何获取数据表中的所有行,但我不知道我会怎么使用SQL查询来搜索为一个值,该值等于所述字符串值I通过下面的方法?

我不知道,如果有人可以添加一些简单的code如何查询与SQL数据库在我的code? preciate的帮助!谢谢!

事情是这样的:查询字符串=SELECT * FROM DB_TABLE那里TABLE_IMAGE_PATH =文件路径;

  //读取从数据库
公共字符串readContacts(字符串文件路径){

    的String []列=新的String [] {表标识符,TABLE_IMAGE_PATH,TABLE_CONTACT_NAME,TABLE_CONTACT_NUMBER,TABLE_CONTACT_ADDRESS};
    光标C = db.query(DB_TABLE,列,NULL,NULL,NULL,NULL,NULL);

    INT ID = c.getColumnIndex(表标识符);
    INT的ImagePath = c.getColumnIndex(TABLE_IMAGE_PATH);
    INT名称= c.getColumnIndex(TABLE_CONTACT_NAME);
    INT数= c.getColumnIndex(TABLE_CONTACT_NUMBER);
    INT地址= c.getColumnIndex(TABLE_CONTACT_ADDRESS);

    字符串结果=;

    为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
    结果=结果+ c.getString(ID)++ c.getString(的ImagePath)++ c.getString(名称)++ c.getString(编号)++ c.getString(地址) ;
    }

    返回结果;
}
 

解决方案

您可以搜索查询

其精确的字符串

 查询字符串=SELECT * FROM DB_TABLE那里TABLE_IMAGE_PATH像'+文件路径+;
 

如果值包含的字符串

 查询字符串=SELECT * FROM DB_TABLE那里TABLE_IMAGE_PATH像%+文件路径+%;
 

如果从去年的价值需求

 查询字符串=SELECT * FROM DB_TABLE那里TABLE_IMAGE_PATH像%+文件路径+;
 

如果从第一个值的需求

 查询字符串=SELECT * FROM DB_TABLE那里TABLE_IMAGE_PATH像'+文件路径+%;
 

把这些

  SQLiteDatabase DBR = this.getReadableDatabase();
    光标CUR = dbR.rawQuery(查询,NULL);
            回报CUR
 

使用此光标,你可以得到的价值。

I'm trying to learn how to use database in Android and so far I have learned how to fetch all rows of data in the table, but I'm not sure how I'm going to use a SQL query to search for a value that is equal to the string value I pass to the method below?

I wonder if someone could add some simple code how to query the database with SQL in my code? Preciate the help! Thanks!

Something like this: String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH = filePath";

// Read from database
public String readContacts(String filePath){

    String[] columns = new String[]{TABLE_ID, TABLE_IMAGE_PATH, TABLE_CONTACT_NAME, TABLE_CONTACT_NUMBER, TABLE_CONTACT_ADDRESS};
    Cursor c = db.query(DB_TABLE, columns, null, null, null, null, null);

    int id = c.getColumnIndex(TABLE_ID);
    int imagePath = c.getColumnIndex(TABLE_IMAGE_PATH);
    int name = c.getColumnIndex(TABLE_CONTACT_NAME);
    int number = c.getColumnIndex(TABLE_CONTACT_NUMBER);
    int address = c.getColumnIndex(TABLE_CONTACT_ADDRESS);

    String result = "";

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
    result = result + c.getString(id) + " " + c.getString(imagePath) + " " + c.getString(name) + " " + c.getString(number) + " " + c.getString(address);
    }

    return result;
}

解决方案

You can search Query

its for exact string

String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH  like  '"+filePath+"';

if value contains your string

String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH  like  '%"+filePath+"%';

if value needs from last

String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH  like  '%"+filePath+"';

if value needs from first

String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH  like  '"+filePath+"%';

put these

SQLiteDatabase dbR = this.getReadableDatabase();
    Cursor cur = dbR.rawQuery("query", null);
            return cur

using this cursor you can get value.

这篇关于如何使用SQL来搜索的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆