Program 10-3
Program 10-3
<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
MainActivity.java :
package com.example.tenprog;
import android.content.*;
import android.database.sqlite.*;
import android.os.*;
import android.widget.*;
import androidx.appcompat.app.*;
public class MainActivity extends AppCompatActivity {
EditText n,e,p;SQLiteDatabase d;
protected void onCreate(Bundle
b){super.onCreate(b);setContentView(R.layout.activity_main);
n=findViewById(R.id.editName); e=findViewById(R.id.editEmail);
p=findViewById(R.id.editPassword);
d=new DbHelper(this).getWritableDatabase();
findViewById(R.id.btnRegister).setOnClickListener(v->r());
}
void r(){
String N=n.getText().toString(),E=e.getText().toString(),P=p.getText().toString();
if(N.isEmpty()||E.isEmpty()||P.isEmpty()){Toast.makeText(this,"Fill all
fields",Toast.LENGTH_SHORT).show();return;}
ContentValues v=new
ContentValues();v.put("name",N);v.put("email",E);v.put("password",P);
if(d.insert("users",null,v)!=-
1){Toast.makeText(this,"Success",Toast.LENGTH_SHORT).show();n.setText("");e.setText("");
p.setText("");}
else Toast.makeText(this,"Failed",Toast.LENGTH_SHORT).show();
}
class DbHelper extends SQLiteOpenHelper{
DbHelper(Context c){super(c,"userdb",null,1);
}
public void onCreate(SQLiteDatabase d){
d.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY,name TEXT,email
TEXT,password TEXT)");
}
public void onUpgrade(SQLiteDatabase d,int o,int n){
d.execSQL("DROP TABLE IF EXISTS users");onCreate(d);
}
}
}