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();
Related
How do I alter column in sqlite?
This is in Postgresql
ALTER TABLE books_book ALTER COLUMN publication_date DROP NOT NULL;
I believe there is no ALTER COLUMN in sqlite at all, only ALTER TABLE is supported.
Any idea? Thanks!
There's no ALTER COLUMN in sqlite.
I believe your only option is to:
Rename the table to a temporary name
Create a new table without the NOT NULL constraint
Copy the content of the old table to the new one
Remove the old table
This other Stackoverflow answer explains the process in details
While it is true that the is no ALTER COLUMN, if you only want to rename the column, drop the NOT NULL constraint, or change the data type, you can use the following set of dangerous commands:
PRAGMA writable_schema = 1;
UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE BOOKS ( title TEXT NOT NULL, publication_date TEXT)' WHERE NAME = 'BOOKS';
PRAGMA writable_schema = 0;
You will need to either close and reopen your connection or vacuum the database to reload the changes into the schema.
For example:
Y:\> **sqlite3 booktest**
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> **create table BOOKS ( title TEXT NOT NULL, publication_date TEXT NOT
NULL);**
sqlite> **insert into BOOKS VALUES ("NULLTEST",null);**
Error: BOOKS.publication_date may not be NULL
sqlite> **PRAGMA writable_schema = 1;**
sqlite> **UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE BOOKS ( title TEXT NOT
NULL, publication_date TEXT)' WHERE NAME = 'BOOKS';**
sqlite> **PRAGMA writable_schema = 0;**
sqlite> **.q**
Y:\> **sqlite3 booktest**
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> **insert into BOOKS VALUES ("NULLTEST",null);**
sqlite> **.q**
REFERENCES FOLLOW:
pragma writable_schema
When this pragma is on, the SQLITE_MASTER tables in which database can be changed using ordinary UPDATE, INSERT, and DELETE statements. Warning: misuse of this pragma can easily result in a corrupt database file.
[alter table](From http://www.sqlite.org/lang_altertable.html)
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table. But you can alter table column datatype or other property by the following steps.
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT
For more detail you can refer the link.
CREATE TABLE temp_Table(x,y[,etc]);
INSERT INTO temp_Table SELECT * FROM Table;
DROP TABLE Table;
ALTER TABLE temp_Table RENAME TO Table;
Thanks for helping me to find a definitive method!
ALTER COLUMN does not exist in SQLite.
Only Supported alter operations:
Alter Table Name
Alter Table Column Name
Add New Column
Drop Column
Alex Jasmin's answer shows possible way
Reference:
Sqlite Alter Table
I have a link table event_user with a composite key based on an user_id and event_id.
I have an array of (user_id, event_id) pairs in Java where i want to ensure that there doesn't exists any other (user_id, event_id) pair in the table that doesn't exists in the array.
In other tables, i just created a string of ids, and then i created the following query
DELETE FROM tablename WHERE column NOT IN ( 1 , 2 , ... n)
However this behavior with NOT IN can not be achieved with multiple columns.
How can achieve that using Java and sqlite efficiently?
Thanks in advance.
In SQLite, IN works only with a single column.
So you have to write out all comparisons explicitly:
DELETE FROM TableName
WHERE (Col1 != 1 OR Col2 != 10)
AND (Col1 != 2 OR Col2 != 20)
AND ...;
SQLite can build an index for a large IN list, but this is not done for large expressions like this.
So if the query becomes too big, you can put the IDs into a temporary table, and use a subquery to check for matching rows:
DELETE FROM TableName
WHERE NOT EXISTS (SELECT 1
FROM IDsToKeep
WHERE IDsToKeep.Col1 = TableName.Col1
AND IDsToKeep.Col2 = TableName.Col2);
For efficiency, the temporary table should be indexed (or just be a WITHOUT ROWID table with a PK on both columns).
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.
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?
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