SQLite database in android

Hello everybody,

I am noob with android and need help ...

I am developing an application that requires me to write to SQLiteDatabase in one action and access it from another action. This is a problem. Any suggestions / ideas on how we can share a database in multiple actions ...?

+3
source share
3 answers

I would recommend you use the SQLiteOpenHelper class .

Just use the same database name sequentially in your actions, this should not cause any problems.

SQLiteOpenHelper helper = new SQLiteOpenHelper(
    context, R.string.db_name, null, 1);
SQLiteDatabase db = helper.getWritableDatabase();
+3
source

The problem of accessing the same database can be handled in several different ways.

, , , SQLITEOpenHelper .

Android , SQlite.

0

You can simply create a common class for the database and use it by creating an object.

public class DbOperation extends SQLiteOpenHelper{
public static final String name="mydb.db";
public static final String MainTab="MainTab";
public static final String ID="_ID";
public static final String LevelName="LevelName";
int version =2;

public DbOperation(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name,null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
String str="CREATE TABLE "+MainTab+"("+ID+" integer primary key autoincrement,"+LevelName+" text not null unique key)";     

}

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

}

}

Use this database in any action below

DbOperation ob=new DbOperation ();
SQLiteDatabase db=new SQLiteaDatabase();
db=ob.getWritableDataBase();

and now you can use operation like query,delete,update

 Cursor cur=db.query(Table_name,null,null,null,null); etc
0
source

Source: https://habr.com/ru/post/1755559/


All Articles