码迷,mamicode.com
首页 > 其他好文 > 详细

学生管理系统的学习

时间:2014-10-18 22:11:54      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   io   os   ar   java   sp   

经过一天的学习,终于对数据库的操作有了一定成度的了解,但是期间出现了几次bug,真心体会到bug销魂,让人醉生梦死啊,不多说了上代码

涉及的主要内容

1 创建 数据库的帮助类sqliteophelper,并重写其构造方法

package com.djf.salary.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 创建一个数据库打开的帮助类,实现默认的构造方法,名称 游标工厂 数据库的版本 在oncreate方法中执行sql语句,创建一张表格
 * 
 * 
 */
public class SalaryInfoDBOpenHelper extends SQLiteOpenHelper {
    

    public SalaryInfoDBOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    // 实现一些默认的构造方法
    

    // 数据库被创建的的时候开始调用,用于数据表结构的初始化
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table salaryinfo(_id integer primary key autoincrement,name varchar(10),company varchar(20),salary integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

 

2 创建数据库

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		helper = new SalaryInfoDBOpenHelper(MainActivity.this, "student.db",
				null, 1);
		dao = new StudentInfoDao(this);
		tv = (TextView) findViewById(R.id.tv);

	}

 

3 创建学生信息对象,并写出get set 方法

package com.djf.salary.domain;

public class StudentInfo {
	private int id;
	private String name;
	private String company;
	private int salary;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "StudentInfo [id=" + id + ", name=" + name + ", company="
				+ company + ", salary=" + salary + "]";
	}
}

 

4 创建数据库增删改查的方法

package com.djf.salary.db.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.djf.salary.db.SalaryInfoDBOpenHelper;
import com.djf.salary.domain.StudentInfo;

public class StudentInfoDao {
 private Context context;
 private SalaryInfoDBOpenHelper helper;

 public StudentInfoDao(Context context) {
  this.context = context;
  helper = new SalaryInfoDBOpenHelper(context, "student.db", null, 1);
  // TODO Auto-generated constructor stub
 }

 public void insert(String name, String company, int salary) {

  SQLiteDatabase db = helper.getWritableDatabase();
  db.execSQL(
    "insert into salaryinfo (name,company,salary) values (? ,? ,?)",
    new Object[] { name, company, salary });
  db.close();
 }

 public void delete(int id) {

  SQLiteDatabase db = helper.getWritableDatabase();
  db.execSQL("delete from salaryinfo where _id=?", new Object[] { id });
  db.close();
 }

 public void upgreadsalary(int salary, int id) {
  SQLiteDatabase db = helper.getWritableDatabase();
  db.execSQL("update salaryinfo set salary=? where _id=?", new Object[] {
    salary, id });
  db.close();
 }

 public StudentInfo find(int id) {

  SQLiteDatabase db = helper.getReadableDatabase();
  // 得到结果的游标集
  Cursor cursor = db.rawQuery("select * from salaryinfo where _id= ? ",
    new String[] { String.valueOf(id) });

  boolean result = cursor.moveToNext();
  StudentInfo studentInfo = null;
  if (result) {
   String name = cursor.getString(cursor.getColumnIndex("name"));
   String company = cursor.getString(cursor.getColumnIndex("company"));
   int salary = cursor.getInt(cursor.getColumnIndex("salary"));
   studentInfo = new StudentInfo();
   studentInfo.setCompany(company);
   studentInfo.setName(name);
   studentInfo.setSalary(salary);
   studentInfo.setId(id);

  }
  cursor.close();
  db.close();
  return studentInfo;
 }
}

 

5 对数据库进行操作

public void add(View view) {
  dao.insert("张三", "小米科技", 10000);
 }

 public void delete(View view) {
  dao.delete(32);
 }

 public void update(View view) {
  dao.upgreadsalary(9999, 2);

 }

 public void query(View view) {
  StudentInfo info = dao.find(3);
   int id =info.getId();
   String name = info.getName();
   String company = info.getCompany();
   int salary = info.getSalary();
   tv.setText("id=" + id + "\n" + "name=" + name + "\n" + "company="
   + company + "\n" + "salary=" + salary);
   System.out.println("学生信息:"+info.toString()
     );
  // if (info==null) {
  // tv.setText("duanjiefei");
  // }
  // else{
  // tv.setText("liudandan");
  // }
  // db = helper.getReadableDatabase();
  // Cursor cursor = db.query("salaryinfo", new String[] { "_id", "name",
  // "company" }, null, null, null, null, null);
  // while (cursor.moveToNext()) {
  // int id = cursor.getInt(cursor.getColumnIndex("_id"));
  // String name = cursor.getString(cursor.getColumnIndex("name"));
  // String company = cursor.getString(cursor.getColumnIndex("company"));
  // String result = id+" "+name+" "+company+" \n";
  // tv.setText(result);
  // }
//  if (info == null) {
//   tv.setText("duanjiefi");
//  } else {
//   // tv.setText("liudandan");
//   String name = info.getName();
//   String company =info.getCompany();
//   int id = info.getId();
//   int salary = info.getSalary();
//   tv.setText("姓名:"+name+"\n"+"公司:"+company+"\n"+"薪水:"+salary);
//  }
//  String name = info.getName();
//  String company =info.getCompany();
// int id = info.getId();
// int salary = info.getSalary();
// tv.setText("姓名:"+name+"\n"+"公司:"+company+"\n"+"薪水:"+salary);

 }

 期间出现了空指针异常,改了一下午,迷迷糊糊的bug竟然消失了,一直感觉没有操作新的东西,希望在后续的学习中的能够解决类似的问题~~~~~~~

学生管理系统的学习

标签:android   style   blog   color   io   os   ar   java   sp   

原文地址:http://www.cnblogs.com/jiefeiduan/p/4033636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!