Created
February 7, 2018 19:58
-
-
Save serdoune/8cf1c2415d90ba970e056e549b51516d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DB_Controller extends SQLiteOpenHelper { | |
public DB_Controller(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { | |
super(context, "TEST.db", factory, version); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase sqLiteDatabase) { | |
sqLiteDatabase.execSQL("CREATE TABLE STUDENTS(ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT UNIQUE, LASTNAME TEXT);"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { | |
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS STUDENTS;"); | |
onCreate(sqLiteDatabase); | |
} | |
public void insert_student(String firstname, String lastname){ | |
ContentValues contentValues=new ContentValues(); | |
contentValues.put("FIRSTNAME",firstname); | |
contentValues.put("LASTNAME",lastname); | |
this.getWritableDatabase().insertOrThrow("STUDENTS","",contentValues); | |
} | |
public void delete_student(String firstname){ | |
this.getWritableDatabase().delete("STUDENTS","FIRSTNAME='"+firstname+"'",null); | |
} | |
public void update_student(String old_firstname, String new_firstname){ | |
this.getWritableDatabase().execSQL("UPDATE STUDENTS SET FIRSTNAME='"+new_firstname+"'WHERE FIRSTNAME='"+old_firstname+"'"); | |
} | |
public void list_all_student(TextView textView){ | |
Cursor cursor=this.getReadableDatabase().rawQuery("SELECT * FROM STUDENTS",null); | |
textView.setText(""); | |
while (cursor.moveToNext()){ | |
textView.append(cursor.getString(1) +" "+cursor.getString(2)+"\n"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment