aspose file tools*
The moose likes Android and the fly likes Where are SQLite3 databases created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Where are SQLite3 databases created?" Watch "Where are SQLite3 databases created?" New topic
Author

Where are SQLite3 databases created?

Chris Trout
Greenhorn

Joined: May 05, 2010
Posts: 16
My apology in advance if I am posting this question in the wrong forum. I looked but did not find a better place.

My question is this - I am working thru the Hello, Android book examples and have successfully run and understand the Events database example on pages 170-180.
[ although I don't understand where the events.db file is being created.]
So, I decided to use the sqlite3 cmd line interface to create a different database, some tables, and write a small Android program to manipulate it.
I can invoke sqlite3 just fine from the cmd prompt, the db gets created and can create new tables, etc.
When I close sqlite3 I can see my new database.

What folder in my new Android program project should I copy my new datase to and how do I tell my code where to find it?

Thank you very much in advance to anyone who would like to offer some help.

-Chris
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

I believe it's "/data", although I seem to recall a possibility that apps may have their own directories that contain SQLite.

In actual practice, however, it's not common to define a SQLite database externally, but rather to have the Android app do the creation. That way the app is self-installing.


Customer surveys are for companies who didn't pay proper attention to begin with.
Chris Trout
Greenhorn

Joined: May 05, 2010
Posts: 16
Hi Tim,
Thanks very much for your reply.
As I was waiting for a reply I was thinking that I probably didn't do a very good job of explaining my problem, so I'm going to elaborate.

I am working exclusively in Eclipse currently as I don't have a phone.
I would like to use a SQLite look-up table in my app.
There is a database example in the Hello, Android book (starts on p. 170 as I recall).
It creates a sqlite db from within the app. That is interesting and I will use that technique in the future on some app I'm sure.
Although it doesn't seem to work exactly as advertised. It creates the db and tbl the first time thru but doesn't drop the tbl when it run it again. It just keeps adding
records to the table. Here is the relevant db code:

Here is the Constants interface -

package com.example.events;

import android.provider.BaseColumns;

public interface Constants extends BaseColumns{
public static final String TABLE_NAME = "events";

// define the Columns in the Events database
public static final String TIME = "time";
public static final String TITLE = "title";



}


Here is the class that extends SQLiteOpenHelper -

package com.example.events;

import static android.provider.BaseColumns._ID;
import static com.example.events.Constants.TABLE_NAME;
import static com.example.events.Constants.TIME;
import static com.example.events.Constants.TITLE;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class EventsData extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;

// create a helper object for the Events database
public EventsData(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME
+ " INTEGER," + TITLE + " TEXT NOT NULL);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int OldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);

}

}

* The tbl is never being dropped. I have set debuggable to try in my manifest file and set a breakpoint in this method to see if it is being invoked but it doesn't stop at that breakpoint or any other breakpoint I set in the app. I don't understand why?

I have looked all over for the events.db file but can't find it anywhere.
In actuality, I would like to have it be created under the "assets" folder in the Eclipse project structure for my Android project, but don't know how to tell to do that.
Would you be able to help me understand how to do that?
Once I understand that I can go onto my next little learning app where I put a look-up db into that same place and open it from there. Not exactly sure how to do an open on an existing db but will try and figure that out once this problem is solved.

Thanks again for your assistance as I am looking forward to hearing back from you.
-Chris
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

Hint: use the "Code" button when including code or XML samples. It makes things more readable.

No, it would be extremely rude for the emulator to destroy the database every time it started the app. I'd have a lot of grief if all my test data got wiped out every time I deployed a new version of an app. If you want to erase the database, you can do that easily from an Android Activity. You'll also find Android code samples available where a database adds/drops tables as part of a schema version upgrade process. For real-world apps, it would often be the case that an uninstaller would remove the database, as well as destroying any other persistent artefacts that the app might have created.

I don't know that the tables in SQLite are discrete files. I think maybe they're in a single "db" file. But then again, I don't care, since the Android OS/SQLite DBMS are taking care of the grungy implementation details for me, and I'm not (currently) doing anything that requires mucking around in the database internals.
Chris Trout
Greenhorn

Joined: May 05, 2010
Posts: 16
My apology for not attaching the code examples.
I'll blame that one on being new to the ranch...

I tried changing the version number of the db and that caused the tbl (not the db) to be dropped. That's good, so that problem is solved.

You mentioned that there are Android db code samples available.
I need to be able to state somewhere in the code where I want the db created initially. In my case I think it should be under that assets folder in the Eclipse Android project structure. Is that where db files should reside in the dev env? Or do people usually specify another folder under it called "databases"?

Finally, if you could point me to some relevant code example I would greatly appreciate it.

-Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where are SQLite3 databases created?