DBHELPER ..........}}}}}}}}}
package com.example.userlap.model2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
public static final String Database="Model.db";
public DBHelper(Context context) {
super(context, Database, null, 1);
//SQLiteDatabase db=this.getWritableDatabase() ;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ UserProfile.Users.TableName+" ("+ UserProfile.Users.Col_1+" Integer primary key AUTOINCREMENT,"+ UserProfile.Users.Col_2 +" TEXT,"+ UserProfile.Users.Col_3 +" TEXT,"+ UserProfile.Users.Col_4 +" TEXT,"+ UserProfile.Users.Col_5 +" TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table if exists "+ UserProfile.Users.TableName);
onCreate(db);
}
public boolean addInfo(String uname,String DOb,String password,String Gender){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(UserProfile.Users.Col_2,uname);
cv.put(UserProfile.Users.Col_3,DOb);
cv.put(UserProfile.Users.Col_4,password);
cv.put(UserProfile.Users.Col_5,Gender);
long ans=db.insert(UserProfile.Users.TableName,null,cv);
if(ans==-1){
return false;
}
else
return true;
}
public boolean updateInfor(int id,String uname,String DOb,String password,String Gender){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(UserProfile.Users.Col_2,uname);
cv.put(UserProfile.Users.Col_3,DOb);
cv.put(UserProfile.Users.Col_4,password);
cv.put(UserProfile.Users.Col_5,Gender);
long ans=db.update(UserProfile.Users.TableName,cv,"UserInfo="+id,null);
if(ans==-1){
return false;
}
else
return true;
}
public List readAllInfor(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
public List readAllInfor(int id){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName+" where _ID="+id,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
public Integer deleteInfo(int id){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(UserProfile.Users.TableName,"_ID=?"+id,null);
}
}
USERPROFILE ---------------------------------------------- -------
package com.example.userlap.model2;
import android.provider.BaseColumns;
public class UserProfile {
private UserProfile() {
}
public final static class Users implements BaseColumns{
public static final String Col_1="_ID";
public static final String Col_2="userName";
public static final String Col_3="dateOfBirth";
public static final String Col_4="Password";
public static final String Col_5="Gender";
public static final String TableName="UserInfo";
}
}
HOME ---------------------------------------------- -----
package com.example.userlap.model2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class Home extends AppCompatActivity {
DBHelper mydb;
Button register,login;
EditText id,pw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mydb=new DBHelper(this);
register=(Button)findViewById(R.id.button2);
login=(Button)findViewById(R.id.button) ;
id=(EditText)findViewById(R.id.editText);
pw=(EditText)findViewById(R.id.editText2);
login();
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i1=new Intent(Home.this,ProfileManagement.class);
startActivity(i1);
}
});
}
public void login(){
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<ui> list=(ArrayList<ui>)mydb.readAllInfor(Integer.parseInt(id.getText().toString()));
if(!list.isEmpty()){
for(ui u:list){
if(u.getID()==Integer.parseInt(id.getText().toString()) && u.getPasssword().equals(pw.getText().toString())){
Intent i1=new Intent(Home.this,EditProfile.class);
startActivity(i1);
}
}
}
}
});
}
}
ProfileManagement ----------------------------
package com.example.userlap.model2;
import android.content.Intent;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class ProfileManagement extends AppCompatActivity {
Button edit,register;
EditText uname,pass,dob;
DBHelper mydb;
RadioButton type;
RadioGroup gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_management);
mydb=new DBHelper(this);
edit=(Button)findViewById(R.id.button6);
register=(Button)findViewById(R.id.button7);
uname=(EditText)findViewById(R.id.editText6);
pass=(EditText)findViewById(R.id.editText7);
dob=(EditText)findViewById(R.id.editText8);
gender=(RadioGroup)findViewById(R.id.radioGroup2);
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i2=new Intent(ProfileManagement.this,EditProfile.class);
startActivity(i2);
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean res= mydb.addInfo(uname.getText().toString(),dob.getText().toString(),pass.getText().toString(),gtype(gender));
if(res=true){
Toast.makeText(ProfileManagement.this,"Successful",Toast.LENGTH_LONG).show();
}
else
Toast.makeText(ProfileManagement.this,"NOT ",Toast.LENGTH_LONG).show();
}
});
}
public String gtype(View v){
int res=gender.getCheckedRadioButtonId();
type=(RadioButton)findViewById(res);
return type.getText().toString();
}
}
EditProfile ---------------------------------------------- -------------
package com.example.userlap.model2;
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.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
public class EditProfile extends AppCompatActivity {
Button search,edit,delete;
DBHelper mydb;
RadioButton type,male,female;
RadioGroup gender;
EditText uname,pass,dob,id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydb=new DBHelper(this);
//ArrayList<ui> list11=new ArrayList<>();
setContentView(R.layout.activity_edit_profile);
search=(Button)findViewById(R.id.button3);
edit=(Button)findViewById(R.id.button4);
delete=(Button)findViewById(R.id.button5);
uname=(EditText)findViewById(R.id.editText3);
pass=(EditText)findViewById(R.id.editText5);
dob=(EditText)findViewById(R.id.editText4);
gender=(RadioGroup)findViewById(R.id.radioGroup);
id=(EditText)findViewById(R.id.editText9);
male=(RadioButton)findViewById(R.id.radioButton);
female=(RadioButton)findViewById(R.id.radioButton2);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<ui> list11= (ArrayList<ui>) mydb.readAllInfor(Integer.parseInt(id.getText().toString()));
if(!list11.isEmpty()){
for(ui i:list11){
uname.setText(i.getUsename());
pass.setText(i.getPasssword());
dob.setText(i.getDOB());
if(i.getGender().equals("Male")){
male.setChecked(true);
}
else
female.setChecked(true);
}
}
}
});
}
public String gtype(View v){
int res=gender.getCheckedRadioButtonId();
type=(RadioButton)findViewById(res);
return type.getText().toString();
}
}
ui ---------------------------------------------- -------------------------------------
package com.example.userlap.model2;
public class ui {
private String usename;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
private int ID;
private String Passsword;
private String DOB;
private String Gender;
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPasssword() {
return Passsword;
}
public void setPasssword(String passsword) {
Passsword = passsword;
}
public String getDOB() {
return DOB;
}
public void setDOB(String DOB) {
this.DOB = DOB;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
}
答案 0 :(得分:0)
-使用游标-
选择单个记录
public MovieDetailsVO getMovie(int movie_id) {
MovieDetailsVO movieDetails = new MovieDetailsVO();
SQLiteDatabase db = this.getReadableDatabase();
//specify the columns to be fetched
String[] columns = {KEY_MOVIE_ID, KEY_MOVIE_NAME, KEY_GENRE, KEY_YEAR, KEY_RATING};
//Select condition
String selection = KEY_MOVIE_ID + " = ?";
//Arguments for selection
String[] selectionArgs = {String.valueOf(movie_id)};
Cursor cursor = db.query(TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
movieDetails.setMovieId(cursor.getInt(0));
movieDetails.setMovieName(cursor.getString(1));
movieDetails.setGenre(cursor.getString(2));
movieDetails.setYear(cursor.getInt(3));
movieDetails.setRating(cursor.getDouble(4));
}
db.close();
return movieDetails;
}
检索所有记录
public List getAllMovies() {
List movieDetailsList = new ArrayList();
String selectQuery = "SELECT * FROM " + TABLE_NAME
+ " ORDER BY " + KEY_MOVIE_ID + " DESC";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//if TABLE has rows
if (cursor.moveToFirst()) {
//Loop through the table rows
do {
MovieDetailsVO movieDetails = new MovieDetailsVO();
movieDetails.setMovieId(cursor.getInt(0));
movieDetails.setMovieName(cursor.getString(1));
movieDetails.setGenre(cursor.getString(2));
movieDetails.setYear(cursor.getInt(3));
movieDetails.setRating(cursor.getDouble(4));
//Add movie details to list
movieDetailsList.add(movieDetails);
} while (cursor.moveToNext());
}
db.close();
return movieDetailsList;
}
答案 1 :(得分:0)
选择单个记录
public List readAllInfor(int id){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName+" where _ID="+id,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
-调用
ArrayList<ui> list11= (ArrayList<ui>) mydb.readAllInfor(Integer.parseInt(id.getText().toString()));
if(!list11.isEmpty()){
for(ui i:list11){
uname.setText(i.getUsename());
pass.setText(i.getPasssword());
dob.setText(i.getDOB());
if(i.getGender().equals("Male")){
male.setChecked(true);
}
else
female.setChecked(true);
选择所有记录
public List readAllInfor(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
-调用
ArrayList<ui> list11= (ArrayList<ui>) mydb.readAllInfor();
if(!list11.isEmpty()){
for(ui i:list11){
uname.setText(i.getUsename());
pass.setText(i.getPasssword());
dob.setText(i.getDOB());
if(i.getGender().equals("Male")){
male.setChecked(true);
}
else
female.setChecked(true);