SQLLite data base Insert Update Delete View
------
Sqlite database insert update delete –
1) àactivity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.vk.sqllite_database_24_5.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/etName"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone no"
android:id="@+id/etPhno"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:id="@+id/etId"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Insert"
android:id="@+id/btIns"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Update"
android:id="@+id/btUpdate"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delete"
android:id="@+id/btDelete"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tvId"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tvNmae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tvPh"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.vk.sqllite_database_24_5.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/etName"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone no"
android:id="@+id/etPhno"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:id="@+id/etId"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Insert"
android:id="@+id/btIns"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Update"
android:id="@+id/btUpdate"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delete"
android:id="@+id/btDelete"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tvId"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tvNmae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tvPh"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
2)à DatabaseAdepter.java
package com.example.vk.sqllite_database_24_5; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by vk on 5/25/2017. */ public class DatabaseAdapter extends SQLiteOpenHelper { // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "contactsManager"; // Contacts table name private static final String TABLE_CONTACTS = "contacts"; // Contacts Table Columns names public static final String KEY_ID = "id"; public static final String KEY_NAME = "name"; public static final String KEY_PH_NO = "phone_number"; public DatabaseAdapter(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")"; db.execSQL(CREATE_CONTACTS_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); // Create tables again onCreate(db); } /*public long insertContacts(String name,String phno){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, name); // Contact Name values.put(KEY_PH_NO, phno); // Contact Phone Number // Inserting Row long isInsert=db.insert(TABLE_CONTACTS, null, values); Log.e("Insert","-----"+isInsert); db.close(); // Closing database connection return isInsert; }*/ public long insertContacts(contactDetails contactDetails){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, contactDetails.getName()); // Contact Name values.put(KEY_PH_NO, contactDetails.getPhno()); // Contact Phone Number // Inserting Row long isInsert=db.insert(TABLE_CONTACTS, null, values); Log.e("Insert","-----"+isInsert); db.close(); // Closing database connection return isInsert; } // Getting contacts Count public Cursor getContactsCount() { String countQuery = "SELECT * FROM " + TABLE_CONTACTS+" where "+KEY_ID; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); //cursor.close(); // return count return cursor; } // Updating single contact public int updateContact(String name,String phone,String id) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, name); values.put(KEY_PH_NO, phone); // updating row return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] { String.valueOf(id) }); } // Deleting single contact public void deleteContact(String id) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { String.valueOf(id) }); db.close(); } }
3)à ContactDetail.java
package com.example.vk.sqllite_database_24_5; /** * Created by vk on 5/25/2017. */ public class contactDetails { private String name, phno; private int id; /*getter setter no used karvo jena thi niche no code avi jase*/ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhno() { return phno; } public void setPhno(String phno) { this.phno = phno; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
4)àMainActivity.java
package com.example.vk.sqllite_database_24_5; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private DatabaseAdapter db; private EditText etName,etPhno,etId; private TextView tvId,tvNmae,tvPh; private Button btIns,btUpdate,btDelete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ db=new DatabaseAdapter(MainActivity.this); etName = (EditText) findViewById(R.id.etName); etPhno = (EditText) findViewById(R.id.etPhno); etId = (EditText) findViewById(R.id.etId); tvId = (TextView) findViewById(R.id.tvId); tvNmae = (TextView) findViewById(R.id.tvNmae); tvPh = (TextView) findViewById(R.id.tvPh); btIns = (Button) findViewById(R.id.btIns); btUpdate = (Button) findViewById(R.id.btUpdate); btDelete = (Button) findViewById(R.id.btDelete); btIns.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=etName.getText().toString(); String phno=etPhno.getText().toString(); if (name.length()>0 && phno.length()>0){ contactDetails contactDetails = new contactDetails(); contactDetails.setName(name); contactDetails.setPhno(phno); // long insert=db.insertContacts(name,phno); long insert=db.insertContacts(contactDetails); displayDetails(); } } }); btUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=etName.getText().toString(); String phno=etPhno.getText().toString(); String id=etId.getText().toString(); if (name.length()>0 && phno.length()>0 && id.length()>0) { db.updateContact(name,phno,id); displayDetails(); } } }); btDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id=etId.getText().toString(); if (id.length()>0) { db.deleteContact(id); displayDetails(); } } }); } private void displayDetails(){ Cursor c=db.getContactsCount(); if (c!=null && c.getCount()>0){ String ids=""; String names=""; String phnos=""; //c.getColumnIndex(DatabaseAdapter.KEY_ID) //c.getColumnIndex(DatabaseAdapter.KEY_NAME) //c.getColumnIndex(DatabaseAdapter.KEY_PH_NO) c.moveToFirst(); do { ids = ids+c.getInt(c.getColumnIndex(DatabaseAdapter.KEY_ID))+"\n"; names = names+c.getString(c.getColumnIndex(DatabaseAdapter.KEY_NAME))+"\n"; phnos = phnos+c.getString(c.getColumnIndex(DatabaseAdapter.KEY_PH_NO))+"\n"; } while (c.moveToNext()); tvNmae.setText(names); tvPh.setText(phnos); tvId.setText(ids); } } }
*****************************************************sqlite
task with login json thruv databas e entriy and
display****************************
1)àActivityMain.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/background" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.vk.task.MainActivity"> <ImageView android:layout_width="250dp" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:src="@drawable/logo" android:layout_marginBottom="5dp" /> <android.support.design.widget.TextInputLayout android:id="@+id/materialEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical"> <EditText android:id="@+id/etusername" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/user" android:drawablePadding="5dp" android:hint="Enter Email" android:imeOptions="actionNext" android:inputType="textEmailAddress" android:scaleY="1" android:singleLine="true" android:layout_marginBottom="30dp" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:drawablePadding="5dp" android:id="@+id/etpassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/password" android:hint="Enter Password" android:imeOptions="actionNext" android:inputType="textPassword" android:scaleY="1" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btlogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="LogIn" android:textColor="@color/colorAccent" android:textSize="18dp" /> </LinearLayout> </ScrollView>
2)àhome.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="Email" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="Password" /> </TableRow> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/tvemail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tvpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> </TableLayout>
3)àhome.java
public class home1 extends AppCompatActivity { private DatabaseAdapter db; private TextView tvpassword,tvemail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home1); tvemail=(TextView)findViewById(R.id.tvemail); tvpassword=(TextView)findViewById(R.id.tvpassword); db = new DatabaseAdapter(home1.this); displayDetails(); } private void displayDetails(){ Cursor c=db.getContactsCount(); if (c!=null && c.getCount()>0){ String ids=""; String names=""; String phnos=""; //c.getColumnIndex(DatabaseAdapter.KEY_ID) //c.getColumnIndex(DatabaseAdapter.KEY_NAME) //c.getColumnIndex(DatabaseAdapter.KEY_PH_NO) c.moveToFirst(); do { names = names+c.getString(c.getColumnIndex(DatabaseAdapter.KEY_NAME))+"\n"; phnos = phnos+c.getString(c.getColumnIndex(DatabaseAdapter.KEY_PH_NO))+"\n"; }while (c.moveToNext()); tvemail.setText(names); tvpassword.setText(phnos); // tvId.setText(ids); } } }
4)àcontactDetail.java
public class contactDetails { private String email,password; private int id; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
5)àDatabaseAdepter.java
public class DatabaseAdapter extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 2; // Database Name private static final String DATABASE_NAME = "contactsManager1"; // Contacts table name private static final String TABLE_CONTACTS = "contacts"; // Contacts Table Columns names public static final String KEY_ID = "id"; public static final String KEY_NAME = "name"; public static final String KEY_PH_NO = "phone_number"; public DatabaseAdapter(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")"; db.execSQL(CREATE_CONTACTS_TABLE); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); // Create tables again onCreate(db); } public long insertContacts(contactDetails contactDetails){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, contactDetails.getEmail()); // Contact Name values.put(KEY_PH_NO, contactDetails.getPassword()); // Contact Phone Number // Inserting Row long isInsert=db.insert(TABLE_CONTACTS, null, values); Log.e("Insert","-----"+isInsert); //Toast.makeText(DatabaseAdapter.this, "Inserted", Toast.LENGTH_SHORT).show(); db.close(); // Closing database connection return isInsert; } public Cursor getContactsCount() { String countQuery = "SELECT * FROM " + TABLE_CONTACTS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); //cursor.close(); // return count return cursor; } // Getting contacts Count }
6)àMainActivity.java
public class MainActivity extends AppCompatActivity { private DatabaseAdapter db; private EditText etusername, etpassword; private Button btn; private String email, password; private String id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init() { etusername = (EditText) findViewById(R.id.etusername); etpassword = (EditText) findViewById(R.id.etpassword); btn = (Button) findViewById(R.id.btlogin); db = new DatabaseAdapter(MainActivity.this); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { email = etusername.getText().toString(); password = etpassword.getText().toString(); contactDetails contactDetails = new contactDetails(); contactDetails.setEmail(email); contactDetails.setPassword(password); //long insert=db.insertContacts(name,phno); long insert = db.insertContacts(contactDetails); Toast.makeText(MainActivity.this, "inserted", Toast.LENGTH_SHORT).show(); new Retrive().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, email, password); } }); } class Retrive extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... param) { String data = null; String success = null; try { data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(param[0], "UTF-8"); data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(param[1], "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String text = ""; BufferedReader reader = null; // Send data try { // Defined URL where to send data URL url = new URL("http://pujansoftglobalsolutions.com/demo/android/parlor/login.php"); // Send POST data request URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the server response reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; // Read Server Response while ((line = reader.readLine()) != null) { // Append server response in string sb.append(line + "\n"); } text = sb.toString(); try { JSONObject j = new JSONObject(text); success = j.getString("success"); JSONArray j1 = new JSONArray("result"); for (int i = 0; i < j1.length(); i++) { JSONObject obj = j1.getJSONObject(i); id = obj.getString("registration_id"); } } catch (JSONException e) { // e.printStackTrace(); } } catch (Exception ex) { } finally { try { reader.close(); } catch (Exception ex) { } } return success; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s.equals("1")) { Toast.makeText(MainActivity.this, "Log In Success", Toast.LENGTH_SHORT).show(); Intent inttt = new Intent(getApplicationContext(), home1.class); startActivity(inttt); } else { Toast.makeText(MainActivity.this, "Invalid Email", Toast.LENGTH_SHORT).show(); } } } }
Comments
Post a Comment