Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:17:36+00:00 2026-05-26T03:17:36+00:00

There is an application that need to create several tables in DB. I wrote

  • 0

There is an application that need to create several tables in DB. I wrote a simple class work work with database

package db.example.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;


public class DB {

    private final Map<String, String> FIELDS;

    private DatabaseHelper dbHelper;
    private SQLiteDatabase Db;

    private final String DB_NAME = "tasstelecom";
    private final String TABLE_NAME;

    private final int DATABASE_VERSION;
    private final String CREATE_TABLE;

    private final Context context;

    private class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
                super(context, DB_NAME, null, DATABASE_VERSION);
            }

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

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

            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
             onCreate(db);
        }
    }

    public DB(Context ctx, String tableName, Map<String, String> fields, int version) {
        this.context = ctx;

        this.TABLE_NAME = tableName;
        this.FIELDS = fields;
        this.DATABASE_VERSION = version;

        String createFields = "";

        for(Map.Entry<String, String> m: FIELDS.entrySet()) {
            createFields += m.getKey().toString()+" "+m.getValue().toString()+", ";
        }

        createFields = createFields.replaceAll(",\\s$", "");

        this.CREATE_TABLE = "create table "+tableName+" (" + createFields+")";
    }

    public DB open() throws SQLException {
        dbHelper = new DatabaseHelper(context);
        Db = dbHelper.getWritableDatabase();

        return this;
    }

    public void close() {
        dbHelper.close();
    }

    public long insert(Map<String,String> vals) {
        ContentValues initialValues = new ContentValues();

        for(Map.Entry<String, String> m : vals.entrySet()) {
            initialValues.put(m.getKey(), m.getValue());
        }

        return Db.insert(TABLE_NAME, null, initialValues);
    }

    public Cursor executeQuery(String[] fieldsArray) {
        return Db.query(TABLE_NAME, fieldsArray, null, null, null, null, null);
    }

    public void backupDatabase() throws IOException {
        String inFileName = "/data/data/ru.ashot.tasstelecom/databases/"+DB_NAME;
        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);

        String outFileName = Environment.getExternalStorageDirectory()+"/"+DB_NAME+".sqlite";

        OutputStream output = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer))>0){
            output.write(buffer, 0, length);
        }

        output.flush();
        output.close();
        fis.close();
    } }

calling a constructor creates database, if it not exist, and create a table

table1 = new DB(this, "table1", fields1, 1);
table2 = new DB(this, "table2", fields1, 2);

it works right, but every time i call constructor, i need to specify new number of DB version, and it`s a little bit uncomfortable.

Is there a method to create several tables with method i described under same version of DB?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:17:36+00:00Added an answer on May 26, 2026 at 3:17 am

    You don’t need to call constructor multiple times, one time is enough. Replace:

    private final String CREATE_TABLE;
    

    With:

    protected ArrayList<String> mSqlQueries = new ArrayList<String>();
    

    And change this:

    this.CREATE_TABLE = "create table "+tableName+" (" + createFields+")";
    

    To:

    String sql = "create table "+tableName+" (" + createFields+")";
    mSqlQueries.add(sql);
    

    And change onCreate as follows:

    @Override
    public void onCreate(SQLiteDatabase db) {
        for(String sql : mSqlQueries) {
                db.execSQL(sql);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any application that will read a MySQL database table and generate a
I have a web-app that is a sort of calendar application and there are
I'm building an application that lets my users create several markers on a map.
I need to code a small/simple database application using C, for my CS degree
I have a small application that doesn't use Spring container. Now there's a need
Is there a good application (that has some kind of gui) for testing memory
Is there an application out there that Generates an HTML form from a PDF
Is there a way to Minimize an external application that I don't have control
is there a way to capture application that was previously active, before my application
In the application that I am working on, there is tabular data (for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.