3 回答
TA贡献1942条经验 获得超3个赞
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "SURNAME"
;
public static final String COL4 = "MARKS";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
// SQLiteDatabase db = this.getWritableDatabase(); }
public void onCreate(SQLiteDatabase db) {
db.execSQL(" create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name, String surname, String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, surname);
contentValues.put(COL4, marks);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getAlldata() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
public boolean updateData(String id, String name, String surname, String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1, id);
contentValues.put(COL2, name);
contentValues.put(COL3, surname);
contentValues.put(COL4, marks);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[] {
id
});
return true;
}
}
public Integer deleteData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ? ", new String[] {
id
});
}
public class MainActivity extends AppCompatActivity {
public Button but1;
DatabaseHelper myDb;
EditText editName, editSurname, editMarks, editTextid;
Button btnAddData;
Button btnViewall;
public void init() {
but1 = (Button) findViewById(R.id.button5);
but1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent my = new Intent(MainActivity.this, Activity2.class);
startActivity(my);
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
myDb = new DatabaseHelper(this);
editName = (EditText) findViewById(R.id.editText_name);
editSurname = (EditText) findViewById((R.id.editText_surname));
editMarks = (EditText) findViewById((R.id.editText_marks));
editTextid = (EditText) findViewById(R.id.editText_id);
btnAddData = (Button) findViewById((R.id.button_add));
btnViewall = (Button) findViewById(R.id.button_view);
Adddata();
viewAll();
update();
}
public void Adddata() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editName.getText().toString(), editSurname.getText().toString(), editMarks.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data Not Inserted", Toast.LENGTH_LONG).show();
}
});
}
public void update() {
btnUpdate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isUpdate = myDb.updateData(editTextid.getText().toString(), editName.getText().toString(), editSurname.getText().toString(), editMarks.getText().toString());
if (isUpdate == true)
Toast.makeText(MainActivity.this, "Data Updated", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Not Updated", Toast.LENGTH_LONG).show();
}
}
public void Delete() {
btnDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer deleteRows = myDb.deleteData(editTextid.getText().toString());
if (deleteRows > 0)
Toast.makeText(MainActivity.this, "Data Updated", Toast.LENGTH_LONG).show();
}
});
}
public void viewAll() {
btnViewall.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAlldata();
if (res.getCount() == 0) {
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("id :" + res.getString(0) + "\n");
buffer.append("name :" + res.getString(1) + "\n");
buffer.append("surname: " + res.getString(2) + "\n");
buffer.append("marks: " + res.getString(3) + "\n\n");
TA贡献1776条经验 获得超12个赞
公共类 DatabaseHelper 扩展 SQLiteOpenHelper {
public static final String DATABASE_NAME = "student.db";
公共静态最终字符串 TABLE_NAME ="student_table";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "SURNAME"
; public static final String COL4 = "MARKS";
公共数据库助手(上下文上下文){
超级(上下文,DATABASE_NAME,空,1);
// SQLiteDatabase db = this.getWritableDatabase(); }
公共无效onCreate(SQLiteDatabase db){
db.execSQL(" 创建表" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)" );
}
@覆盖
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
公共布尔插入数据(字符串名称,字符串姓氏,字符串标记){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2,name);
contentValues.put(COL3,surname);
contentValues.put(COL4,marks);
长结果= db.insert(TABLE_NAME, null, contentValues);
如果(结果 == -1)
返回假;
别的
返回真;
}
公共游标 getAlldata(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null );
返回资源;}
}
公共布尔更新数据(字符串 ID,字符串名称,字符串姓氏,字符串标记){ SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1,id);
contentValues.put(COL2,name);
contentValues.put(COL3,surname);
contentValues.put(COL4,marks);
db.update(TABLE_NAME , contentValues, "ID = ?",new String[]{ id }); 返回真;} }
公共整数删除数据(字符串 ID){
SQLiteDatabase db = this.getWritableDatabase();
返回 db.delete(TABLE_NAME , "ID = ? ", new String[]{id}); }
公共类 MainActivity 扩展 AppCompatActivity {
公共按钮but1;
数据库助手 myDb;
EditText editName、editSurname、editMarks、editTextid;
按钮 btnAddData;
按钮 btnViewall;
公共无效初始化(){
but1 = (Button)findViewById(R.id.button5);
but1.setOnClickListener(
新的 View.OnClickListener() {
@覆盖
public void onClick(View v) { Intent my = new Intent(MainActivity.this,Activity2.class); 开始活动(我的);} });
@覆盖
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
工具栏工具栏 = (工具栏) findViewById(R.id.toolbar);
setSupportActionBar(工具栏);
myDb = 新的 DatabaseHelper(this);
editName = (EditText)findViewById(R.id.editText_name);
editSurname = (EditText)findViewById((R.id.editText_surname));
editMarks = (EditText)findViewById((R.id.editText_marks));
editTextid = (EditText)findViewById(R.id.editText_id);
btnAddData= (Button)findViewById((R.id.button_add));
btnViewall=(Button)findViewById(R.id.button_view);
添加数据();
查看全部();
更新();
}
公共无效添加数据(){
btnAddData.setOnClickListener (
新的 View.OnClickListener() {
@覆盖
public void onClick(View v) {
boolean isInserted = myDb.insertData( editName.getText().toString(), editSurname.getText().toString(), editMarks.getText().toString() );
if(isInserted == true)
Toast.makeText(MainActivity.this ,"数据插入", Toast.LENGTH_LONG).show();
别的
Toast.makeText(MainActivity.this,"未插入数据", Toast.LENGTH_LONG).show(); } }); }
公共无效更新(){
btnUpdate.setOnClickListener(
新的 View.OnClickListener() {
@覆盖
public void onClick(View v) {
boolean isUpdate = myDb.updateData(editTextid.getText().toString(), editName.getText().toString(), editSurname.getText().toString(), editMarks.getText().toString() );
if(isUpdate == true)
Toast.makeText(MainActivity.this , "数据更新", Toast.LENGTH_LONG).show();
别的
Toast.makeText(MainActivity.this, "Not Updated" , Toast.LENGTH_LONG).show(); } }
公共无效删除(){
btnDelete.setOnClickListener (
新的 View.OnClickListener() {
@覆盖
public void onClick(View v) {
整数 deleteRows = myDb.deleteData(editTextid.getText().toString());
如果(删除行> 0)
Toast.makeText(MainActivity.this , "数据更新", Toast.LENGTH_LONG).show(); } }); }
公共无效viewAll(){
btnViewall.setOnClickListener(
新的 View.OnClickListener() {
@覆盖
public void onClick(View v) {
光标 res = myDb.getAlldata();
if(res.getCount() == 0){
返回;
}
StringBuffer 缓冲区 = new StringBuffer();
而(res.moveToNext()){
buffer.append("id :" + res.getString(0)+ "\n");
buffer.append("name :" + res.getString(1)+ "\n");
buffer.append("姓氏:" + res.getString(2) +"\n");
buffer.append("标记:" + res.getString(3) + "\n\n");
公共最终类 UserProfile {
private UserProfile(){
}
public static UserProfile getProfile(){
UserProfile userProfile = new UserProfile();
return userProfile;
}
class Users implements BaseColumn{
public static final String TABLE_NAME = "UserInfo";
public static final String COL_ID = "_ID";
public static final String COL_USERNAME = "userName ";
public static final String COL_DOB = "dateOfBirth";
public static final String COL_GENDER = "Gender";
public static final String COL_PASSWORD = "Password";
private int id;
private String username;
private String dob;
private String gender;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public Users getUser(){
Users users = new Users();
return users;
}
}
添加回答
举报