0

goodmornig, I have a problem with the creation of database on Android studio. the code have no problem but the app say "unfortunately stoppep". how can i solve this problem?

package com.example.maria.database;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {
DatabaseHelper myDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDB = new DatabaseHelper(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

and there is the other activity

package com.example.maria.database;

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

public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2= "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";



public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY        AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, intnewVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);

}
}
xxxm
  • 11
  • 3

2 Answers2

6

You have missed space in query:

db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY        AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");

It should be:

db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY        AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");
Green goblin
  • 9,898
  • 13
  • 71
  • 100
0

Please upload Logcat Output.

I think there must be some error In create table. . In most cases this happens

Also make open and close methods before calling any other methods.

public class DatabaseHelper extends SQLiteOpenHelper{ public static final String DATABASE_NAME = "student.db"; public static final String TABLE_NAME = "student_table"; public static final String COL_1 = "ID"; public static final String COL_2= "NAME"; public static final String COL_3 = "SURNAME"; public static final String COL_4 = "MARKS";

You have used upper case for column name. Make it lower case and try.