0

I'm trying to make an app that can store data but the schema of the data is given by the user. So somehow I need to pass the table name and columns' name to DatabaseHelper.java so use them in onCreate method.

I found out you can pass strings using Intent but here it didn't work because it turned out it works only between activites.

So I can get the information from the user and but it in a string separating names with commas. But I can't send the string to DatabaseHelper.java to execute in onCreate.

How I get Info

Is there a way to do this? Or do I have to use another approach? Or is this something impossible?

Thanks in advance, all Best

My method to get the information and process it:

/* Called when the user clicks the Finalize button */
    public void createSchema(View view) {
        String create_table = ""; // Store table name and columns name
        schemaName = (EditText)findViewById(R.id.create_schema_name);
        String schemaNameString = schemaName.getText().toString().trim();
        fieldsContainer = (LinearLayout)findViewById(R.id.fieldsContainer);
        int childCount = fieldsContainer.getChildCount();
        if (schemaNameString.length() != 0) {
            // When the name is entered
            if (childCount > 0) {
                // When there is at least one field entered
                create_table += this.slug(schemaNameString) + ",_id,timestamp,latitude,longitude,";
                for (int i = 0; i < childCount; i++) {
                    View childView = fieldsContainer.getChildAt(i);
                    TextView childTextView = (TextView)(childView.findViewById(R.id.added_schema_field));
                    String childTextViewString = childTextView.getText().toString();
                    create_table += this.slug(childTextViewString) + ",";
                }
                create_table = create_table.substring(0, create_table.length() - 1);
                Toast.makeText(getApplicationContext(), create_table, Toast.LENGTH_SHORT).show();
                // Doesn't work
                // Intent intent = new Intent(this, DatabaseHelper.java);
                // intent.putExtra(CREATE_TABLE, create_table);
                // startActivity(intent);
            } else {
                // No field entered
                Toast.makeText(getApplicationContext(), "There must be at least one field entered to " + schemaNameString + ".", Toast.LENGTH_SHORT).show();
            }           
        } else {
            // No name entered
            Toast.makeText(getApplicationContext(), "No name entered for the schema", Toast.LENGTH_SHORT).show();
        }
    }

My DatabaseHelper.java:

package com.example.draft;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "myDB";

    // Doesn't work
    // Intent intent = getIntent();
    // String create_table = intent.getStringExtra(CreateSchemaActivity.CREATE_TABLE);

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

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("");
        onCreate(db);
    }

}

1 Answer 1

2

You cannot use SQLiteOpenHelper. You will need to manage your database, and its upgrades, yourself, working directly with SQLiteDatabase.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I did that and it's working now. I'm not sure if this is safe though. :/ SQL commands are executed directly in a method in the corresponding activity using try, catch and finally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.