Search
 
SCRIPT & CODE EXAMPLE
 

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;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: English print format number in java 
Java :: view binding 
Java :: add new item to array java 
Java :: programme javascrip mineur et majaur 
Java :: Creategoogle maps marker with custom image/bitmap 
Java :: zoomin pdf in android studio 
Java :: Which of the below is a correct identifier for a method name in Java * 2 points 0start #stop 0_start *start# 
Java :: reactive kafka 
Java :: internal hashcode 
Java :: android java navigation bar show icon with title 
Java :: null check in line java 
Java :: what is difference between constant and final in java 
Java :: calculate tip and sales tax function 
Java :: cgange background from button click java fx 
Java :: how to implement seekbar for music in java 
Java :: find if element is powers of 2 
Java :: variable cannot be dereferenced java 
Java :: Kotlin const to Java 
Java :: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type project and qualifiers [@Default] 
Java :: how to enforce xml dtd validation in java 
Java :: getBatteryPercentage android studio 
Java :: ["org.elasticsearch.bootstrap.startupexception: java.lang.illegalstateexception: failed to obtain node locks, 
Java :: repository null when testing service 
Java :: how to do two constructors with super 
Java :: java run multiple cmd commands 
Java :: lmethod java 
Java :: default access modifier java 
Java :: equals() method in java algorithm 
Java :: how to make a typewriter effect in jaca 
Java :: gif to blob java 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =