dimanche 22 mars 2015

SQLite DB crashes every time

I am very new to Android developing and trying my luck with SQLite but i am stuck right now. Every time i start my application the app crashes. I simply want to add a value to the DB when i click on a button, but it does not work.


Could you help me?


This is my MainActivity imports [..]



public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnStarzeit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSpeichern(1);
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
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);
}

private void startSpeichern(int value){
DatabaseHandler dbHandler = new DatabaseHandler(this,null,null,1);
dbHandler.neueStartzeit(value);
}
}


and this is my DatabaseHandler



imports[..]
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 9;
private static final String DATABASE_NAME = "Name der Datenbank";
public static final String TABLE_PRODUCTS = "produkte";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public static final String COLUMN_QUANTITY="menge";

public DatabaseHandler (Context context, String name, SQLiteDatabase.CursorFactory factory,int version){
super(context,DATABASE_NAME,factory,DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db){

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_PRODUCTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_PRODUCTNAME
+ " INTEGER," + COLUMN_QUANTITY + " INTEGER" + ")";
db.execSQL(CREATE_PRODUCTS_TABLE);
}

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

public void neueStartzeit(){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME,1);
values.put(COLUMN_QUANTITY,1);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_PRODUCTS,null, values);
db.close();
}
}

Aucun commentaire:

Enregistrer un commentaire