Why do some SQLite methods expect arguments as an object [], while others need String []?

I'm curious about the design of the Android SQLite API . For example, we have

public void execSQL(String sql, Object[] bindArgs) 

for SQL that do not return data, and we also have

 public Cursor rawQuery(String sql, String[] selectionArgs) 

for SQL returning data.

Why are the arguments expected as an array of Object in the first and as an array of String in the latter case? I understand that accepting only rows may make sense due to SQLite binding. However, if this is the case, why not use String arrays for arguments sequentially? Is there any non-String argument that makes sense for execSQL but not for rawQuery ?

+4
source share
2 answers

There is one. For example, if you want to compare BLOB values. Take a look at this example .

UUID stored as BLOB

 db.execSQL("DELETE FROM "+USER_TABLE+" WHERE "+USER_UUID+"=?", new Object [] { uuid.toByteArray } 

According to the documentation, byte [] , String, Long and Double are supported in bindArgs. However, it is recommended that you do not use this method to execute SELECT / INSERT / UPDATE / DELETE statements.

I just don’t know why.

+1
source

Actually, accepting only strings does not make sense, because strings are never compared with numbers, i.e. queries such as

 c.rawQuery("SELECT * FROM mytable WHERE some_number_column = ?", args); 

will never return records with numbers. (You would need to declare all columns as TEXT and never use expressions or convert them with something like CAST(... AS TEXT) .)

Jens mentioned a possible explanation, but this is no reason for an inconsistent and simple poor design.

0
source

Source: https://habr.com/ru/post/1444399/


All Articles