Im using google app engine data-store built in eclipse using my model for the table. The id is just the date and time from android.
I can query by a row like this and it does work!
select from Quotes as Quotes ORDER BY votes DESC
I want to get my results back by my entities id however this query does not work
select from Quotes as Quotes ORDER BY Id DESC
Here is my table. How can I query by my id/Name and trust me ive tried
select from Quotes as Quotes ORDER BY ID/Name DESC
edit: you probably notice i have a dummyid. I do not want to use that row because I made it in a very hacky way and requires extra loading on the users side.
Oh, dear. I see the problem, now. You have a column named ID/Name. It's usually wise to keep identifiers limited to alphanumeric characters.
Can you rename the column? That would be the best step forward.
If that's not an option, you can wrap it in backticks so that it's treated as an identifier:
SELECT * FROM Quotes ORDER BY `ID/Name` DESC;
See SQL Fiddle, which almost certainly won't match your schema but should get the point across.
That Id/Name is the key field, imagine it is similar to primary key. to refer to that field in query, use
__key__
Example: select * from EntityTable where __key__ = Key('EntityTable', ....)
In your example, using date/time as key name is not really helpful, maybe you can find another info to be used as key.
Related
I have the following problem for which I’m trying to find an elegant solution.
The front-end shows a search input field. The user is allowed to enter any string. I want to send a request to the backend and get all entities which contain the entered value in any column. It is unknown in which table and which column the entered string can be found.
Example:
Entities: Human, Building, Planet
All of them have 15+ attributes.
Now, the user enters the number 12 into the search field. I want the backend to find any entries in the database which contain the number 12 which could be a person’s shoe size or the street number the building is in and so on.
As technologies for the backend, I’m using JPA (Hibernate) and a Postgres database.
I’d like to implement that as generic as possible so I don’t have to modify anything if new tables or new attributes come along.
What I already tried is getting all table names like that:
"select col.table_name from information_schema.columns col join information_schema.tables tab on tab.table_schema = col.table_schema and tab.table_name = col.table_name and tab.table_type = 'BASE TABLE' where col.table_schema not in ('information_schema', 'pg_catalog')"
+ "group by col.table_name"
Since jpa querys use the Java class name instead of the table name, it’s no use tough. Same problem is for attributes. Using native querys to avoid this problem would make things even more complicated.
Is there any good solution to search the whole database for a specific value?
What are some examples of efficiently searching through a directory as you're typing a person's name?
Say for example, we have a database with 1 million users. We start typing in the search box: "sea", it will display every user's name on a scroll-able window that has "sea" on it (kind of like searching through a Skype directory). After changing a letter, the window should update immediately. All of this is coming from a SQL database. What are few efficient libraries, algorithms that can do this without much delay?
First consider changing the task from "name contains substring" to "name starts with substring". If this is possible, then add index on your name column in database table and use the query:
select name from table where name like :1 || '%'
Limit the number of returned rows using DBMS-specific syntax, for example, for Oracle add
and rownum < 20
This query should return your rows pretty fast.
If you really need "contains substring", then decide whether you want the search to be handled by database or by an external text indexing solution.
For database-contained solution you'll have to use a different approach depending on DBMS. Every one of these solutions requires configuration steps not described here.
For Oracle you can use Oracle Text, see
http://www.oracle.com/technetwork/documentation/index-098492.html
The query will look like
select name from table where contains(name, :1) > 0
For Postgres you can use Full Text Search.
You can also use a solution that is not dependent on the database, for example, see Apache Solr:
http://lucene.apache.org/solr/
for example
SELECT name
FROM Table
WHERE name LIKE '%sea%'
I want to generate unique keys automatically in solr. I checked the default function here
but it is generating id like 1cdee8b4-c42d-4101-8301-4dc350a4d522. In my application, I need unique autoincrement numbers like we do in MySql. What should be approach to do this ? Solrj pointers would be much helpful.
Another solution (hack) that I've implemented is to create a record in solr inside the existing schema. For example if you have a schema which has 2 string fields then you can store the values as MAX_VALUE and the other being the actual integer max value stored as string. So anytime you would add, you'd have to query for "fieldname:MAX_VALUE" and retrieve the string value from the other field of the same document. You can parse it and add 1. You then update the existing MAX_VALUE document. It's not the most feasible but it is a solution. The implementation keeps your max number within your index rather than in another application.
It's also solj friendly as it's fairly straight forward to make the query and the update query.
I apologize for the grammar. Do comment if you can't understand what I'm saying.
Search Database with Keyword:
Is there a way to query the entire table of SQLite database for matching word. I am trying to place a word in keyword JTextField for it search the entire Job table and to return rows which contain that matching words. Each row representing a unique job.
I presume this below snipped structure would not achieve the result
SELECT * FROM Job WHERE Job MATCH 'Microsoft';
Any quick and simple recommendations?
It appears you can use the syntax you're suggesting as long as it's a full text search table:
http://www.phparch.com/2011/11/full-text-search-with-sqlite/
You should probably read the official SQLite documentation on FTS tables. Note that these are "virtual" tables and as such don't use the database file in the same way - this might not be what you want, but it matches the syntax you're looking for. The SQLite page linked has pretty good descriptions. Note that the FTS module is an optional extra which may not be installed.
You shouldn't need to worry about SQL injection attacks as long as you use placeholders. Look at this SO question, specifically the second answer with the code snippet. Essentially you put a ? in the statement, and then supply the string to substitute as a parameter to the query function. That should do SQL escaping for you.
i don't know sqlite but in mysql we have full text search which is used as
select * from table where match(name) against('value')
just see weather full text search is there or not in sqlite, i found this link verify it
http://www.sqlite.org/fts3.html#section_3
In my aplication I use eclipselink and the criteria api.
My database have a table with a column called "order".
The problem is when I use the criteria api to create a select it made this sql:
SELECT id, order, name, phone, uri FROM campus
It throw a exception because "order" is a restrict keyword in sql
How can I force the criteria api to put quotes in the columns names?
The easiest (and IMHO the best) way is to change order to campus_order or something like that and avoid using SQL keywords as a field identifier. This practice typically causes problems.
I will be glad to know that criteria API has some kind of work around for this problem but I'd recommend you to rename the column. Today you are using criteria API, tomorrow you will use something else... But at the end of the day the good old SQL is generated and the last think you want is to find that one of your queries does not work because the column name equals to keyword of one of SQL versions.
Renaming the field is an easier option, but JPA will quote delimited fields. To mark it as delimited, just add quotes when defining the column: "\"order\"".