0

I've watched countless tutorials and examples about SQLite on Android but I just don't get some things. could someone please explain some things to me?

This the code that I came up with until now (SQLite helper):

public class SQLiteHelper extends SQLiteOpenHelper {

public static String DATABASE_NAME = "urnik";
public static int DATABASE_VERSION = 1;

public SQLiteHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

//Tabela prva ura
public static String IME_TABELE = "prva_ura";
public static String ID_PREDMETA = "id_predmeta";
public static String NAZIV_PREDMETA = "naziv_predmeta";

String prva_query =
        "CREATE TABLE " + IME_TABELE + " ("
        + ID_PREDMETA + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + NAZIV_PREDMETA + " TEXT" + ");";


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(prva_query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onCreate(db);
}

So as far as I know, this should create a new database and one table called "prva_ura". But I have a lot of questions about this.

  • I only created this class, I haven't attached it to MainActivity or anything, will the database still be created when app is ran for the first time or do I have to do something else?

  • I'm assuming the database is created when the app is launched for the first time after creating database. Can I find the database file anywhere to check if everything is as it's supposed to be?

I'd appreciate some tips in general, thank you!

Guy
  • 6,414
  • 19
  • 66
  • 136

4 Answers4

0

All you have to do is create an instance of your class, and the database plus all tables get created.

Once the database is created, then next time your app runs, the onCreate() doesn't get called again.

As for checking that everything has been created properly, you can use the sqlite command line client

http://www.sqlite.org/sqlite.html

Details of the database location on your device are here Location of sqlite database on the device

Community
  • 1
  • 1
Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47
0

Yes and yes.

I would advise you to look into motodev as this give you a nice database view in eclipse which is much easier to work with. It also handles generating ContentProviders for you so you do not have to write code as you have done above, simply define your database using the database view and generate the ContentProviders.

Saved me a lot of hours.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • BTW, MOTODEV Studio is no longer available from Motorola. You might be able to find it on a third-party site. The database tool and some of the other plugins should continue to work with newer SDKs, but IIRC, the last official Tools version we supported was 20.0.1 with Platform version 4.1.2. – Eric Cloninger Mar 27 '14 at 15:16
0

The database is first created when either getReadableDatabase() or getWriteableDatabase() are called. These methods will create the db if it doesn't exist or open an existing one.

So for you to create it, just query it. Write some CRUD methods and the first one that is called will create your db.

To upgrade your database, simply increment the database version and it will auto update.

From dev page getWriteableDatabase() http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase()

Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/or onOpen(SQLiteDatabase) will be called.

Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close() when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.

Matt R
  • 1,276
  • 16
  • 29
0
  1. You need to call the onCreate method first probably in your main activity or any other activity to create your activity when yor app runs.

Change your onUpgrade code to this


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

}
  1. Yes there is a place where you can find your database if you switch from java to DDMS
mojoblanco
  • 683
  • 1
  • 7
  • 17