继续上一个项目SQLiteDatabase的User类登陆注册Student类增删改查

Spring5945 2016-04-03 01:37:36
上一个项目还没讲完,地址如下
http://bbs.csdn.net/topics/391928080

User逻辑层如下:


Student逻辑层如下:
package com.example.sqlitedatabase;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.iotek.entity.Student;

public class StudentBiz {
private DBHelper dbHelper = null;

public StudentBiz(Context context) {
dbHelper = new DBHelper(context);
}

/**
* 添加一个学生
*
* @param stu
* @return
*/
public long addStudent(Student stu) {
/**
* 这行代码一定要有,getWritableDatabase()获取SQLiteDatabase实例,才能创建出表
*/
SQLiteDatabase db = dbHelper.getWritableDatabase();
/*
* db.execSQL("insert into student(name,age,score)values(?,?,?)", new
* Object[] { stu.getName(), stu.getAge(), stu.getScore() });
*/
ContentValues values = new ContentValues();
values.put("name", stu.getName());
values.put("age", stu.getAge());
values.put("score", stu.getScore());
// insert into student() values()
long id = db.insert("student", null, values);// 返回的是记录的id
Log.i("add", id+"");
return id;
}

/**
* 删除一个学生
*
* @param _id
* @return
*/
public int delStudent(int _id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// db.execSQL("delete from student where _id=?", new Object[] { _id });
int rows = db.delete("student", "_id=?", new String[] { _id + "" });// 返回影响的行数
return rows;
}

/**
* 更新一个学生信息
*
* @param stu
* @return
*/
public int updateStudent(Student stu) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
/*
* db.execSQL( "update student set name=?,age=?,score=? where _id=?",
* new Object[] { stu.getName(), stu.getAge(), stu.getScore(),
* stu.get_id() });
*/
ContentValues values = new ContentValues();
values.put("name", stu.getName());
values.put("age", stu.getAge());
values.put("score", stu.getScore());
int rows = db.update("student", values, "_id=?",
new String[] { stu.get_id() + "" });
return rows;
}

/**
* 得到所有的学生
*
* @return
*/
public List<Student> getAllStudents() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
List<Student> stuList = new ArrayList<Student>();
// 返回的是游标
/*
* Cursor cursor = db.rawQuery("select _id,name,age,score from student",
* null);
*/
Cursor cursor = db.query("student", new String[] { "_id", "name",
"age", "score" }, null, null, null, null, null);
while (cursor.moveToNext()) {
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
int score = cursor.getInt(cursor.getColumnIndex("score"));
stuList.add(new Student(_id, name, age, score));

}
return stuList;
}

/**
* 根据学号查询一个学生
*
* @param _id
* @return
*/
public Student getStudentById(int _id) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Student stu = null;
// 返回的是游标
/*
* Cursor cursor = db.rawQuery(
* "select _id,name,age,score from student where _id=?", new String[] {
* _id + "" });
*/

Cursor cursor = db.query("student", new String[] { "_id", "name", "age", "score" },
"_id=?", new String[] { _id + "" }, null, null, null);
if (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
int score = cursor.getInt(cursor.getColumnIndex("score"));
stu = new Student(id, name, age, score);

}
return stu;
}

}


布局文件如下:
activity_main


item
就一个textView,放适配器用的,ListView里显示的控件布局为TextView


Login


register



具体的控件id在上一个项目能够根据封装的findView()方法里对应的找到,每个activity对应每个布局layout。

...全文
327 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Douhaining_ 2018-06-22
  • 打赏
  • 举报
回复
student 和 user 类的代码没有啊,可以发一下吗
d-lancer 2016-06-11
  • 打赏
  • 举报
回复
求源码啊,多谢楼主

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧