-4

I tried to create a database (SQLite, SQLiteOpenHelper) on an Android project, but when I run my app at that time application says:

"Sorry the application My application has stopped"

Why does this happen?

My class of the logical database:

public class DataBaseManager {

    public static final String TABLE_NAME = "Clientes";

    public static final String CN_ID = "_nuip";
    public static final String CN_NAME = "Name";
    public static final String CN_DATE = "Date";
    public static final String CN_CASH = "Cash";

    public static final String CREATE_TABLE = " create tabla" + TABLE_NAME + " ("
            + CN_ID + " integer primary key autoincrement , "
            + CN_NAME + " text not null,"
            + CN_DATE + " text not null,"
            + CN_CASH + " text);";
}

My class extends SQLiteOpenHelper:

public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String DB_NAME  = "clientes.sqlite" ;
    private static final int DB_SCHEME_VERSION = 1;

    public AdminSQLiteOpenHelper(Context context) {
         super(context,DB_NAME, null, DB_SCHEME_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DataBaseManager.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

My Main Activity:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdminSQLiteOpenHelper adm = new AdminSQLiteOpenHelper(this);
        SQLiteDatabase db2 = adm.getWritableDatabase();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

0

First you delete the memory cache and data of your applications, in settings>Manager Applications>My App.
then, add this line in your query "IF NOT EXISTS" , so your query are;
public static final String CREATE_TABLE = " create tabla" + TABLE_NAME + " IF NOT EXISTS (" + CN_ID + " integer primary key autoincrement , " + CN_NAME + " text not null," + CN_DATE + " text not null," + CN_CASH + " text);";

Osvaldo Bringaz
  • 127
  • 1
  • 7
0

Correct you code with this code.

public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS" 
    + TABLE_NAME + " ("
    + CN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
    + CN_NAME + " TEXT NOT NULL, "
    + CN_DATE + " TEXT NOT NULL, "
    + CN_CASH + " TEXT);";
}
Jaydip
  • 592
  • 5
  • 27