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 6969521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:35:30+00:00 2026-05-27T16:35:30+00:00

I got this class, which creates database at the 1st run of the app.

  • 0

I got this class, which creates database at the 1st run of the app. I wanted to fill one table with some example values from strings.xml. Basically I’d do this like:

private static final String CATEGORIES_FILL = "INSERT INTO categories VALUES ('', " + getString(R.string.fuel) + "); " +
                                              "INSERT INTO categories VALUES ('', " + getString(R.string.food) + "); " +
                                              "INSERT INTO categories VALUES ('', " + getString(R.string.treatment) + "); " +
                                              "INSERT INTO categories VALUES ('', " + getString(R.string.salary) + "); ";

The problem is, the class is not an activity, and string must be static so I can use

db.execSQL(CATEGORIES_FILL);

Please correct my approach so I could use strings.xml to do that.

I assume I could do that in my activity class surrounding it with try block, but I don’t like the idea of executing this every time I open the activity.

Fragment of database class

private static class DatabaseHelper extends SQLiteOpenHelper {


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


        final String CATEGORIES_FILL = "INSERT INTO categories VALUES ('', " + context.getString(R.string.fuel) + "); " +
        "INSERT INTO categories VALUES ('', " + context.getString(R.string.food) + "); " +
        "INSERT INTO categories VALUES ('', " + context.getString(R.string.treatment) + "); " +
        "INSERT INTO categories VALUES ('', " + context.getString(R.string.salary) + "); "; 
    }

    /* Tworzenie tabel
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        db.execSQL(CATEGORIES_FILL); //need the way to access this final here
        db.execSQL(EXPENSES_CREATE);
        db.execSQL(INCOMES_CREATE);
        db.execSQL(BUGS_CREATE);            
    }
  • 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-27T16:35:31+00:00Added an answer on May 27, 2026 at 4:35 pm

    Something vaguely like this.

    Add to res/values/<yourchoiceoffilename>.xml & add your string references to a string-array.

    <resources>
        <string-array name="categories">
            <item>@string/fuel</item>
            <item>@string/food</item>
            <item>@string/treatment</item>
            <item>@string/salary</item>
        </string-array>
    </resources>
    

    Create a SQLiteStatement on your database.

    SQLiteDatabase db = ...;
    SQLiteStatement insert = db.compileStatement("INSERT INTO categories (yourcolumnnamehere) VALUES (?)");
    
    //process each string
    for (String category : getResources().getStringArray(R.array.categories)) 
    {
        insert.bindValue(1, category);
        long id = insert.executeInsert(); // In case you care about the row id.
    }
    

    Edit: And, consider doing something like following to your class (I’ve probably left a bug or two in there, but you should get the gist of it):

    private static class DatabaseHelper extends SQLiteOpenHelper {
        ...
        // Replace 'yourcolumnnamehere' with whatever your column is actually named.
        private static final String INSERT_CATEGORY = "INSERT INTO categories (yourcolumnamehere) VALUES (?)"
        ...
        ...
    
        private final String[] mCategories;
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            mCategories = context.getResources().getStringArray(R.array.categories);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
    
            // Here you create the SQLiteStatement that will be used
            // to add categories. The values are stored in mCategories.
            SQLiteStatement statement = db.compileStatement(INSERT_CATEGORY);
            for (String category : mCategories) {
                statement.bindValue(1, category);
                statement.executeInsert();
            }
            statement.close();
    
            db.execSQL(EXPENSES_CREATE);
            db.execSQL(INCOMES_CREATE);
            db.execSQL(BUGS_CREATE);            
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an iPhone app using objective C. I've got class A, which
I've got a Validator class which creates an instance of a Validations class, which
I've got this class: class Foo { public string Name { get; set; }
I got a scenario like this Class Parent { Property A; } Class A
Say I've got a class like this: class Test { int x; SomeClass s;
I've got a class representing an interval. This class has two properties start and
I've got a set of models that look like this: class Page(models.Model): title =
I've got a class somewhat like this: public class Test { private final List<ISomeType>
I've got a simple java class that looks something like this: public class Skin
I've got a class that looks something like this: public class Parent { public

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.