Reputation: 27
I want to store data using threads in SQLite Database. Each thread has its own database to store data. I'm using threads to increase performance and decrease processing time. as you know SQLite doesn't allow multi threaded data writing. So I planned to make a database for each thread to save time and increase performance.
Thread1 will insert data in Database[0] Thread2 will insert data in Database[1] . . . so on.
How can I achieve this using SQLite Helper?
I'm using this code
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 3);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertdata(int Dbnumber)
{
Database[dbnumber].excquery("Insert into ....");
}
Upvotes: 1
Views: 2241
Reputation: 8662
If you change the constant to a regular class member, like this:
public class DBHelper extends SQLiteOpenHelper {
public String databaseName;
public DBHelper(Context context, String databaseName) {
super(context, databaseName, null, 3);
this.databaseName = databaseName;
}
// ...
}
Then you can instantiate as many of these as you want:
DBHelper db1 = new DBHelper(context, "db1.db");
DBHelper db2 = new DBHelper(context, "db2.db");
You might have to change DATABASE_NAME to databaseName in other places in your code, but you get the idea.
Upvotes: 3
Reputation: 3710
Create the another class which extends to SQLiteOpenHelper. As simple as that.
Why you need the different thread. When you will create the object of that class it will create all tables for you.
If you want to create the separate thread for each DB use Thread. @Wizad suggests what is the need of another DB when you can store multiple tables in DB. BTW it's your choice go ahead.
Upvotes: 0