SQLite query not work on Android rawQuery - android

Edit: Problem solved, query is correct. My problem is;
I work with local database. And i'm not reach database directly from assets folder. My code copy database from assets folder to SD card when database not exits on SD card. Therefore my database changes only effect database in asset folder. And i was tried right query on old database.
Sorry for the question.
I tried my SQLite query on DB Browser for SQLite and it's worked.
But on Android same query not worked.
String[] a = new String[1];
a[0] = yazilanKelime + '%';
Cursor friendCursor = db.rawQuery( "SELECT * FROM kelimeler WHERE kelime LIKE ? ORDER BY sayi DESC LIMIT 10", a);
If i remove "ORDER BY sayi DESC" part, it's work. Where am I doing wrong?
UPDATE: Return this Exception: android.database.sqlite.SQLiteException: no such column: sayi (code 1): , while compiling: SELECT * FROM kelimeler WHERE kelime LIKE ? ORDER BY sayi DESC LIMIT 10
DB Schema:

As the exception says, no such column: sayi means that the column sayi does not exists in your table kelimeler. Check this first.

Related

Why can't I perform a simple count in a subquery?

I'm trying to write a simple query that fetches all the columns of a specific table, but also adds a count of rows from a related table.
Here is the query:
SELECT
todos.*,
(SELECT COUNT(assets.*) FROM assets WHERE assets.parent_id = todos._id) AS asset_count
FROM todos
Here is the error:
Caused by: android.database.sqlite.SQLiteException: near "*": syntax error (code 1): , while compiling: SELECT todos.*, (SELECT COUNT(assets.*) FROM assets WHERE assets.parent_id = todos._id) AS asset_count FROM todos
try replace COUNT(assets.*) for COUNT(*) or COUNT(assets._id)
my second suggestion, try running the inside select like a single one
SELECT COUNT(*) FROM assets WHERE assets.parent_id = 1 <-- any _id you have

SQLCipher join two database tables

I successfully attached DB to another DB using SQLCipher like this:
ATTACH DATABASE '/storage/emulated/0/Android/data/testApp/files/dbTest1.s3db'
AS dbTest1 KEY 'Password';
I am not getting any error. When I try to use this query:
SELECT column1, column2, column3 FROM dbTest1.SaTTest
UNION SELECT column1,column2,column3 FROM SatTest
WHERE (Address1 LIKE '%cor%' OR column1 LIKE '%cor%')
I am getting this error:
I/Database(2587): sqlite returned:
error code = 1, msg = no such table: dbTest1.SaTTest.
How can i use alias in attached database tables?
I believe it's just a typo, it should be dbTest1.SatTest instead of dbTest1.SaTTest. Please always keep in mind case-sensivity

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();

Error: No such column name

I have been trying to execute a query:-
String selectQuery="SELECT "+ROLE+" FROM "+TABLE_EMPLOYEE+ " WHERE "+USER_ID+ "='"+userId+"' AND "+PASSWORD+"='"+password+"';";
cursorObj = dbObj.rawQuery(selectQuery, null);
This will result to:-
SELECT Role FROM employee WHERE User_Id='HondaSE' AND Password='456';
The logcat says:-
01-08 12:05:10.070: W/System.err(9318): android.database.sqlite.SQLiteException: no such column:
HondaQE: , while compiling: SELECT Role FROM employee WHERE User_Id=HondaQE AND Password=123;
I have tried to run the query with double quotes as well, for userId and password. resulting in:-
SELECT Role FROM employee WHERE User_Id="HondaSE" AND Password="456";
However both the queryies work perfectly fine when executed in SQLITE Data browser.
Both respond with same error.
Your logcat shows as below.
while compiling: SELECT Role FROM employee WHERE User_Id=HondaQE AND Password=123;
there is no single quotes around strings in User_Id=HondaQE AND Password=123.
You weren't compiling your raw query correctly. I would advise you to use "query" instead and use selection args to avoid these mistakes and possible sql injections.
Rewrite like this
String selectQuery="SELECT "+ROLE+" FROM TABLE_EMPLOYEE " WHERE USER_ID ='" + userId + "' AND PASSWORD = '" +password+ "'";
I had the closed the db before accessing the cursor. This was the error.
After accessing the cursor, the db is to be closed. Error solved.

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.

Categories

Resources