user1682793
user1682793

Reputation: 33

Android APP Database Connect

i want to create a SQLLite Database with "openOrCreateDatabase". The problem is, that i get an error while creating a new table.

11-11 12:36:39.865: E/AndroidRuntime(1520): java.lang.IllegalStateException: 
database not open

This is my code:

public class MainActivity extends Activity {

EditText playerName;
Button submitPlayer;
SQLiteDatabase myDB = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    playerName = (EditText)findViewById(R.id.playerName);
    submitPlayer = (Button)findViewById(R.id.submitPlayer);


        myDB = SQLiteDatabase.openOrCreateDatabase("/data/data/com.my.appname/mydb.db",null);           
  try{          

        myDB.execSQL("CREATE TABLE IF NOT EXISTS player (playername VARCHAR)");         
        myDB.execSQL("INSERT INTO player VALUES('test')");

... ..

I added 2 lines in the manifest:

< permission-group android:name="android.permission-group.STORAGE">

< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Whats wrong ?

Thanks a lot !

Upvotes: 0

Views: 103

Answers (1)

edofic
edofic

Reputation: 727

You shouldn't be accessing your database like this. And also you don't need storage permissions for /data/data, that's for sdcard. What you should be doing is using SQLiteOpenHelper to manage your db. Here's a tutorial to get you started

Upvotes: 2

Related Questions