Created
May 25, 2020 12:39
-
-
Save BetelGeuseee/115d75ba1bb54bbc88ce90f4605f3353 to your computer and use it in GitHub Desktop.
some databse operations on android
This file contains hidden or 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
package com.example.databaseproject; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.SQLException; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class ShirshakDatabaseAdapter { | |
ShirshakHelper helper; | |
public ShirshakDatabaseAdapter(Context context){ | |
helper = new ShirshakHelper(context); | |
} | |
public long insertData(String name,String password){ | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
ContentValues contentValues = new ContentValues(); | |
contentValues.put(ShirshakHelper.NAME,name); | |
contentValues.put(ShirshakHelper.PASSWORD,password); | |
long id=db.insert(ShirshakHelper.TABLE_NAME,null,contentValues); | |
db.close(); | |
return id; | |
} | |
public String getAllData(){ | |
StringBuffer buffer = new StringBuffer(); | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
String[] columns={ShirshakHelper.UID,ShirshakHelper.NAME,ShirshakHelper.PASSWORD}; | |
Cursor cursor=db.query(ShirshakHelper.TABLE_NAME,columns,null,null,null,null,null); | |
while(cursor.moveToNext()) { | |
int index1 = cursor.getColumnIndex(ShirshakHelper.UID); | |
int id = cursor.getInt(index1); | |
int index2 = cursor.getColumnIndex(ShirshakHelper.NAME); | |
String name = cursor.getString(index2); | |
int index3 = cursor.getColumnIndex(ShirshakHelper.PASSWORD); | |
String password = cursor.getString(index3); | |
buffer.append(id+" "+name+" "+password+"\n"); | |
} | |
return buffer.toString(); | |
} | |
public String getData(String name){ | |
StringBuffer buffer = new StringBuffer(); | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
String[] columns={ShirshakHelper.NAME,ShirshakHelper.PASSWORD}; | |
Cursor cursor=db.query(ShirshakHelper.TABLE_NAME,columns,ShirshakHelper.NAME+" = '"+name+"' ",null,null,null,null); | |
while(cursor.moveToNext()) { | |
int index4 = cursor.getColumnIndex(ShirshakHelper.NAME); | |
String personName = cursor.getString(index4); | |
int index5 = cursor.getColumnIndex(ShirshakHelper.PASSWORD); | |
String password = cursor.getString(index5); | |
buffer.append(name+" "+password+"\n"); | |
} | |
return buffer.toString(); | |
} | |
public int updateName(String oldName,String newName){ | |
ContentValues content = new ContentValues(); | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
content.put(ShirshakHelper.NAME,newName); | |
String[] whereArgs={oldName}; | |
int count= db.update(ShirshakHelper.TABLE_NAME,content,ShirshakHelper.NAME+"=?",whereArgs); | |
return count; | |
} | |
public int deleteName(){ | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
String[] whereArgs ={"yug"}; | |
int count=db.delete(ShirshakHelper.TABLE_NAME,ShirshakHelper.NAME+"=?",whereArgs); | |
return count; | |
} | |
static class ShirshakHelper extends SQLiteOpenHelper{ | |
private static final String DATABASE_NAME = "shirshakdatabase"; | |
private static final int DATABASE_VERSION=3; //you should keep updating your version name while making changes on table(ANY CHANGESS) | |
private static final String TABLE_NAME="SHIRSHAKTABLE"; | |
private static final String UID="_id"; | |
private Context context; | |
private static final String NAME="Name"; | |
private static final String PASSWORD="Password"; | |
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME; | |
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255) ,"+PASSWORD+" VARCHAR(255));"; | |
ShirshakHelper(Context context){ | |
super(context,DATABASE_NAME,null,DATABASE_VERSION); | |
this.context=context; | |
Message.message(context,"constructor was called"); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
try { | |
db.execSQL(CREATE_TABLE); | |
Message.message(context,"onCreate was called"); | |
} | |
catch(SQLException e){ | |
Message.message(context,""+e); | |
} | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
try{ | |
Message.message(context,"onUpgrade was called"); | |
db.execSQL(DROP_TABLE); | |
onCreate(db); | |
}catch (SQLException e){ | |
Message.message(context,""+e); | |
} | |
} | |
} | |
} | |
////////////////////////////////now mainActivity | |
package com.example.databaseproject; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
public class MainActivity extends AppCompatActivity { | |
EditText username,password,detail; | |
ShirshakDatabaseAdapter databaseAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
username=(EditText)findViewById(R.id.usernameText); | |
password=(EditText)findViewById(R.id.passwordText); | |
detail=(EditText)findViewById(R.id.editText); | |
databaseAdapter = new ShirshakDatabaseAdapter(this); | |
} | |
public void addUser(View v){ | |
String user = username.getText().toString(); | |
String pass = password.getText().toString(); | |
long id=databaseAdapter.insertData(user,pass); | |
if(id<0){ | |
Message.message(this,"Unsuccesfull"); | |
} | |
else{ | |
Message.message(this,"Successfully inserted a row"); | |
} | |
} | |
public void viewDetails(View v){ | |
String data=databaseAdapter.getAllData(); | |
Message.message(this,data); | |
} | |
public void viewUserDetails(View v){ | |
String text=detail.getText().toString(); | |
String data=databaseAdapter.getData(text); | |
Message.message(this,data); | |
} | |
public void update(View v){ | |
databaseAdapter.updateName("shirshak","BetelGeuseee"); | |
} | |
public void delete(View v){ | |
int count=databaseAdapter.deleteName(); | |
Message.message(this,""+count); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment