English 中文(简体)
5. 充满活力的问询
原标题:SQLite dynamic query

我有一个专门的KQ数据库,我想做的是用户选择一个过滤器。 例如,有一个图书数据库,用户只是想从“Agata christies书”中查找数据。

因此,我选择了一种选择,然后打算将所选择的田地转至另一个执行询问的活动。

我的问题是,如何使问题充满活力? 想一去1个以上的过滤器,如何根据从其他活动获得的数据,使“WHERE”条款成为一种意图?

感谢

最佳回答

Lazy way but tried and true:

String query = "Select id FROM books WHERE 1=1"
if (condition1) query+= " AND name="+theName;
if (condition2) query+= " AND author="+theAuthor;
if (condition3) query+= " AND price="+thePrice;

If you have full control of options aka via spinners, this is safe. If its an edittext, use preparedStatements and bind the arguments to avoid SQLI.

问题回答

但这并不是这样做的最佳方式,但假定你事先知道,你可以有1个ger化过滤器(price)和1个str过滤器(作者名称),我试图:

SELECT * FROM BOOKS WHERE (price<0 OR AND BOOKS.price = price ) AND (author="" OR BOOKS.author = author);

I m 不是专家,请检查Syntax。 陷阱在这里是为了确定价格和带;如果过滤器没有固定,则0(所有线都考虑在内,因为情况是价格的调整;0是真实的),并且将作者作为空洞,不过滤提交人(SlectT不会过滤这些线,因为情况是真实的)。

这将发挥作用!

boolean filterName = false;
boolean filterPrice = false;
ArrayList<String> selectionArgs = new ArrayList<String>();
String query = "SELECT * FROM BOOKS WHERE 1=1";
if(filterName) {
    selectionArgs.add(searchString);
    query += " AND NAME = ?";
}
if(filterPrice) {
    selectionArgs.add(priceString);
    query += " AND PRICE= ?";
}

Cursor c = m_Database.rawQuery(query, (String[])selectionArgs1.toArray());

引证这一点,用学生数据作为实例的Im

public ArrayList<Student>getStudent(int stdID,String stdName)
{
ArrayList<Student> studentArr = new ArrayList<>();
 SQLiteDatabase db = this.getReadableDatabase();
 Cursor cursor = null;

String sqlQuery = "SELECT * FROM tblstudent WHERE 1=1";
String sqlParam = "";

/*Here we re building the dynamic sql.*/

if(stdID > 0)
{
sqlQuery += " AND std_id = ?";
sqlParam = String.valueOf(stdID);
}

if(!stdName.trim().isEmpty())
{
sqlQuery += " AND std_name = ?";
sqlParam = stdName.trim();
}

/*Do this to avoid SQL injection Attack (SIA) [it s very important!]*/
sqlParam = sqlParam.replaceAll(";|--","");


 if (db != null) {
    
  if (!sqlParam.isEmpty()) {

    cursor = db.rawQuery(SQL,new String[]{sqlParam});
        
    }else {
   /*if  the "sqlParam" is empty.*/

   cursor = db.rawQuery(SQL,null);
    }
 if (cursor != null && cursor.getCount()>0) {

 while(cursor.moveToNext()){

Student stdmodel = new Student();

stdmodel.setstdID(cursor.getInt(0));
stdmodel.stdName(cursor.getString(1));

studentArr.add(stdmodel);
}

 cursor.close();
  }
}

return studentArr;
} 




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签