My query is:
String _query = "select DISTINCT Y.CATE_CODE4 CATE_CODE, Y.CATE_NAME4 CATE_NAME,
0 IS_PRESET from G_MASTER X INNER JOIN CATEGORY_VIEW Y ON X.CATE_4 = Y.CATE_CODE4
where X.IS_SALE = 1 AND Y.IS_LOCKED4 = 0 AND Y.CATE_NAME2 not in ('XXXX')";
How does Android SQLite make db.query()?
First of all you should read SQLiteDatabase reference.
You could use a rawQuery(), here's an example of it :
rawQuery - db.rawQuery("select DISTINCT Y.CATE_CODE4 CATE_CODE, Y.CATE_NAME4 CATE_NAME,
0 IS_PRESETG_MASTER X INNER JOIN CATEGORY_VIEW Y ON X.CATE_4 = Y.CATE_CODE4 from table where X.IS_SALE = 1 AND Y.IS_LOCKED4 = 0 AND Y.CATE_NAME2 not in ('XXXX')"",new String[]{"data"});
It's just an example, you have to adapt your code to this.
Related
The query below is working perfectly well if the user's phone supports sqlite version 3.25.0 or higher. Otherwise, as you can guess, the query doesn't work. How can I transform the code, without using the row_number () function?
SELECT *
FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY recipe_name) AS rn,
rt.*
FROM
SyncRecipeIngredientTable sr
JOIN RecipeIngredientTable ri ON ri.recipe_ingredient_id = sr.recipe_ingredient_id
JOIN RecipeTable rt ON rt.recipe_id = sr.recipe_id
WHERE ri.recipe_item_name in ("patates", "soğan", "su")
GROUP by rt.recipe_id
HAVING COUNT(*) >= 3
)
WHERE rn = 1
The database's ER diagram:
The expected result:
I would expect recipes to have unique names. If so, then the outer quer is simply not needed:
SELECT rt.*
FROM SyncRecipeIngredientTable sr JOIN
RecipeIngredientTable ri
ON ri.recipe_ingredient_id = sr.recipe_ingredient_id JOIN
RecipeTable rt
ON rt.recipe_id = sr.recipe_id
WHERE ri.recipe_item_name in ('patates', 'soğan', 'su')
GROUP by rt.recipe_id
HAVING COUNT(*) >= 3;
If recipes can have duplicated names, then you can use a correlated subquery:
WITH rt AS (
SELECT rt.*
FROM SyncRecipeIngredientTable sr JOIN
RecipeIngredientTable ri
ON ri.recipe_ingredient_id = sr.recipe_ingredient_id JOIN
RecipeTable rt
ON rt.recipe_id = sr.recipe_id
WHERE ri.recipe_item_name in ('patates', 'soğan', 'su')
GROUP by rt.recipe_id
HAVING COUNT(*) >= 3
)
SELECT rt.*
FROM rt
WHERE rt.recipe_id = (SELECT MAX(rt2.recipe_id)
FROM rt rt2
WHERE rt2.recipe_name = rt.recipe_name
);
This is the structure and data of my table funco inside database your.db (SQLite):
Id Disp Tp
1 Ball 2
2 Light 1
3 Sea 4
This is my code:
var db = SQLiteDatabase.openDatabase(this.filesDir.path +
"/your.db",null, SQLiteDatabase.OPEN_READWRITE)
val c = db.rawQuery("Select Id, Disp, Tp From Funco Where Id<=2;",null)
var stat = c.moveToFirst()
var result=""
while (stat) {
val mId = c.getInt(c.getColumnIndex("Id"))
val mDisp = c.getString(c.getColumnIndex("Disp"))
val mTp = c.getInt(c.getColumnIndex("Tp"))
result += "$mId $mDisp $mTp | "
stat = c.moveToNext()
}
c.close()
db.close()
The result value:
1 Ball 2 | 2 Light 1 |
If I replace my second line for
val c = db.rawQuery("Select ?, ?, ? From Funco Where ?<=2;"
,arrayOf("Id","Disp","Tp","Id"))
There is no error raised, but the cursor is empty!
Why?
Update:
The #tynn answer is right. I think that the documentation is subtle.
I think that compiler shoud be throw an error and not just return empty cursor.
In the same flavor, one can write
val c = db.query("funco",arrayOf("Id","Disp","Tp"),"Id<=?",
arrayOf("2"),null,null,null)
But below code fails
val c = db.query("funco",arrayOf("Id","Disp","Tp"),"?<=?",
arrayOf("id","2"),null,null,null)
As the documentation states:
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
The question marks are there to bind different values to a precompiled SQL query. This functionality is not suitable for configuring the columns to be used in the query. Your first approach is the correct one. You could make the 2 in it dynamic though:
db.rawQuery("Select Id, Disp, Tp From Funco Where Id<=?;", arrayOf("2"))
im getting data from database which match any prefex how to add % on bothside of variable? below is my query i want to add '"+School_name+"' to this '%"+School_name+"%' how to do that?
Cursor mCursor3 = db.selectQuery("SELECT * FROM uss_school sch LEFT JOIN uss_school_to_level sl ON sch.school_id = sl.school_id LEFT JOIN uss_level l ON sl.level_id = l.level_id WHERE sch.name like '"+School_name+"'");
How do I add quotation around string variable any idea? my database execute query if "" is around variable name what do I do ? I did this"+"+ School_name+"+"but its not working how I add variable name with quotation ?
Bundle bundle = this.getIntent().getExtras();
School_name = bundle.getString("Serachvalue");
db.selectQuery("SELECT * FROM uss_school sch LEFT JOIN uss_school_to_level sl ON
sch.school_id = sl.school_id LEFT JOIN uss_level l ON sl.level_id = l.level_id WHERE
l.title ="+ School_name+" ORDER BY sch.name ASC");
But data base execute wuery with in quotes,
SELECT * FROM uss_school sch LEFT JOIN uss_school_to_level sl ON sch.school_id =
sl.school_id LEFT JOIN uss_level l ON sl.level_id
= l.level_id WHERE l.title = "Elementary" ORDER BY sch.name ASC
Try following way,
db.selectQuery("SELECT * FROM uss_school sch LEFT JOIN uss_school_to_level sl ON
sch.school_id = sl.school_id LEFT JOIN uss_level l ON sl.level_id = l.level_id WHERE
l.title ='"+ School_name+"' ORDER BY sch.name ASC");
You simply need to add ' whenever you are using string variable in query field.
Here is SQL UPDATE I'm trying to execute:
UPDATE T
SET T.CurrentStopNumber = TS.CurrentStopNumber
FROM Trip T
INNER JOIN (SELECT TripId, MIN(StopNumber) CurrentStopNumber
FROM TripStop
WHERE TripId = '106504'
AND (IsPickup = 1 OR IsDrop = 1)
AND StopNumber > (SELECT COALESCE(max(StopNumber), 0)
FROM TripUpdate
WHERE TripId = '106504'
AND Type = 2)) TS ON T.TripId = TS.TripId
I get error on second line:
/* Error message: SQL script is wrong mismatched input . expecting "EQ" */
I am familiar with SQL Server and I'm sure it would run on SQL Server just fine. Subquery runs fine and returns 1 row as I expect. I just need to update table with that value. What is wrong?
Use:
UPDATE TRIP
SET currentstopnumber = (SELECT MIN(ts.stopnumber)
FROM TRIPSTOP ts
WHERE ts.tripid = TRIP.tripid
AND ts.tripid = '106504'
AND 1 IN (ts.ispickup, ts.isdrop)
AND ts.stopnumber > (SELECT COALESCE(MAX(tu.stopnumber), 0)
FROM TRIPUPDATE tu
WHERE tu.tripid = ts.tripid
AND tu.type = 2)
GROUP BY ts.tripid)
WHERE EXISTS (SELECT NULL
FROM TRIPSTOP ts
WHERE ts.tripid = TRIP.tripid
AND ts.tripid = '106504'
AND 1 IN (ts.ispickup, ts.isdrop)
AND ts.stopnumber > (SELECT COALESCE(MAX(tu.stopnumber), 0)
FROM TRIPUPDATE tu
WHERE tu.tripid = ts.tripid
AND tu.type = 2))
The JOIN is preferable, but the syntax isn't widely supported.
I don't think that you should specify the table for the fields that you set, as the database already knows which table to update:
SET CurrentStopNumber = TS.CurrentStopNumber
(IIRC, that is not allowed in SQL Server either.)