Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

access db in fragments

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "hunderassen.db";
public static final String TABLE_NAME = "hunderassen_table";
public static final String COLUMN1 = "ID";
public static final String COLUMN2 = "name_Hunderasse";
public static final String COLUMN3 = "durchschnittlicheDauer";
public static final String COLUMN4 = "groesseHund";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1); //creating db and table
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, name_Hunderasse TEXT, durchschnittlicheDauer TEXT, groesseHund TEXT) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    this.onCreate(db);
}

public boolean insertData(String name_hr, String durchschnitt, String groesse) {

    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN2, name_hr);
    contentValues.put(COLUMN3, durchschnitt);
    contentValues.put(COLUMN4, groesse);
    long result = database.insert(TABLE_NAME, null, contentValues);

    //check if value is -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #access #db #fragments
ADD COMMENT
Topic
Name
9+4 =