-4

I am trying to implement a database using SQLite within my Android application.
The user should enter his/her first and second name.
When I run the application, it does not work and this message appears: "Unfortunately, name.db has stopped."

My code is as follows

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

public class SDatabaseHelper extends DatabaseHelper {


private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="perInfo.db";
private static final String TABLE_NAME="Personal Informaition";
private static final String COLUMN_FNAME="First Name";
private static final String COLUMN_SNAME="Second Name";

SQLiteDatabase db;

public SDatabaseHelper( Context context)
{

    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_NAME  + "("
            + COLUMN_FNAME + " TEXT, "
            + COLUMN_SNAME + " TEXT )";

    db.execSQL(query);

}
public boolean insertdata(String fn, String sn ){
    db=this.getWritableDatabase();
    ContentValues values =new ContentValues();
    values.put(COLUMN_FNAME,fn);
    values.put(COLUMN_SNAME, sn);

    long result = db.insert(TABLE_NAME, null, values);
    if(result == -1)
        return false;
    else
        return true;
}

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

  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;


 public class persInfo extends ActionBarActivity {

EditText ftext, stext;
SDatabaseHelper dbHandler;
Button addbutt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pers_info);
    dbHandler=new SDatabaseHelper(this);

    ftext=(EditText) findViewById(R.id.name1);
    stext=(EditText) findViewById(R.id.name2);
    addbutt=(Button) findViewById(R.id.add);

    AddData();
}

public void AddData()
{
  addbutt.setOnClickListener(
          new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  boolean inserted = dbHandler.insertdata(ftext.getText().toString(),
                          stext.getText().toString());
                  if(inserted = true)
                      Toast.makeText(persInfo.this,"Data inserted",Toast.LENGTH_LONG).show();
                  else
                      Toast.makeText(persInfo.this,"Data dosnt inserted",Toast.LENGTH_LONG).show();

              }
     } ); } }

  public class personalInfo {

  String fname,sname;

   public void setFname(String fname){
        this.fname=fname;
   }
public String getFname(){
    return this.fname;
}
public void setSname(String Sname){
    this.sname=sname;
}
public String getSname(){
    return this.sname;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ghadeer
  • 63
  • 1
  • 1
  • 3

1 Answers1

1

The problem here is that you are using spaces without escaping them

private static final String TABLE_NAME="Personal Informaition";
private static final String COLUMN_FNAME="First Name";
private static final String COLUMN_SNAME="Second Name";

which will break the SQL table creation command syntax.
If you really want to use spaces, you have to escape them, like so:

private static final String TABLE_NAME="[Personal Informaition]";
private static final String COLUMN_FNAME="[First Name]";
private static final String COLUMN_SNAME="[Second Name]";

But I would suggest using underscores, like so:

private static final String TABLE_NAME="Personal_Informaition";
private static final String COLUMN_FNAME="First_Name";
private static final String COLUMN_SNAME="Second_Name";
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115