I would like to have an option to call 'rawQuery' with a list of Integers passed into it, but it looks I can't: all 'rawQuery' methods require array of Strings...
Why? I can call 'toString' for each passed int object, but I don't see any logic here. Why Google don't provide rawQuery that accept list of Objects?
Are there any limitation or constrains for that?
Probably I miss something?
Thank you.
Probably because rawQuery isn't the prefered method of querying. And since sqlite columns basically don't have types, they probably don't want to assume calling toString on whatever it is you're passing.
SQLite supports the concept of "type affinity" on columns. The type
affinity of a column is the recommended type for data stored in that
column. The important idea here is that the type is recommended, not
required. Any column can still store any type of data. It is just that
some columns, given the choice, will prefer to use one storage class
over another. The preferred storage class for a column is called its
"affinity".
Is there a reason you want to use rawQuery instead of execSQL?
No complex objects are accepted in the list of parameters, as stated on the documentation for the parameter bindArgs:
Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported.
bindArgs only byte[], String, Long and Double are supported in bindArgs.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String, java.lang.Object[]).
Related
I've been trying to write some queries (for MySQL) and I'm not sure how to write this particular one that involves Integer and UInteger types:
DSL.position(T1.FIELD, ":", -1).eq(T2.UINTFIELD)
position returns a Field<Integer>, so I can't simply compare this result to my Field<UInteger>. Is there a simple way to achieve it? I'd like to keep the correct field types for further validation.
You can use DSL.cast() or DSL.coerce() for this. I recommend coerce.
DSL.cast() produces a SQL cast expression and a jOOQ field reference of the desired type
DSL.coerce() does not affect the generated SQL but still produces a jOOQ filed reference of the desired type.
For example:
position(T1.FIELD, ":", -1).eq(coerce(T2.UINTFIELD, SQLDataType.INTEGER))
I wonder if there is way to know the types of results I retrieved from the database in java. Ultimately I want to be able to use appropriate get method for each column of the resultSet (or be able to convert getObject() to the proper type) without me knowing anything about the database/data explicitly.
You will need the ResultSetMetadata. Call getMetadata() on the ResultSest, then call getColumnType on the metadata object.
The returned value is from java.sql.Types.
The number, types and properties of a ResultSet object's columns are provided by the ResulSetMetaData object returned by the ResultSet.getMetaData method.
Use getMetaData(). It contains the type of each column, among other things.
I am working in Java. I have an class called Command. This object class stores a variable List of parameters that are primitives (mostly int and double). The type, number, and order of parameters is specific to each command, so the List is type Object. I won't ever query the table based on what these parameter values are so I figured I would concatenate them into a single String or serialize them in some way. I think this may be a better approach that normalizing the table because I will have to join every time and that table will grow huge pretty quickly. (Edit: The Command object also stores some other members that won't be serialized such as a String to identify the type of command, and a Timestamp for when it was issued.)
So I have 2 questions:
Should I turn them into a delimited String? If so, how do I get each object as a String without knowing which type to cast them to? I attempted to loop through and use the .toString method, but that is not working. It seems to be returning null.
Or is there some way to just serialize that data of the array into a column of the DB? I read about serialization and it seems to be for the context of serializing whole classes.
I would use JSON serializer and deserializer like Jackson to store and retrieve those command objects in DB without losing the specific type information. On a side note, I would have these commands implement a common interface and store them in a list of commands and not in a list of objects.
My General Question is: Is it inefficient/bad practice to call preparedStatement.executeBatch() if there's only one query in the batch?
I'm writing a generic method for the Java Helper Library to execute a query. There's a javabean called HelperQuery which holds a list of arrays another javabean called QueryParameter which holds a type (like STRING, BLOB, INT, etc.) and a value. The QueryParameter is used to fill the HelperQuery's PreparedStatement. In many cases, there will be only one array of QueryParameters.
My Specific Question is: Should I handle things differently if there's only one array of QueryParameters or would it be ok to handle things exactly the same regardless of how many QueryParameters there are?
executeBatch is a "super" method from the PreparedStatement's parent Statement which returns an int[] which indicates the success/failure of the executed queries and executeQuery returns a ResultSet. Therefore, it would be a good idea to have the two be totally different method calls so the developer can handle them differently. I would recommend:
An executeQuery(HelperQuery helperQuery) method which will return the associated ResultSet and will only get the first QueryParameters from the HelperQuery (for convenience) and another method which the developer can specify which QueryParameter set to use (either have them specify a number of the QueryParameter list or just pass in the QueryParameters explicitly (I recommend the second of the two)).
An executeBatch(HelperQuery helperQuery method which will return the int[] and the developer can handle that as they wish.
It's always good to give the user (developer in this case) power to do what they want (but also provide for a simple solution for them to perform common tasks).
Is there a way to get all possible values from a MySQL enum column?
The MySQL documentation says the MySQL enum type is returned as a Java String, so I basically would like a way to get all possible strings I can pass when querying a table with such an enum.
I couldn't immediately find anything when I was looking through the metadata returned for such a column, but since enum isn't standard SQL, I'm not sure it's even possible... any suggestions?
SHOW COLUMNS FROM Table LIKE field returns something like:
enum('value1','value2','value3','value4'). Parse out the enum values from the string with a regular expression ("/'(.*?)'/").