Im a bit new to OOP so i want to know if im doing things correctly. For communication with database i have created a class SQLiteHelper witch does all the usual stuff (onCreate, onUpdate) and also opens and closes connection.
Here is the class, at the moment it has just on table but more will be added:
public class SQLiteHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "notebook";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_LIST = "list";
public static final String TABLE_LIST_ID = "_id";
public static final String TABLE_LIST_NAME = "name";
public SQLiteDatabase db;
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table " + TABLE_LIST + "(" + TABLE_LIST_ID
+ " integer primary key autoincrement, " + TABLE_LIST_NAME
+ " text not null);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST);
onCreate(db);
}
public void open(){
db = getWritableDatabase();
}
public void close(){
db.close();
}
}
And next for each table i will create a new class witch extends previous class andd where i do all the operations relevant to that specific table.
For example ListSQL:
public class ListSQL extends SQLiteHelper {
public ListSQL(Context context) {
super(context);
}
public void delete(int id) {
open();
db.delete(TABLE_LIST, TABLE_LIST_ID + " = " + id, null);
close();
}
}
My question is that in OOP is this the correct way of doing things? Espesially the usage of open/close methods and db and TABLE variables in ListSQL seem kind of strange to me?
I always open the db connection in onResume() and close it in onPause(). In this way database is always open for each activity.
The reason I am not doing it in onCreate() and onDestroy() is once user go to other activity onCreate() of new activity will be called first then onDestroy() of old activity so if I perform any operation(ex:- search in my list or changing the status of user etc) on places other then onCreate() it will crash the app with reason database already closed.
Note: You have to open and close the connection, even if you are using SQLiteHelper class.
According to the Android manual, you do not need to close a database when using an SQLiteOpenHelper. The system will do it for you.
As #Dan mentioned above, you do no need to open and close the database every time you do a read/write operation if you are using SQLiteOpenHelper. The best way to use the database is :
1.
Declare and initialize an instance of SQLiteHelper in your Application base class like this :
public class App extends Application {
public static SQLiteHelper db;
#Override
public void onCreate() {
super.onCreate();
db = new SQLiteHelper(getApplicationContext());
db.open();
}
}
2.
In you activity, or any other place you want to use the DB, initialize the SQLiteHelper object like this :
SQLiteHelper db = App.db;
And then you can use the database anyway you want without having to worry about opening and closing it (:
Related
I am having a class named GameDb and it's extending SQLiteOpenHelper. I don't know why, but onCreate it's not called, the table is not created !
Having a singelton for all my activities and fragments:
public MyApp extends Application
{
// .... Code
private GameDb gameDb;
public GameDb getGameDb()
{
if (gameDb == null)
gameDb = new GameDb(this);
return this.gameDb;
}
}
Getting all games from database, calling from an activity
public class MyActivity extends FragmentActivity
{
#Override
protected void onCreate(Bundle bundle)
{
// Code....
// Get all games
ArrayList<Game> allGames = getMyApp().getGameDb().getAllGames();
}
}
Class which contains all necessary methods for querying the database (writing and reading). This methods are not written below because it's not necessary.
public class GameDb
{
private SQLiteDatabase db;
private GameDbHelper dbHelper;
public GameDb(Context context)
{
this.dbHelper = new GameDbHelper(context);
this.db = dbHelper.getWritableDatabase();
}
public ArrayList<Games> getAllGames()
{
// return db.query(....);
}
public void insertNewGame(int id, String name)
{
// db.query(....);
}
// Static inner class which extends SQLiteOpenHelper
private static class GameDbHelper extends SQLiteOpenHelper
{
public GameDbHelper(Context context)
{
super(context, "DB_NAME", null, 1);
}
// On create not called !
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE my_table(_id INTEGER AUTOINCREMENT, date TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
dropTable(db);
onCreate(db);
}
public void dropTable(SQLiteDatabase db)
{
db.execSQL("DROP TABLE IF EXISTS my_table");
}
}
}
When I'm trying to get all games, or add a new game I'm getting a FATAL EXCEPTION, (1) no such table: my_table
You may have a old database present on your device and database doesn't always get deleted when you reinstall the app using IDE. Thus you must uninstall the app manually and the reinstall your app using IDE.
I don't believe onCreate() gets implicitly called when you instantiate your database object. You need to explicitly call your Activity's openOrCreateDatabase() method.
Hey Folks am making a simple application in which ill insert two accounts manually into a table for the login authentication and then after that ill do some working based on the accounts type. Am having issues in setting up Sqlite functions. I have one package com.example.emp_management and in it i have 2 classes MainActivity.java and Database_Wrap.java. Code for the Database_Wrap.java is given below:
package com.example.emp_management;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Database_Wrap //hotornot
{
public static final String Database_name = "Employee_Managament_System";
public Sql_Lite_Work OurHelper;
public final Context OurContext;
public SQLiteDatabase ourDatabase;
public static class Sql_Lite_Work extends SQLiteOpenHelper
{ //dbhelper = sql_lite_work
public Sql_Lite_Work(Context context)
{
super(context,Database_name , null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE" + "Login_Authentication"+ "(" +
"ID" + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
"UserName" + "TEXT NOT NULL," +
"Password" + "TEXT NOT NULL);"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + "Login_Authentication");
onCreate(db);
}
}
public Database_Wrap(Context c)
{
OurContext = c;
}
public Database_Wrap Open()
{
OurHelper = new Sql_Lite_Work(OurContext);
ourDatabase = OurHelper.getWritableDatabase();
return this;
}
public void close()
{
OurHelper.close();
}
}
Now next thing i want to do is in my mainactivity i want to do something like:
Database_Wrap entry= new Database_Wrap(MainActivity.this);
and then acces the function written in Database_Wrap to open the database and write the values into table for admin and other users:
entry.Open();
but in eclipse am not able to access open function on entry. I dnt know what is wrong here. Am a beginer. May be am forgetting something. Kindly have a look thankyou!
Java is case sensitive. Try:
Database_Wrap entry= new Database_Wrap(MainActivity.this);
Both your ws in Wrap were lower case.
I just put your code into a small test project. It compiles just fine. I then added this code, in an Activity in the same project:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Database_Wrap entry= new Database_Wrap(MainActivity.this);
entry.Open();
}
That too, works just fine.
Judging by the appearance of the code (nothing at all wrong with it, just that the style makes experienced Java devs cringe), you are new to Java; probably eclipse too. Eclipse is weird and does funky stuff all the time. Perhaps you've just encountered some short term anomaly.
I need only select operation. Therefore the data should be inserted before the application starts. What is the best way to do this?
Use an subclass of SQLiteOpenHelper to access your db. The onCreate will be called once, when the database is created.
To use your db you can then do:
DBHelper db = new DBHelper(context);
db.getReadableDatabase().query(....);
db.close();
To extend SQLiteOpenHelper do something like (the sql for the table creation is not tested!):
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final Integer DATABASE_VERSION = 1;
DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/*
* Called ONCE on the very first db access (when the db file is created)
*/
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE mytable (_id PRIMARY KEY AUTOINCREMENT, value TEXT); \n" +
"INSERT INTO mytable (value) VALUES ('data'));");
}
/*
* Called every time you open a database
*/
#Override
public void onOpen(SQLiteDatabase db) {
}
/*
* Called ONCE if DATABASE_VERSION has changed since the last instantiation of DBHelper
*/
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
This is the fist time I've used SQLiteOpenHelper (or databases on android). When I get a writeable database I was wondering why onCreate isnt being called on each new instance of the class. Am I doing something wrong?
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyDatabase.db";
private static final int DATABASE_VERSION = 1;
private String PrSQLcmd = "";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE IF NOT EXISTS Contact(Firstname TEXT, LastName TEXT");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
In SQLiteOpenHelper, the meaning of 'onCreate' is different from what it is in an Activity. Here,'onCreate' is called only once, which is the first time you create the database. The next time you run the app, the database is already there, so it won't call 'onCreate'. Your object level initialization should be done in the constructor and not in 'onCreate'
To see 'onCreate' being called, either manually delete the db file, or simply uninstall the app.
How to declare database in android? How we make a database in android and method?
Basically you extend a class with SQLiteOpenHelper and implement onCreate and onUpgrade.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db_table";
public DatabaseHelper2(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE coordinate (field1 CHAR NOT NULL ,field2 INTEGER NOT NULL ,field3 INTEGER NOT NULL);");
db.execSQL("INSERT INTO spotit VALUES( test ,10,10);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Then initiate the class in your main activity.
DatabaseHelper db = new DatabaseHelper(this);
db.getReadableDatabase();
db.close();
onCreate will be invoked when you call db.getReadableDatabase()
all you need is here:
http://developer.android.com/guide/topics/data/data-storage.html#db