Hai Friends today i am going to share about how we can connect android with SQLite Database in Simple way for Efficient Purpose..
Try this program on Android Ginger bread Version.
Snapshots:
Copy and Paste following code into Eclipse and Run the Program.
LoginSecurity.java
LoginSeminar.java
Try this program on Android Ginger bread Version.
Snapshots:
Copy and Paste following code into Eclipse and Run the Program.
LoginSecurity.java
package login.log; 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.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class LoginSecurity { String DbName="NewLogin"; String DbTable="logi"; int DbVersion=1; String Key_Name="Name"; String Key_Pass="Password"; String CreateTable="create table logi(Name text not null, Password text not null);"; sqlitehelper shelp; SQLiteDatabase sbase; Context con; public LoginSecurity(Context c) { con=c; } public LoginSecurity openforread() throws SQLException { shelp=new sqlitehelper(con, DbName, null, DbVersion); sbase=shelp.getReadableDatabase(); return this; } public LoginSecurity openforwrite() throws SQLException { shelp=new sqlitehelper(con, DbName, null, DbVersion); sbase=shelp.getWritableDatabase(); return this; } public void close() { shelp.close(); } public long insert(String s1, String s2) { ContentValues cv=new ContentValues(); cv.put(Key_Name, s1); cv.put(Key_Pass, s2); return sbase.insert(DbTable, null, cv); } public String getPass(String pa) { String[] col=new String[]{Key_Name, Key_Pass}; Cursor c=sbase.query(DbTable, col, null, null, null, null, null); String result=""; int index=c.getColumnIndex(Key_Pass); for(c.moveToFirst();!c.isAfterLast();c.moveToNext()) { result=c.getString(index); } return result; } public class sqlitehelper extends SQLiteOpenHelper { public sqlitehelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(CreateTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } }
LoginSeminar.java
package login.log; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class LoginSeminar extends Activity { /** Called when the activity is first created. */ Button btsub,btcan; Button btnreg; EditText myuser,mypass; LoginSecurity ls; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnreg=(Button)findViewById(R.id.button3); btcan=(Button)findViewById(R.id.button2); myuser=(EditText)findViewById(R.id.editText1); mypass=(EditText)findViewById(R.id.editText2); btsub=(Button)findViewById(R.id.button1); ls=new LoginSecurity(this); try { btsub.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String x=myuser.getText().toString(); String y=mypass.getText().toString(); String temp=""; ls.openforread(); temp=ls.getPass(x); ls.close(); if(temp.equals(y)) { Toast.makeText(getApplicationContext(), "You are valid user", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Sorry u r not valid user", Toast.LENGTH_SHORT).show(); } } }); } catch(Exception e) { Toast.makeText(getApplicationContext(), "ur login not exists pls register here", Toast.LENGTH_SHORT).show(); } btnreg.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent i=new Intent(getApplicationContext(), RegisterAccount.class); startActivity(i); } }); } }
RegisterAccount.java
Android – Creating Registration Page using SQLITE Database
ReplyDeletehttp://apptronix.net/registration-page-sqlite/