on 2011 Sep 09 5:21 AM
My Android application is using ultralite DB. I should store a primary key (pk) into a java variable. pk datatype is uniqueidentifier; as I cannot use String neither long type Please what is the best way to store it?
Thank you very much in advance. S
Request clarification before answering.
Sorry, I don't use UltraLite/UltraLiteJ so my chance to help is quite delimited...
What error (SQLCODE) are you getting?
Do you use an UUIDValue type or a string or byte[] to store the UUID?
You might try to use a prepared statement, here with a UUID string, such as (untested!):
// Create a new PreparedStatement object from an existing connection. String sql_string = "SELECT * FROM myTable WHERE pk = ?"; PreparedStatement ps = conn.prepareStatement(sql_string); ps.set(1, '5ad8bc00-2822-433f-88d1-2dd028e70b17'); // result returns true if the execute statement runs successfully. boolean result = ps.execute(); // Check if the PreparedStatement contains a ResultSet. if (ps.hasResultSet()) { // Store the ResultSet in the rs variable. ResultSet rs = ps.getResultSet; } // Close the PreparedStatement to release resources. ps.close();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just let me hint that there are some reasons not to use UUIDs as PKs (some of them are discussed in this forum, too) and that GLOBAL AUTOINCREMENT defaults (with datatype INT/BIGINT) might be an alternative...
From the docs:
UNIQUEIDENTIFIER values are stored as BINARY(16), but are described to client applications as BINARY(36). This description ensures that if the client fetches the value as a string, it has allocated enough space for the result.
And SQL type BINARY is automatically mapped to Java type byte[]. So that seems to be the preferred Java mapping.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
try the uuid without the dashes in your example:
SELECT * FROM mytable WHERE pk='5ad8bc002822433f88d12dd028e70b17';
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That would mean you are using the UUID as a binary literal, not as a string constant. And then the 0x prefix is necessary. However, it should work to use it as a string constant, too.
Therefore we're glad that you have found a solution.
Nevertheless it would be interesting to find out why it hasn't worked for you the way so many here have suggested...
Thank you for your kind reply. I did not create that db but I only have to connect to its and to execute query.
In this particular case, I have to get my pk <(uniqueidentifier datatype) and use it into another query.
My error is probably because I execute a query in such a way: SELECT * FROM mytable WHERE pk='5ad8bc00-2822-433f-88d1-2dd028e70b17';
would you kindly provide us an example?
Thanks in advance. S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
80 | |
30 | |
10 | |
8 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.