<?xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="426dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Welcome to main activity " android:textColor="#ffffff" android:textSize="15sp" /> <Button android:id="@+id/btnDatabaseSqlite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_intent_explicit" android:layout_below="@+id/btn_intent_explicit" android:text="Database SqLite" android:textSize="8sp" /> </RelativeLayout> </LinearLayout> </LinearLayout>
package com.uk.modak; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ProjectonOverallClassMainActivity extends Activity { public Button btnDatabaseSqlite; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnDatabaseSqlite = (Button) findViewById(R.id.btnDatabaseSqlite); btnDatabaseSqlite.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent b = new Intent(ProjectonOverallClassMainActivity.this,SqliteDatabaseHomeActivity.class); startActivity(b); } }); } public void onStart(){ super.onStart(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="19dp" android:text="Name :" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="32dp" android:text="Surname :" /> <EditText android:id="@+id/editText_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_alignParentRight="true" android:ems="10" android:inputType="textPersonName" /> <EditText android:id="@+id/editText_surname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_alignParentRight="true" android:ems="10" android:inputType="textPersonName" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="35dp" android:text="Marks :" /> <EditText android:id="@+id/editText_Marks" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView3" android:layout_alignParentRight="true" android:ems="10" android:inputType="textPersonName" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView3" android:layout_marginTop="32dp" android:text="Id :" /> <EditText android:id="@+id/editText_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView4" android:layout_alignParentRight="true" android:ems="10" android:inputType="textPersonName" /> <Button android:id="@+id/button_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView4" android:layout_marginTop="67dp" android:layout_toRightOf="@+id/textView4" android:text="Add " /> <Button android:id="@+id/button_viewAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button_add" android:layout_alignBottom="@+id/button_add" android:layout_toRightOf="@+id/button_add" android:text="View" /> <Button android:id="@+id/button_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button_viewAll" android:layout_alignBottom="@+id/button_viewAll" android:layout_toRightOf="@+id/button_viewAll" android:text="Update" /> <Button android:id="@+id/button_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button_update" android:layout_alignBottom="@+id/button_update" android:layout_toRightOf="@+id/button_update" android:text="Delete" /> </RelativeLayout> </LinearLayout>
package com.uk.modak; import android.app.AlertDialog; import android.database.Cursor; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.app.Activity; public class SqliteDatabaseHomeActivity extends Activity { DatabaseHelper myDb; EditText editName,editSurname,editMarks ,editTextId; Button btnAddData; Button btnviewAll; Button btnDelete; Button btnviewUpdate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addeditupdatelayout); myDb = new DatabaseHelper(this); editName = (EditText)findViewById(R.id.editText_name); editSurname = (EditText)findViewById(R.id.editText_surname); editMarks = (EditText)findViewById(R.id.editText_Marks); editTextId = (EditText)findViewById(R.id.editText_id); btnAddData = (Button)findViewById(R.id.button_add); btnviewAll = (Button)findViewById(R.id.button_viewAll); btnviewUpdate= (Button)findViewById(R.id.button_update); btnDelete= (Button)findViewById(R.id.button_delete); AddData(); viewAll(); UpdateData(); DeleteData(); } public void DeleteData() { btnDelete.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Integer deletedRows = myDb.deleteData(editTextId.getText().toString()); if(deletedRows > 0) Toast.makeText(SqliteDatabaseHomeActivity.this,"Data Deleted",Toast.LENGTH_LONG).show(); else Toast.makeText(SqliteDatabaseHomeActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show(); } } ); } public void UpdateData() { btnviewUpdate.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { boolean isUpdate = myDb.updateData(editTextId.getText().toString(), editName.getText().toString(), editSurname.getText().toString(),editMarks.getText().toString()); if(isUpdate == true) Toast.makeText(SqliteDatabaseHomeActivity.this,"Data Update",Toast.LENGTH_LONG).show(); else Toast.makeText(SqliteDatabaseHomeActivity.this,"Data not Updated",Toast.LENGTH_LONG).show(); } } ); } public void AddData() { btnAddData.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { boolean isInserted = myDb.insertData(editName.getText().toString(), editSurname.getText().toString(), editMarks.getText().toString() ); if(isInserted == true) Toast.makeText(SqliteDatabaseHomeActivity.this,"Data Inserted",Toast.LENGTH_LONG).show(); else Toast.makeText(SqliteDatabaseHomeActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show(); } } ); } public void viewAll() { btnviewAll.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Cursor res = myDb.getAllData(); if(res.getCount() == 0) { // show message showMessage("Error","Nothing found"); return; } StringBuffer buffer = new StringBuffer(); while (res.moveToNext()) { buffer.append("Id :"+ res.getString(0)+"\n"); buffer.append("Name :"+ res.getString(1)+"\n"); buffer.append("Surname :"+ res.getString(2)+"\n"); buffer.append("Marks :"+ res.getString(3)+"\n\n"); } // Show all data showMessage("Data",buffer.toString()); } } ); } public void showMessage(String title,String Message){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(true); builder.setTitle(title); builder.setMessage(Message); builder.show(); } }
package com.uk.modak; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "Student.db"; public static final String TABLE_NAME = "student_table"; public static final String COL_1 = "ID"; public static final String COL_2 = "NAME"; public static final String COL_3 = "SURNAME"; public static final String COL_4 = "MARKS"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db); } public boolean insertData(String name,String surname,String marks) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_2,name); contentValues.put(COL_3,surname); contentValues.put(COL_4,marks); long result = db.insert(TABLE_NAME,null ,contentValues); if(result == -1) return false; else return true; } public Cursor getAllData() { SQLiteDatabase db = this.getWritableDatabase(); Cursor res = db.rawQuery("select * from "+TABLE_NAME,null); return res; } public boolean updateData(String id,String name,String surname,String marks) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_1,id); contentValues.put(COL_2,name); contentValues.put(COL_3,surname); contentValues.put(COL_4,marks); db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id }); return true; } public Integer deleteData (String id) { SQLiteDatabase db = this.getWritableDatabase(); return db.delete(TABLE_NAME, "ID = ?",new String[] {id}); } }
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ProjectonOverallClassMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SqliteDatabaseHomeActivity" android:label="@string/app_name" > </activity> <receiver android:name=".ConnectionReceive"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> </application>
Total : 26654
Today :3
Today Visit Country :