sqlite local variable as table name - android

I am trying to use a rawquery like this:
cursor = helper.getReadableDatabase().rawQuery(
"select _id from inAd where Name like '%"+str2+"%'",
null);
My problem is, inAd is a local variable.
How can I use such a one as a table name?

Related

Android: ViewBinder & SimpleCursorAdapter SQL woes

I am struggling to get more complicated queries to work with SimpleCursorAdapter and ViewBinder with a ListView. When I was just returning all entries in my table, that was no problem. However, I want to return a list of artist names from my tables in order of name. The big problem concerns the "rowid _id" field which SimplerCursorAdapter/ViewBinder expects.
My code worked fine when I had queries of the form SELECT rowid _id, Artist.NAME etc, but I want to use the DISTINCT keyword to return the unique set if artist names. I can't put "rowid _id" before "DISTINCT Artist.Name" and I can't put it after. What is the solution for this?
The query I want (A) is (shown without the "rowid _id"):
String sQuery = String.format( "SELECT DISTINCT Artist.Name, Artist.ID FROM Artist JOIN Tune ON Artist.ID=Tune.ArtistID AND Tune.Type=%d AND Tune.SubType=%d ORDER BY Artist.Name", nType, nSubtype );
To clarify, this works:
Cursor c = db.rawQuery( "SELECT rowid _id, Name, Rating FROM Tune ORDER BY Name", null );
Whenever I put rowid _id back into query (A), I get "no such column rowid" exceptions:
String sQuery = String.format( "SELECT rowid _id, Artist.Name, Artist.ID FROM Artist JOIN Tune ON Artist.ID=Tune.ArtistID AND Tune.Type=%d AND Tune.SubType=%d ORDER BY Artist.Name", nType, nSubtype );
What am I doing wrong?
EDIT: I don't even understand what the "rowid _id" does anyway - my SQLite Manager (test tool) doesn't like it either when I have a query with a join. It only seems to work on a simple 1 table query.. So if thats the case.. how do I make this query work without it for SimpleCursorAdapter & ViewBinder?
The answer was to forget about using rowid and use my own Artist.ID field instead. This will work as long as I alias the field name to _id which SimpleCursorAdapter expects in column 0.
String sQuery = String.format( "SELECT DISTINCT Artist.ID _id, Artist.Name FROM Artist JOIN Tune ON Artist.ID=Tune.ArtistID AND Tune.Type=%d AND Tune.SubType=%d ORDER BY Artist.Name", nType, nSubtype );

Insert statement in sqlite android with db.ececSQL statement

I just want to insert values into a table if the value provided does not exists in that table, I mean I have provided a column as UNIQUE, so sqlite3 UNIQUE constraint will be broken when that value is tried to input twice, i want an sqlite3 insert statement which helps to do this my code is. I read that INSERT IGNORE is used for this purpose. Can somebody provide me with syntax to do this correctly?
My code is given below.
String insertQuery1 = "INSERT INTO Bookdetails bookpath,lastchapter VALUES(?,?)";
db.execSQL(Query1, new String[] { filepath, none });
What is the correct syntax for this query? filepath and none are string which have values assigned
Also this table Bookdetails has a primarykey field 'id' which is auto increment? will it create any problems when data is inserted like this way?
String insertQuery1 = "INSERT INTO Bookdetails (bookpath,lastchapter) VALUES(?,?)";
db.execSQL(Query1, new String[] { filepath, none });
Change
INSERT INTO Bookdetails bookpath,lastchapter VALUES(?,?)
to
INSERT OR IGNORE INTO Bookdetails (bookpath,lastchapter) VALUES(?,?)
The OR IGNORE causes that when a constraint such as UNIQUE is violated, the insert doesn't take place and there won't be an error.
The column names to insert to need to be in () parens.
Additionally, you're passing some other query to execSQL() than the insertQuery1 here.
Also this table Bookdetails has a primarykey field 'id' which is auto increment? will it create any problems when data is inserted like this way?
No, since no insertion takes place.
If you had INSERT OR REPLACE instead of the ignore, it would translate to a DELETE followed by INSERT, generating a new row id in case the id was not specified in the insert itself.
The query is wrong:
INSERT INTO Bookdetails bookpath,lastchapter VALUES(?,?)
It should be:
INSERT INTO Bookdetails (bookpath, lastchapter) VALUES (?, ?)
Mind the parentheses surrounding the fields list!

Table count in SQLite

I copied existing SQLite database from assets to databases folder. When I tried to retrieve data from one of table(gloss) it is giving an error:
android.database.sqlite.SQLiteException: no such table: gloss: , while
compiling: SELECT DISTINCT id, fk, lang, value FROM gloss WHERE value
like '%a%'
I want to make sure that all tables are copied to new db inside 'data/data/package/databases/'
How to get count of tables in SQLite db?
Why are you moving it from /assets? To count tables you should use this query:
SELECT count(*) FROM sqlite_master
WHERE type = 'table' AND name != 'android_metadata' AND name != 'sqlite_sequence';
Edit**
It seems that my implementation was wrong. Use the query with the other answer.
Thanks to #CL from the comments:
String SQL_GET_ALL_TABLES = "SELECT count(*) FROM sqlite_master
WHERE type = 'table' AND
name != 'android_metadata' AND
name != 'sqlite_sequence';"
return db.compileStatement(SQL_GET_ALL_TABLES).simpleQueryForLong();

SQL open a table confusion

I needed to store some data related to class periods in an Android project. After looking at my options, I thought a SQL database would be a good choice. Unfortunately, I can't seem to get my statement to actually open a database. My open database statement string goes like this:
"create table "+ DATABASE_PERIOD+" (_id integer primary key autoincrement,"
+KEY_CLASSTITLE+" text not null, "+KEY_PERIOD+" text not null,
"+KEY_XPERIODS+" text not null, "+KEY_DOUBLEPERIODS+" text not null);
I based it off of the Notepad project on the Android website. I just added a few more fields, but it is unable to open the database. If you guys want the error message or some other kind of info, I'll try to get it for you (This is my first time with SQL so I don't really know what is needed to fix this up). Thanks in advance!
The error message I'm getting goes like this:
android.database.sqlite.SQLiteException: no such table: periodData: , while
compiling: SELECT DISTINCT _id, title, period, xPeriods, doublePeriods FROM
periodData WHERE _id = -1
And as it says there, I'm using SQLite.
My code to put some data into the table in my Database Helper class:
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CLASSTITLE, title);
initialValues.put(KEY_PERIOD, period);
initialValues.put(KEY_XPERIODS, xPeriods);
initialValues.put(KEY_DOUBLEPERIODS, doublePeriods);
return mDb.insert(DATABASE_PERIOD, null, initialValues);
I started using private static strings to ensure that my calls aren't incorrect (Thanks Barak for the catch!)
Your issue is the table name (as the error message indicates). From the code you show you created a table called "period". Yet you try to query it using the table name "periodData".
Two ways to fix it:
1) Change the table name in your database to periodData (more difficult as it involves re-creating the db).
2) Change the table name in your query from "periodData" to "period".
Check out the dev topic on http://developer.android.com/guide/topics/data/data-storage.html#db
You'll create a DbHelper class to help you open the database and your code will look something like:
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
Hope this was helpful.

SQLIte Query, select everything that equals - Android

// get friends
public Cursor getFriends(){
Cursor c = dataBase.query(SqConstants.FRIENDS_TABLE_LOCAL, null, null, null, null, null, null);
return c;
}
I have multiple tables in a SQLite database in my application for android.
I want to select everything from a column in a Friends Table (which holds integers) and select everything that equals that integer from a User Table (in the primary column which also is integer) and the get all the users which equals the numbers stored in the friends column.
It was some while I managed SQL databases but I think the query for a MySQL database would be something like:
SELECT * UsersID FROM TABLE Users EQUALS LOCAL_FRIENDS_ID FROM TABLE Friends
I might be wrong that it would look like this, but something like it.
What I want is to do this in android code, from a cursor get all the Users from the Users table which ID is equal to the FriendsID.
How do I change the above method to make it so?
You could just use the rawQuery
Cursor c = dataBase.rawQuery(SELECT * UsersID FROM TABLE Users EQUALS LOCAL_FRIENDS_ID FROM TABLE Friends, null)
If you need to use a actual parameter for the LOCAL_FRIENDS_ID
string[1] args = new String[LOCAL_FRIENDS_ID.ToString()];
Cursor c = dataBase.rawQuery(SELECT * UsersID FROM TABLE Users EQUALS ? FROM TABLE Friends, args)

Categories

Resources