0

I want to Connect my app with a database on the Android Phone, so I've written a class called DBHelper.

But I get An Error, which says "MODE_PRIVATE" cannot be resolved to a variable. So i made "Context.MODE_PRIVATE". Now, the Variable can be resolved, but I get a new Error:

"The method openOrCreateDatabase(String, int, null) is undefinded for the Type DBHelper".

It doesn't help if I use DBHelper.this.openOrCreateDatabase to open it.

Can anybody help me?

This is my Code:

public class DBHelper {

    SQLiteDatabase db;

    public void insert(String news, Context con){

        db = openOrCreateDatabase("PlanB", con.MODE_PRIVATE, null);
        db.execSQL("DROP TABLE IF EXISTS News");
        db.execSQL("CREATE TABLE IF NOT EXISTS INBOX(id INTEGER,title VARCHAR,text VARCHAR,date VARCHAR);");

        String[] divided = news.split("/newentry/");
        int length = divided.length;
        int pos = 0;

        while(pos <= length){
            String[] entry = divided[pos].split("/;/");
            db.execSQL("INSERT INTO  INBOX VALUES('"+entry[0]+"','"+entry[1]+"','"+entry[2]+"','"+entry[3]+"');");
            pos++;  
        }

    db.close();
    }
}
2
  • 1
    Did you try using context. i.e con.openOrCreateDatabase("PlanB", con.MODE_PRIVATE, null); ? Commented Aug 20, 2013 at 19:52
  • If you read the documentation, developer.android.com/reference/android/content/…, you would see that MODE_PRIVATE is static. The usage is therefore Context.MODE_PRIVATE Commented Aug 20, 2013 at 19:59

2 Answers 2

1

Take a look at SQLiteOpenHelper as you should be extending it somewhere if you are using SQLite in Android.

Documentation:

A helper class to manage database creation and version management.

You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

This is a database helper that you may be able to find some use out of. 

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

Comments

0

Its better you extend SQLiteOpenHelper in your class and override its methods

1.onCreate--which creates a table in a specified database 2.onUpdate-- usually used to drop tables anad make an updations to it.

Also make sure you make a contructor of that class which includes the following statement super(context,DB_NAME, null,DB_VERSION);

Whenever you will declare the object of this class in your another class (any class in your project) this constructor will automatically create database for you. Next onCreate method is called automatically which creates the table if it does'nt exists. Now you can add custom methods in your class which is extending SQLiteOpenHelper to add or delete data into the table. These methods can now be accesed via object declare in your other classes.

Here's a tutorial on SQLiteOpenHelper.

http://www.vogella.com/articles/AndroidSQLite/article.html

I hope this gives you a small hint or guidance.

Comments

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.