How to check the values of an android sqlite db table? - android

I really have two questions:
1/ how to read/check sqlite db table offline?
2/ Why is a sqlite table column of boolean type set to NULL?
ad 1/ I have downloaded sqlite3.exe and when i use the cmd > .dump quotes_table i see all rows like
INSERT INTO quotes_table` VALUES................
INSERT INTO quotes_table` VALUES................
INSERT INTO quotes_table` VALUES................
INSERT INTO quotes_table` VALUES................
etc
Is this correct as i was expecting to see just the values and not the queries?
ad 2/ I alter+updated the quotes_table by
db.execSQL("ALTER TABLE quotes_table ADD COLUMN quoteUsed boolean");
db.execSQL("UPDATE 'quotes_table' SET quoteUsed = 0");
I read on http://www.sqlite.org/datatype3.html sqlite use 0 and 1 for boolean type so i thought putting quoteUsed = 0 should be OK, but the value reads NULL.
How can i fix this?
regards,
ps: FYI just installed SQLite manager addon for Firefox and it makes things easier

1/ how to read/check sqlite db table offline?
Normally, SQLite is used with local databases, so database exists or does not exists - "offline" doesn't apply.
2/ Why is a sqlite table column of boolean type set to NULL?
SQLite doesn't have a boolean data type. It's affinity is NUMERIC. Check Datatypes In SQLite Version 3
NULL values, as in other DBMS, means nothing assigned.
when i use the cmd > .dump quotes_table i see all rows like ...
DUMP is used to extract all data from database. Actually, it returns SQL statements you may use to rebuild a new database from scratch.
To get data only, use SQL queries (check SELECT statement).

when i use the cmd > .dump quotes_table i see all rows like
...
Is this correct as i was expecting to see just the values and not the queries?
Yes, .dump creates the SQL that would produce an identical database.
2/ I alter+updated the quotes_table by
...
I read on http://www.sqlite.org/datatype3.html sqlite use 0 and 1 for boolean type so i thought putting quoteUsed = 0 should be OK, but the value reads NULL. How can i fix this?
Nothing really wrong with the SQL. How are you reading the value?

Related

SQLite Check if Table is FTS4

I'm developing an Android app that uses a SQLite database with FTS4 tables.
In the app there's an option to import a database from the external memory. This database needs to be checked to confirm that it has all the correct tables and columns. I already have the code to do that however I don't know how to check if the tables are "normal" or FTS4. This will result in problems later on with queries with MATCH on them.
The only way I can think of to check if the tables are FTS4 is to do a random query with MATCH and if it gets an error it's because they are not.
Is there a better way to do this like with just a command?
Using MATCH on a plain table results in an error message only if the table has at least one row.
FTS table have a virtual column with the same name as the table name. So you could try a query like SELECT MyTable FROM MyTable.
You could check whether the shadow tables (MyTable_content, MyTable_segdir, etc.) exist.
You could check the CREATE TABLE statement in the system table: SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'MyTable';

Is it possible execute a sql Insert sentence in an Android SQliteDatabase.query comand?

I have a simple sql database with two fields and I would like to insert some values in the database with the sql insert comand. Is it possible ?
The database is the following :
id valueA valueB
Thanks
Gorka
yes in some case its true, SQLLite omit some features of SQL but in insert they work
For Information click hear

Changing a Column type in my Android App's Database

I have an app published in the play store.
The app uses a database which holds a table which has a column of type int.
I'm doing a new change where I need to change the column type to long.
How do I go about handling it in the DatabaseHandler I'v created.
I want to preserve the data stored in the older apps database, so what should ideally be the code in the onUpgrade() function???
You don't need to change the database column type. An INTEGER column will happily contain all the bits needed to represent a Java long.
In fact, there's no long column type in sqlite.
I think using SQLite, the best way is to create a temporary table, copy all your table content, drop the old table and recreate the table with the right type on your column, then you can just copy the content from the temporary table and drop it...
I know this don't fell like the best approach, but I don't think SQLite have some alter table function.
As far I know you can t do this . But You can drop your table if it exists and create it again . Maybe you can find out some useful information here SQLite Modify Column or here Modify a Column's Type in sqlite3

Need a good method to change (SQLite) column data type

I am having a table with 'int' column. During software upgrade, I want to change it to 'long' data type. It seems SQLite does not provide an option (in alter statement) to change/modify column properties. Since I want to do an upgrade, the table might contain data which the user should not lose. So please suggest me a good method to change the column data type property without data loss.
One way suggested in the search links is to create a temporary table, copy the records from the existing table, delete the existing table, and rename the temporary table. I doubt that is efficient.
Your help appreciated!
Regards
Vivek Ragunathan
I used the follow statements to change the type of the column.
CREATE TABLE IF NOT EXISTS **TEMP_TABLE** (id integer primary key autoincrement, **col2change integer not null**, ...)
INSERT INTO TEMP_TABLE SELECT * FROM EXISTING_TABLE
DROP TABLE EXISTING_TABLE
ALTER TABLE TEMP_TABLE RENAME TO EXISTING_TABLE
I changed the int type column in the existing table to integer type. For a few hundred rows, it was reasonably fast.
SQLite3 columns do not have data types, only affinities -- there is no benefit in changing the column type from int to long.
If a wrapper program is truncating values before giving them to SQLite3, there is a way to fix this by editing the schema. It is a dangerous operation, so do it only after backing up your database. The schema is stored in the table sqlite_master; normally it is read-only, but you can modify it if you enable it with the writable_schema pragma. Be careful to only make changes that do not invalidate your data; you may change int to long int since they both have INTEGER affinity.
From SQLite documentation
It is not possible to rename a column, remove a column, or add or
remove constraints from a table.
Check this link
Please do remember that column data types are not rigid in SQLite. Check this link
Edit:
Following your comments on another answer, I guess the option you mentioned - working through the temp table - is the only one, which is not efficient off course.
you could add a new colum, copy the values form the old to the new column, delete the old column and then rename the new column to the old name
AFAIK there is no way in Android to change column data types once a table is created and used. The practiced way is to make a new table and copy the data which you read about

Normal SELECT on an SQLite FTS Table

I'm having an issue performing normal SELECT statements (that is, not the 'searches' they were designed to do- I'm not using MATCH). What I'm trying to do is query the table for all rows of an INTEGER column. However, the SELECT statement always returns no rows. I've inspected the database with an SQLite browser and the query should work. Here it is:
Here's the MAKE TABLE statement:
CREATE VIRTUAL TABLE FTS_journal USING fts3(journal_id INTEGER, journal_text_col TEXT)
And here's the SELECT:
SELECT journal_id FROM FTS_journal
Does it have something to do with my running a 'normal' query over a virtual table? I can't really think of any other reason
EDIT: I'm using Android's version of SQLite, meaning SQLite3. I know it supports FTS properly...
If any further information I can provide will help, please tell me and I will post it.
What you've written should work. Are you 100% positive you've actually inserted something into the table? Have you ran insert statements, and if so, are you sure they were successful?
I tested your statements, running the create statement, then the following statement:
insert into FTS_journal (journal_id, journal_text_col) values (1, 'test');
... then your select statement, and sqlite returned 1 row (journal_id, value 1) just as it should.

Categories

Resources