Student Management System Learning

Source: Internet
Author: User

After a day of study, I finally got a certain degree of understanding about database operations, but several bugs occurred during the period. I really realized the bug was so fascinating that it was just a nightmare, not much about the code

Main Content involved

1. Create the help class sqliteophelper for the database and rewrite its constructor.

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;/*** creates a help class for opening a database to implement the default constructor. The name cursor factory database version executes the SQL statement in the oncreate method, create a table ***/public class salaryinfodbopenhelper extends sqliteopenhelper {public salaryinfodbopenhelper (context, string name, cursorfactory factory, int version) {super (context, name, factory, version ); // todo auto-generated constructor stub} // implement some default constructor methods. // call this method when the database is created, used for data table structure initialization @ override public void oncreate (sqlitedatabase dB) {// todo auto-generated method stub db.exe csql ("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. Create a database

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. Create student information objects and write out the get set method.

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;}@Overridepublic String toString() {return "StudentInfo [id=" + id + ", name=" + name + ", company="+ company + ", salary=" + salary + "]";}}

 

4. Create a database for addition, deletion, modification, and query

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; private salaryinfodbopenhelper helper; Public studentinfodao (context) {This. context = context; helper = new SA Laryinfodbopenhelper (context, "student. DB ", null, 1); // todo auto-generated constructor stub} public void insert (string name, string company, int salary) {sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql ("insert into salaryinfo (name, company, salary) values (? ,? ,?) ", New object [] {name, company, salary}); dB. close ();} public void Delete (int id) {sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql ("delete from salaryinfo where _ id =? ", New object [] {ID}); dB. close ();} public void upgreadsalary (INT salary, int ID) {sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql ("Update salaryinfo set salary =? Where _ id =? ", New object [] {salary, Id}); dB. close ();} public studentinfo find (int id) {sqlitedatabase DB = helper. getreadabledatabase (); // The result set cursor = dB. rawquery ("select * From salaryinfo where _ id =? ", New string [] {string. valueof (ID)}); boolean result = cursor. movetonext (); 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. operate databases

Public void add (view) {Dao. insert ("Zhang San", "mi technology", 10000);} public void Delete (view) {Dao. delete (32);} public void Update (view) {Dao. upgreadsalary (9999, 2);} public void query (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 ("Student Information:" + info. tostring (); // If (Info = NULL) {// TV. settext ("duanjiefei"); //} // else {// TV. settext ("liudandan"); //} // DB = helper. getreadabledatabase (); // cursor = dB. query ("salaryinfo", new string [] {"_ id", "name", // "company"}, 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:" + name + "\ n" + "Company:" + company + "\ n" + "Salary:" + salary ); ///} // string name = info. getname (); // string Company = info. getcompany (); // int id = info. GETID (); // int salary = info. getsalary (); // TV. settext ("name:" + name + "\ n" + "Company:" + company + "\ n" + "Salary:" + salary );}

During this period, a null pointer exception occurred. After an afternoon's modification, the confused bug disappears and the system has never been able to operate on new things, we hope to solve similar problems in the future ~~~~~~~

Student Management System Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.