android:学生管理系统-SQlite

利用sqlite数据库管理学生的信息--完成了增删改查

输入all和0清空整张表

MainActivity:

package com.fs.act;


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


import com.fs.act.R.id;


import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {
private Button add, delete, update, query,update2;
private EditText editvalue, editname;
private TextView tv;
private String NAME="woshikongde";
private int VALUE2 =1024;


private List list = null;
private ArrayAdapter adapter;



public void init() {
this.list = new ArrayList();
this.tv = (TextView) findViewById(R.id.tv);
this.update2 = (Button) findViewById(R.id.update2);
this.add = (Button) findViewById(R.id.add);
this.delete = (Button) findViewById(R.id.delete);
this.update = (Button) findViewById(R.id.update);
this.query = (Button) findViewById(R.id.query);
// this.editid = (EditText) findViewById(R.id.editid);
this.editvalue = (EditText) findViewById(R.id.editvalue);
this.editname = (EditText) findViewById(R.id.editname);


this.add.setOnClickListener(this);
this.delete.setOnClickListener(this);
this.update.setOnClickListener(this);
this.query.setOnClickListener(this);
this.update2.setOnClickListener(this);
}


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.init();
ListView listView = (ListView) this.findViewById(R.id.listView);
this.adapter = new ArrayAdapter(this, R.layout.name, this.list);
listView.setAdapter(adapter);
}


public SQLiteDatabase getDB() {
Sqlite sqlite = new Sqlite(this, "student", null, 1);
// when get SqliteDatabase Object first,
// sqlite.onCreate(db)
SQLiteDatabase db = sqlite.getReadableDatabase();
return db;
}


public void onClick(View v) {
SQLiteDatabase db = this.getDB();
// TODO Auto-generated method stub
int id = v.getId();
if (id == R.id.update2) {

this.list.clear();

// String querySql
// ="select _id ,name,age from stu ";
// Cursor cursor = db.rawQuery(querySql, null);
// System.out.println("zheli!!!!!!!!!!!!");
// String inf = "";
// while (cursor.moveToNext()) {
// int _id = cursor.getInt(0);
// String name = cursor.getString(1);
// int value2 = cursor.getInt(2);
// inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);
// }
if((NAME.equals("woshikongde"))&&(VALUE2==1024)){
Toast.makeText(this, "需要修改数据的时候再按,谢谢", Toast.LENGTH_SHORT).show();
}
if((NAME.equals("woshikongde"))&&(VALUE2!=1024)){
Toast.makeText(this, "需要修改数据的时候再按,谢谢", Toast.LENGTH_SHORT).show();
}
if(!(NAME.equals("woshikongde"))&&(VALUE2==1024)){
Toast.makeText(this, "谢谢", Toast.LENGTH_SHORT).show();
}
if(!(NAME.equals("woshikongde"))&&(VALUE2!=1024)){
Toast.makeText(this, "改写成功", Toast.LENGTH_SHORT).show();

String scname = this.editname.getText().toString();
String scvalue = this.editvalue.getText().toString();
// int vv = Integer.parseInt(scvalue);
if((scname.length()!=0)&&(scvalue.length()==0)){
Toast.makeText(this, "输入错误", Toast.LENGTH_SHORT).show();
}
if((scname.length()==0)&&(scvalue.length()==0)){
Toast.makeText(this, "输入错误", Toast.LENGTH_SHORT).show();
}
if((scname.length()==0)&&(scvalue.length()!=0)){
Toast.makeText(this, "输入错误", Toast.LENGTH_SHORT).show();
}
if((scname.length()!=0)&&(scvalue.length()!=0)){
int vv = Integer.parseInt(scvalue);
String deleteSql
="update stu set name='"+scname+"', age ='"+vv+"' where name='"+NAME+"' and age ='"+VALUE2+"'";
db.execSQL(deleteSql);
String querySql
="select _id ,name,age from stu ";
Cursor cursor = db.rawQuery(querySql, null);
String ainf = "";
while (cursor.moveToNext()) {
int _aid = cursor.getInt(0);
String aname = cursor.getString(1);
int avalue2 = cursor.getInt(2);
ainf += "_id:" + _aid + ",name:" + aname + ",age:" + avalue2 + "\n";
// this.tv.setText(ainf);
}
this.list.add(ainf);
this.adapter.notifyDataSetChanged();
NAME = "woshikongde";
VALUE2 = 1024;
}
}


}
if (id == R.id.add) {

ContentValues cv = new ContentValues();
// String getid = this.editid.getText().toString();
// int gi = Integer.parseInt(getid);
String getname = this.editname.getText().toString();
String getvalue = this.editvalue.getText().toString();
if(getvalue.length()!=0) {
int gv = Integer.parseInt(getvalue);


db.insert("stu",null,cv);
// cv.put("_id", gi);
cv.put("name", getname);
cv.put("age", gv);
long t = db.insert("stu", null, cv);
System.out.println("ok--------------" + t);
String inff = ""+getname+""+gv;
Toast.makeText(this, inff, Toast.LENGTH_SHORT).show();
}

if(getvalue.length()==0) {
// int gv = Integer.parseInt(getvalue);


db.insert("stu",null,cv);
// cv.put("_id", gi);
cv.put("name", getname);
// cv.put("age", 0);
long t = db.insert("stu", null, cv);
System.out.println("ok--------------" + t);
String inff = ""+getname;
Toast.makeText(this, inff, Toast.LENGTH_SHORT).show();
}

}
if (id == R.id.query) {
this.list.clear();
// String querySql
// = "select _id, name, age from stu";
// Cursor cursor = db.rawQuery(querySql, null);
//
// String inf = "";
// System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
// while (cursor.moveToNext()) {
// int _id = cursor.getInt(0);
// String name = cursor.getString(1);
// int value2 = cursor.getInt(2);
// inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// }
//
// Toast.makeText(this, inf, Toast.LENGTH_SHORT).show();
String scname = this.editname.getText().toString();
String scvalue = this.editvalue.getText().toString();
// int vv = Integer.parseInt(scvalue);
// System.out.println("zheli############");
if((scname.length()!=0)&&(scvalue.length()==0)) {
// System.out.println("zheli???????????");
String querySql
="select _id ,name,age from stu where name='"+scname+"'";
Cursor cursor = db.rawQuery(querySql, null);
// System.out.println("zheli!!!!!!!!!!!!");
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);
}
this.list.add(inf);
this.adapter.notifyDataSetChanged();

}
if((scname.length()==0)&&(scvalue.length()==0)) {
// System.out.println("zheli???????????");
String querySql
="select _id ,name,age from stu ";
Cursor cursor = db.rawQuery(querySql, null);
// System.out.println("zheli!!!!!!!!!!!!");
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);

}
this.list.add(inf);
this.adapter.notifyDataSetChanged();

}

else if((scname.length()==0)&&scvalue.length()!=0) {
int vv = Integer.parseInt(scvalue);
String querySql
="select _id ,name,age from stu where age='"+vv+"'";
Cursor cursor = db.rawQuery(querySql, null);
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);
}
this.list.add(inf);
this.adapter.notifyDataSetChanged();
}

else if((scname.length()!=0)&&scvalue.length()!=0) {
int vv = Integer.parseInt(scvalue);
String querySql
="select _id ,name,age from stu where name='"+scname+"' and age='"+vv+"'";
Cursor cursor = db.rawQuery(querySql, null);
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);

}
this.list.add(inf);
this.adapter.notifyDataSetChanged();
}




}
//查询部分完成!!!!!!!!!!!
if (id == R.id.delete){
this.list.clear();
String scname = this.editname.getText().toString();
String scvalue = this.editvalue.getText().toString();
// int vv = Integer.parseInt(scvalue);
// int s = Integer.parseInt(scvalue);
if((scname.length()!=0)&&(scvalue.length()!=0)){
if((scname.equals("all"))&&(scvalue.equals("0")))
{
String deleteSql
=" drop  table stu  ";
db.execSQL(deleteSql);

String createSql
= "create table stu(_id integer primary key autoincrement, name char(10), age integer)";
db.execSQL(createSql);

}

else{

int vv = Integer.parseInt(scvalue);
String deleteSql
="delete from stu where name='"+scname+"'and age ='"+vv+"'";
db.execSQL(deleteSql);
String querySql
="select _id ,name,age from stu ";
Cursor cursor = db.rawQuery(querySql, null);
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);
}
this.list.add(inf);
this.adapter.notifyDataSetChanged();
}
}


if((scname.length()!=0)&&(scvalue.length()==0)){
// int vv = Integer.parseInt(scvalue);
String deleteSql
="delete from stu where name='"+scname+"'";
db.execSQL(deleteSql);
String querySql
="select _id ,name,age from stu ";
Cursor cursor = db.rawQuery(querySql, null);
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);
}
this.list.add(inf);
this.adapter.notifyDataSetChanged();
}
if((scname.length()==0)&&(scvalue.length()!=0)){
int vv = Integer.parseInt(scvalue);
String deleteSql
="delete from stu where age='"+vv+"'";
db.execSQL(deleteSql);
String querySql
="select _id ,name,age from stu ";
Cursor cursor = db.rawQuery(querySql, null);
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);
}
this.list.add(inf);
this.adapter.notifyDataSetChanged();
}
if((scname.length()==0)&&(scvalue.length()==0)){
String inf = "请输入你要删除的名字或年龄";
this.list.add(inf);
this.adapter.notifyDataSetChanged();
}
}




if (id == R.id.update) {
String scname = this.editname.getText().toString();
String scvalue = this.editvalue.getText().toString();
// int vv = Integer.parseInt(scvalue);
if((scname.length()!=0)&&scvalue.length()!=0) {
int vv = Integer.parseInt(scvalue);
String querySql
="select _id ,name,age from stu where name='"+scname+"' and age='"+vv+"'";
Cursor cursor = db.rawQuery(querySql, null);
String inf = "";
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int value2 = cursor.getInt(2);
// inf += "_id:" + _id + ",name:" + name + ",age:" + value2 + "\n";
// this.tv.setText(inf);

if ((name.length()!=0)&&(value2!=0)){
NAME = name;
VALUE2 = value2;
}
}

}


}


}
}


Sqlite.java:

package com.fs.act;


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


public class Sqlite extends SQLiteOpenHelper {
public Sqlite(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);//create database with name and version
}
/**
* create table ....  
* init table   ....
* */
@Override
public void onCreate(SQLiteDatabase db) {
String createSql
= "create table stu(_id integer primary key autoincrement, name char(10), age integer)";
db.execSQL(createSql);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}








/**
class Animal
{
Animal(int x, float y, boolean b){

}
}


class Girl extends Animal  {
Girl(int x, float y, boolean b) {
super(x, y, b);
}
}


 * new Girl(1, 2.0f, true);
 */
 

manifest:


      package="com.fs.act"
      android:versionCode="1"
      android:versionName="1.0">
   
                          android:label="@string/app_name">
           
               
               
           

       



   

   


 


main.xml:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent" 
    android:id = "@+id/tv"
    android:layout_height="wrap_content" 
    android:text="输入all和0清空表"
    
    />
      android:id="@+id/listView"
    android:layout_width="fill_parent" 
    android:layout_height="150sp"
    android:background="#ffffffff"
     
    />
  
          android:id="@+id/editname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="name"        
        android:textSize="20sp"
     />
          android:id="@+id/editvalue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="age" 
        android:digits="1234567890"       
        android:textSize="20sp"
     />
            android:id="@+id/query"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="query"
        android:textSize="20sp"
     />
            android:id="@+id/add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="add"
        android:textSize="20sp"
     />
              android:id="@+id/delete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="delete"
        android:textSize="20sp"
     />
              android:id="@+id/update"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="输入被改写的内容再按下这个键"
        android:textSize="20sp"
     />
                android:id="@+id/update2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="输入改写的内容按下这个键"
        android:textSize="20sp"
     />
  



name.xml:


    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#aaaaaaaa"
    android:textSize="40sp"
    android:textColor="#ff000000"
/>



你可能感兴趣的:(android:学生管理系统-SQlite)