hello I am trying to use the sqlite database on android to push an integer array and store it in the database then use it and update it on demand. what i do not understand is that android has a create or open function for the sqlite database
what is the default value of created database containing an integer column? is it 0? and if i am running the program for the first time i should use the create then each consecutive run should use open how can i separate them ?
i have created the databases using
SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS MyTable(isChkd INT(1));");
Android uses SQLite. If you want to be sure just specify a default value, as seen in the documentation, resp. here:
CREATE TABLE IF NOT EXISTS MyTable (isChkd INT(1) DEFAULT 0)
By default, there will not be any values in a table that you have just created and not entered any data in. It will resolve to null and if you did try to access data without having made any entries, you will get a NullPointerException. You could set the default value to be entered in a column by setting the DEFAULT flag.
Once you have created a database in onCreate(), you only need to call getReadableDatabase() or getWritableDatabase() on an instance of an object of the class which extends SQLLiteOpenHelper to execute your queries.
Related
I have an app released in app store
I want to add a new column to the user table in the sqlite db and want it to be not null
But I also want old users to be able to use the app
What will happen if an old user updates their app with the new version? At login, I get the info from the server and I insert it in the db. The db insertion will probably stop when there is no value for the new column
Also, how do I do the upgrade itself?
in onUpgrade I do "ALTER table USER..."
in the constructor of the SQLite helper I add the new DB version
what else?
Also, I've added 3-4 totally new tables needed for new features. I've called their create queries in the onCreate method of the SQLite helper. Should I do anything else in addition to this?
in the constructor of the SQLite helper I add the new DB version
That will trigger onUpgrade(), good.
However, you cannot use ALTER TABLE to add a column with NOT NULL.
Here's what you can do in onUpgrade() to preserve user data:
Rename the old table to a temporary name
Recreate the table with the new column and NOT NULL
Populate the new table from the old temp table and supply the new column a reasonable non-null default value
Drop the temporary table
Can you give me an example lets say table is called USER with fields NAME and EMAIL and now I want to add a new field AGE?
Here's an example:
sqlite> create table user(name, email);
sqlite> insert into user select 'foo','bar';
sqlite> alter table user rename to user_temp;
sqlite> create table user(name, email, age not null);
sqlite> insert into user select name,email,-1 from user_temp;
sqlite> drop table user_temp;
sqlite> select * from user;
name|email|age
foo|bar|-1
Also, I've added 3-4 totally new tables needed for new features. I've called their create queries in the onCreate method of the SQLite helper. Should I do anything else in addition to this?
should I put the new creates for the new tables in both onCreate and onUpgrade or only in onUpgrade?
Make sure the same new tables are created in onUpgrade().
onCreate() is only run when the database is created for the first time, not on upgrade.
After both onCreate() and onUpgrade() the database schema (table structure) should be compatible. How you implement it is up to you. Putting the CREATE TABLEs there in onUpgrade() is an option. Some people prefer to call onCreate() insinde onUpgrade(), which can cause some headache when trying to migrate old data.
In Android, which I think uses SQlite3
If you create a table with two TEXT values
Then you go to make an entry in that table but specify no value for the second TEXT field, what will happen?
I'm new to SQL, just started MySQL and this SQlite.
the default value probably would be null
I currently have a layout with an EditText field, Datepicker, and a button. I'm trying to find a code that would make it so when the button is pressed, the values of the datepicker and edittext are stored in a SQL file so that it may be retrieved later.
I'm fairly new to using the SQLite functionality on android, and couldn't find a solution to this myself. Thanks!
Firstly read documentation regarding SQLite database from
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.de/articles/AndroidSQLite/article.html#overview_sqlite
To store value on click of button follow the steps.
Create DbHelper class extends SQLiteOpenHelper in which you can create table for required values to store in database for e.g. db.execSQL("create table "+EXAMPLE_TABLE+" ("+EDIT+" text, "+DATE+" text )");
Create a class ExampleProvider extends ContentProvider which manages queries like insert, query(select) , delete and update.
After that on click on button retrieve the values from edit text and datepicker , then store it into one object and then using resolver fire the insert query.
Check out from commandline whether values inserted into db or not.
Similarly you can retrieve values from your databases using query() method which retrieve data in form of cursor .
I'm trying to add a column to my database table which was created a while back and every time i try to run the app with the new column in the database adapter it crashes.
Why is this happening? I have changed the name of the database so it acts like a fresh table but this still doesn't work..
Please help????
If you are using a subclass of SQLiteOpenHelper as a lot of the Android examples suggest, you need to increment the DB_VERSION int that gets passed to the constructor. Otherwise the onUpgrade method doesn't get called and your db schema doesn't change.
How can we execute pre-database sqlite statements in Android?
I have created my own database layer. This layer will create the database and even the queries. It has a method that will create the table by providing the table name, column names and column types as parameters to it and the table will be generated without writing the full length query. So, to implement this I have created a test application and inside the onCreate method I have used my method that is going to generate the CREATE TABLE query and execute it. But as the database is not yet created it's throwing a NullPointerException.
So how can I fix this exception?
You need to use OpenHelper for this. Override it's OnOpen method to make actions on database open.
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onOpen%28android.database.sqlite.SQLiteDatabase%29
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/