samedi 27 décembre 2014

Android Programming SQLite

After clicking submit button, a massage arises: unfortunately SQLite_demo has stop working, Please help me... and one more thing that when the project first displayed on emulator a toast show a pop up :"constructor called " please explain why...


here is my code...........


RahulDatabaseHelper.java



public class RahulDatabaseHelper{
RahulHelper RH;
public RahulDatabaseHelper(Context context)
{
RH=new RahulHelper(context);
}
public long insertData(String username,String userpassword)
{
SQLiteDatabase sqLiteDatabase=RH.getWritableDatabase();**strong text**
ContentValues Values=new ContentValues();
Values.put(RahulHelper.NAME,username);
Values.put(RahulHelper.PASSWORD,userpassword);
long id=sqLiteDatabase.insert(RahulHelper.TABLE_NAME,null,Values);
sqLiteDatabase.close();
return id;
}


static class RahulHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME="vivzdatabase.db";
private static final String TABLE_NAME="VIVZTABLE";
private static final String UID="_id";
private static final String NAME="Name";
private static final String PASSWORD="Password";
private static final int DATABASE_VERSION=4;
private static final String DATABASE_CREATE ="create table" +TABLE_NAME+ "("+UID+ "integer primary key autoincrement, "
+NAME+ "VARCHAR(255)"+PASSWORD+"VARCHAR(255));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS"+TABLE_NAME;

Context context;
RahulHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
Message.mess(context,"Constructor called");
}

@Override
public void onCreate(SQLiteDatabase db) {

try {
db.execSQL(DATABASE_CREATE);
Message.mess(context,"onCreate called");
}catch (SQLException e)
{Message.mess(context,""+e);}

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

try {
Message.mess(context,"onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (SQLException e)
{Message.mess(context,""+e);}
}
}}


MainActivity.java



public class MainActivity extends ActionBarActivity {

EditText et1,et2;
RahulDatabaseHelper rahulDatabaseHelper;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


et1=(EditText)findViewById(R.id.username);
et1=(EditText)findViewById(R.id.userpass);

rahulDatabaseHelper=new RahulDatabaseHelper(this);

}

public void method(View v)
{
String name=et1.getText().toString();
String pass=et2.getText().toString();

long id=rahulDatabaseHelper.insertData(name,pass);
if (id<0)
Message.mess(this,"UnSuccessFull");
else
Message.mess(this,"Row Inserted SuccessFully");

}}


activity_main.xml



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter Name"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter usr name"
android:layout_below="@+id/textView"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter Password"
android:id="@+id/textView2"
android:layout_below="@+id/username"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<EditText
android:id="@+id/userpass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:layout_below="@+id/textView2"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="method"
android:id="@+id/button"
android:layout_below="@+id/userpass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />


Message.java



public class Message {

public static void mess(Context context,String string)
{
Toast.makeText(context,string,Toast.LENGTH_LONG).show();
}}

Aucun commentaire:

Enregistrer un commentaire