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/
Related
I wanna insert some row to a table in Android, I'm using this:
INSERT INTO MyTable (Column_1, Column_1) VALUES ('X',100);
The query runs and no exception is thrown, but when I retrieve all rows from MyTable, no row
is returned.
I do not want to use the insert method, because the queries are read from a file and I want to insert them to the database.
What's wrong with my code?
Update : The rawQuery() method doesn't run the query, but execSQL() does.
The title of your question suggests you're using rawQuery(). It just compiles the SQL but does not run it. Calling one of the moveTo...() methods on the returned Cursor would also execute the SQL.
For an insert query, use execSQL() instead of rawQuery(), even if the documentation incorrectly states it should not be used with INSERT.
How is your Table organized?
You seem to use twice the same column Column_1 is this a typo?
Alternative, you could use insert-command of SQListeDatabase insteat:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java.lang.String,%20java.lang.String,%20android.content.ContentValues%29
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.
I'm using SQLite in android application and i need to execute some complex insert statement (with subqueries).
I'm found SQLiteStatement very useful for this purpose. It can be compiled and executed many times as insert statement returning autoincremeneted primary key.
Since i'm closing my SQLiteOpenHelper (which closes contained SQLiteDatabase) on Activity#onPause() and re-open it in Activity#onResume() i will get different SQLiteDatabase instances time-by-time.
Should i re-compile query for each SQLiteDatabase instance?
Or i can cache compiled statement in static variable and succesfully use it for different SQLiteDatabase instances?
The source code suggests me that i should (SQLiteStatement contains reference to SQLiteDatabase)... But i'm not sure.
Any help is greatly appreciated.
SQLiteStatement objects are associated with a specific SQLiteDatabase object, and cannot be moved to another one (there is no function for that, and the result of the compilation wouldn't work in any other database anyway).
You have to create new statement objects for your new database object.
I am currently studying SQLite and I have found that it uses various classes like ContentValues for insertion and updation... I was wondering whether I have to follow the given way or can I write a normal SQL query and use db.execSQL() method to execute them?
Will it bring any inconsistency to my database because with these all "extra" steps doesnt it stop the flow of the query and I feel it would be faster if we use a query directly.
You can do any SQL command you want with db.execSQL except select command or any other SQL command that return data (you use db.rawQuery() for this). The classes used are helper classes that make it easy for you to manipulate DBs (try inserting 100 rows with 20 columns each using ContentValues and db.execSQL and you will get the point). For small tables it will not differ much (and you will not cause inconsistecies), however, for large tables with inputs that depend on user interface or use calculations, it might be useful to have a class like ContentValues with its helper methods.
Yes you can definitely use this way like using
myDB.execSQL("INSERT INTO MyTable VALUES ('fffff', 'numb', 20)");
to insert values but only when you are using database for small queries.
Also there are some flaws using direct methods which gets removed using ContentValues
For example,try to insert a blob into the database using this method ,you will get a null bitmap while converting the retrieved data to bitmap.But when you insert using ContentValues,you will get the correct data i.e you will be able to convert that into Bitmap.
I have a pre-established SQLite Database in my application. It has a table with rows about 20 rows of text. I want to be able to add additional rows to the table without deleting all of the previous information. The only way I have seen which would allow me to do this is to delete all of the previous databases and then recreate it with the new rows. There must be a better way. Thanks for your help!
Are you confusing rows with columns?
If you really do mean rows then as antlersoft points out, using the SQL INSERT INTO statement will simply add a new row to a table without affecting any existing table data. This is one of the most basic and commonly used SQL statements.
If you actually mean you need to add columns then use the SQL ALTER TABLE statement.
See..
SQL INSERT INTO statement
SQL ALTER TABLE statement
The Android framework, as it relates to SQLite (using a SQLiteOpenHelper) provides two distinct methods for handling database lifecycles - onCreate(), used when the database needs to be created from scratch, and onUpgrade(<database>, int oldVersion, int newVersion) for handling updates. You can specify the "new" version number in the constructor for the superclass of your SQLiteOpenHelper, and the framework knows to call onUpgrade() based on this parameter and the internal version # in the actual sqlite database.
So, to modify your database during a version change just override onUpgrade() and run whatever SQLite stuff that you need.