I have a database now i have to check whether it is empty or not. if empty i have to write the code for insert values otherwise i have to update the values of the fields.
please help me..
You can try something like this:
// Open or crare
myDB = this.openOrCreateDatabase("MyDatabaseName", MODE_PRIVATE, null);
//Check if exists (if not create the table)
myDB.execSQL("CREATE TABLE IF NOT EXISTS MyTableName (Name1 VARCHAR, Name2 VARCHAR);");
//Insert or update your lines
myDB.execSQL("INSERT INTO MyTableName (Name1 , Name2 ) VALUES ('XXX', 'YYY');");
Have you done the Google Notepad tutorials 1,2,3? they have a bunch of database help
Hope that can help
It's possible to include a .db file in your application package and use that as the starting point for your device-side database. It goes in the assets folder. You'll have to manually copy the values out of that .db into the one on the device in your startup method, but it saves putting a bunch of SQL INSERT calls in code.
The MOTODEV Studio Database perspective will assist you with this. You can use it to create a .db from scratch and populate it with your default values. Then, there is a wizard that will set up a content provider and accessor classes for the fields if you need them.
Disclaimer: I'm the product manager for MOTODEV Studio.
Related
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
In my application, I want to delete my existing database and create a new one with default values. Default values can be inserted to the database from XML.
Does anyone have any idea on how to reuse a database?
Assuming that you are using a SQLite database, to delete all the rows from all of your tables inside your database you can use db.execSQL() and heed the advice from this question Drop all tables command:
You can do it with the following DANGEROUS commands:
PRAGMA writable_schema = 1;
delete from sqlite_master where type = 'table';
PRAGMA writable_schema = 0;
you then want to recover the deleted space with
VACUUM
and a good test to make sure everything is ok
PRAGMA INTEGRITY_CHECK;
If you haven't written a way to read your XML data yet, this is excellent reading: Store parsed xml data to sqlite ? Android
Well basically that's not an Android specific question.
Firstly when do you want to recreate the database with default values and how to trigget it.
In an UI event like button click etc?
Or when you start/stop or destroy your activity?
In any cases you need to drop the database and recreate the whole structure (tables,relationships etc.) again.
Hi im trying to read sqlite file which is placed in assets/databases folder
i followed this link to read data from sqlite file
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
im getting error
no such table : while compiling
SELECT _id, name, address FROM
stores
Is there any permission i need to write in manifest to read sqlite file data?
Please let me know how i can solve this issue. Or else please give me any reference link to follow.
Thanks in advance
The way you'd want to do this is to make a new DBManager object that creates the database in its onCreate method--that way when the DatabaseHelper is first instantiated, the table will be created for you. Then you'd instantiate one and call getReadableDatabase() on it to get a DB object which you can then query.
This tutorial may help you more, it's more succinct and up to date: http://developer.android.com/guide/topics/data/data-storage.html#db
After that, to set up the list view in a ListActivity, you can call setListAdapter and pass in a SimpleCursorAdapter. Here's a tutorial on that: http://developer.android.com/guide/appendix/faq/commontasks.html#binding
Check if database already exists or not.
If you are running on emulator.
write following on terminal/shell:
adb shell
cd data/data/your package name(ex. com.android.etc)
ls
if there exists databases directory then may be database is created
cd databases
ls
it will show your database if exists;
sqlite3 "your db name"
then write
.tables
it will show the name of table if exist:
now write your query over here to check for errors for ex:
sqlite> SELECT _id, name, address FROM stores
hope it helps.....
and yes there are no as such required permissions for this.
I try to integrate an existing database file into my Android project.
I follow the instructions on this blog. They write that I have to add a table android_metadata with a column called locale and put en_US into it.
I try to figure out what this table is used for. Because my database content is german. Maybe i then should not put en_US into it? Is this required for localisation of the database content or is the table not needed at all?
the metadata table will be generated automatically. if you have content of german try updating the metadata table 'de_DE'.
If you're okay opening the DB with read-write access then you can let the openDatabase(...) call automatically generate it. For example in Android:
SQLiteDatabase.openDatabase(m_szMainDBPath, null, SQLiteDatabase.OPEN_READWRITE);
The metadata table is required to hold (as its name suggests) meta information about the application. This table is auto-generated in some cases (since api 4 if i remember correctly) but you may want to add it yourself.
So far we have developed apps in android that create database on runtime. We like to know how can we access a pre-built or existing database/sqlite file in our android app? Please provide detail
Take a look at the documentation for android.database.sqlite.SQLiteDatabase.
In particular, there's an openDatabase() command that will access a database given a file path.
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);
Just specify the path to your database as the path variable, and it should work.
I have followed the same road and found two issues:
Include the _id field on any table you create and make sure it is
part of the columns to return when you query any table.
You may run into an error like Failed to open the database. closing
it.. This is because we are copying the database from an existing
file instead of creating tables on top of a db created by the sqlite
android API and the table android_metadata may not exist. For
creating it just run the following query on your db:
CREATE TABLE android_metadata (locale TEXT);
And add the corresponding locale, for example en_US
First copy your SDCARD and give it's path in the variable "DBNAME" in the following example.
it will be something like "/sdcard/persons.db" if you are directly pulling it to sdcard.
Use this for accessing database in application :
Context context = getApplicationContext();
context.getDatabasePath(DataBaseHelper.database_Name);