数据存取(SQLite) 任务: 学生管理系统 其中实现学生信息的添加 ...

tenderlaSoftware and s/w Development

Dec 13, 2013 (3 years and 8 months ago)

68 views

数据存取

SQLite


任务:

学生管理系统


其中实现学生信息的添加、查看、删除。

任务实现:

一、数据库操作设计

建立类
DatabaseHelper

package

xsgl.test;


import

android.content.ContentValues;

import

android.content.Context;

import

android.database.Cursor;

import

android.database.sqlite.SQLiteDatabase;

import

a
ndroid.database.sqlite.SQLiteOpenHelper;


public

class

DatabaseHelper

extends

SQLiteOpenHelper{



private

static

final

String
DB_NAME

=
"xsxx.db"
;


private

static

final

String
TABLE_NAME

=
"xsxx_table"
;


private

static

final

String
CREATE_TABLE

=
"create t
able
"
+
"xsxx_table(_id integer primary key autoincrement,name text,cla
text,tel text)"
;


//

创建

SQLiteDatabase
实例


private

SQLiteDatabase
db
;



//

构造方法


DatabaseHelper(Context context){



super
(context,
DB_NAME
,
null
,2);


}

//

插入


public

void

insert(ContentValues values){



SQLiteDatabase db=getWritableDatabase();



db.insert(
TABLE_NAME
,
null
, valu
es);



db.close();


}



//

删除


public

void

del(
int

id){



if
(
db
==
null
)
db
=getWritableDatabase();



db
.delete(
TABLE_NAME
,
"_id=?"
,
new

String[]{String.
valueOf
(id)});


}



//

查询


public

Cursor query(){



SQLiteDatabase db=getWritableDatabase();



Cursor curs
or1=db.query(
TABLE_NAME
,
null
,
null
,
null
,
null
,
null
,
null
);



return

cursor1;


}



//

关闭


public

void

close(){



if
(
db
!=
null
)
db
.close();


}



//

重写

onCreate()
方法


public

void

onCreate(SQLiteDatabase db) {



//
TODO

Auto
-
generated method stub



this
.
db
=d
b;



db.execSQL(
CREATE_TABLE
);


}


//

重写

onUpgrade()
方法


public

void

onUpgrade(SQLiteDatabase db,
int

oldVersion,
int

newVersion) {



//
TODO

Auto
-
generated method stub




}



}



、主界面设计

(一)修改
main.xml
文件

<?
xml

version
=
"1.0"

encoding
=
"utf
-
8"
?>

<
LinearLayout

xmlns:android
=
"http://schemas.android.com/apk/res/android"


android:layout_width
=
"fill_parent"

android:layout_height
=
"fill_parent"


android:orientation
=
"vertical"
>


<
TextView

android:layout_width
=
"fill_parent"

android:textSize
=
"50px"



android:layout_heigh
t
=
"wrap_content"

android:text
=
"
学生管理系统
"

/>


<
TextView

android:layout_width
=
"fill_parent"

android:textSize
=
"16px"



android:layout_height
=
"wrap_content"

android:text
=
"
姓名:
"

/>


<
EditText

android:layout_height
=
"wrap_content"

android:id
=
"@+id/editText1"



android:layout_width
=
"fill_parent"
>
</
EditText
>


<
TextView

android:text
=
"
班级:
"

android:layout_width
=
"wrap_content"



android:textSize
=
"16px"

android:layout_height
=
"wrap_content"
></
TextView
>


<
EditText

android:layout_height
=
"wrap_content"

android:id
=
"@+id/editText2"



android:layout_width
=
"fil
l_parent"
></
EditText
>


<
TextView

android:text
=
"
电话:
"

android:layout_width
=
"wrap_content"



android:textSize
=
"16px"

android:layout_height
=
"wrap_content"
></
TextView
>


<
EditText

android:layout_height
=
"100px"

android:id
=
"@+id/editText3"



android:layout_width
=
"
fill_parent"
></
EditText
>


<
LinearLayout

android:layout_height
=
"wrap_content"



android:layout_width
=
"fill_parent"

android:gravity
=
"right"
>



<
Button

android:layout_height
=
"wrap_content"

android:id
=
"@+id/button1"




android:textSize
=
"16px"

android:text
=
"
保存
"

android:layout_width
=
"wrap_content"
></
Button
>



<
Button

android:layout_height
=
"wrap_content"

android:id
=
"@+id/button2"




android:textSize
=
"16px"

android:text
=
"
查看
"

android:layout_width
=
"wrap_content"
></
Button
>


</
LinearLayout
>

</
LinearLayout
>

(二)主界面的代码实现

package

xsgl.test;




import

xsgl.test.R;

import

android.app.Activity;

import

android.content.ContentValues;

import

android.content.Intent;

import

android.os.Bundle;

import

android.view.View;

import

android.widget.Button;

import

android.widget.EditText;


p
ublic

class

XsglActivity
extends

Activity {


/** Called when the activity is first created. */



private

EditText
editText_name
,
editText_class
,
editText_tel
;


private

Button
button_save
,
button_query
;




public

void

onCreate(Bundle savedInstanceState)
{


super
.onCreate(savedInstanceState);


setContentView(R.layout.
main
);


editText_name
=(EditText)findViewById(R.id.
editText1
);


editText_class
=(EditText)findViewById(R.id.
editText2
);


editText_tel
=(EditText)findViewById(R.
id.
editText3
);


button_save
=(Button)findViewById(R.id.
button1
);


button_query
=(Button)findViewById(R.id.
button2
);




button_save
.setOnClickListener(
new

View.OnClickListener() {




public

void

onClick(View v) {





//
TODO

Auto
-
g
enerated method stub





String name=
editText_name
.getText().toString();





String cla=
editText_class
.getText().toString();





String tel=
editText_tel
.getText().toString();





ContentValues values =
new

ContentValues();










values.put(
"name"
, name)
;





values.put(
"url"
,cla);





values.put(
"notes"
,tel);





DatabaseHelper

dbHelper =
new

DatabaseHelper
(getApplicationContext());





dbHelper.insert(values);










Intent intent =
new

Intent(XsglActivity.
this
,
Query_Test
.
class
);





startActivity(int
ent);




}



});




button_query
.setOnClickListener(
new

View.OnClickListener() {




public

void

onClick(View v) {





//
TODO

Auto
-
generated method stub





Intent intent =
new

Intent(XsglActivity.
this
,
Query_Test
.
class
);





startActivity(int
ent);




}



});




}

}

三、
查询界面设计

(一)修改
xsxxlist.xml

<?
xml

version
=
"1.0"

encoding
=
"utf
-
8"
?>

<
LinearLayout

xmlns:android
=
"http://schemas.android.com/apk/res/android"


android:layout_width
=
"fill_parent"

android:layout_height
=
"fill_parent"


android:orientation
=
"horizontal"
>


<
TextView

android:text
=
"TextView"

android:id
=
"@+id/textView1"



android:paddingRight
=
"10px"

android:layout_width
=
"wrap_content"



android:layout_height
=
"wrap_content"
></
TextView
>


<
TextView

android:text
=
"TextView"

android:id
=
"@+id/textView2"



android:paddingRight
=
"10px"

android:layout_width
=
"wrap_content"



android:layout_height
=
"wrap_content"
></
TextView
>


<
TextView

android:text
=
"TextView"

android:id
=
"@+id/textView3"



android:paddingRight
=
"10px"

android:layout_width
=
"wrap_content"



android:layout_height
=
"wrap_con
tent"
></
TextView
>


<
TextView

android:text
=
"TextView"

android:id
=
"@+id/textView4"



android:paddingRight
=
"10px"

android:layout_width
=
"wrap_content"



android:layout_height
=
"wrap_content"
></
TextView
>

</
LinearLayout
>

(

)
查询代码

package

xsgl.test;


import

android
.app.AlertDialog;

import

android.app.ListActivity;

import

android.content.DialogInterface;

import

android.database.Cursor;

import

android.os.Bundle;

import

android.view.View;

import

android.widget.AdapterView;

import

android.widget.AdapterView.OnItemClickL
istener;

import

android.widget.ListView;

import

android.widget.SimpleCursorAdapter;


public

class

Query_Test
extends

ListActivity{


protected

void

onCreate(Bundle savedInstanceState) {



//
TODO

Auto
-
generated method stub



super
.onCreate(savedInstanceStat
e);



final

DatabaseHelper dbHelper =
new

DatabaseHelper(
this
);



Cursor cursor = dbHelper.query();



String[] from = {
"_id"
,
"name"
,
"url"
,
"notes"
};



int
[] to =
{R.id.
textView1
,R.id.
textView2
,R.id.
textView3
,R.id.
textView4
};



SimpleCursorAdapter scadapter
=
new

SimpleCursorAdapter(
this
,R.layout.
xsxxlist
,cursor,from,to);



ListView listView = getListView();



listView.setAdapter(scadapter);



final

AlertDialog.Builder adBuilder =
new

AlertDialog.Builder(
this
);






listView.setOnItemClickListener(
new

OnItemC
lickListener() {




public

void

onItemClick(AdapterView<?> arg0, View arg1,
int

arg2,
long

arg3) {





//
TODO

Auto
-
generated method stub





final

long

temp = arg3;





adBuilder.setMessage(
"
确认要删除记录吗?
"
).setPositiveButton(
"
确认
"
,
new

DialogInterface.OnClickL
istener() {






public

void

onClick(DialogInterface dialog,
int

which)
{







//
TODO

Auto
-
generated method stub







dbHelper.del((
int
)temp);







Cursor cursor = dbHelper.query();







String[] from = {
"_id"
,
"name"
,
"url"
,
"notes"
};







int
[] to =
{
R.id.
textView1
,R.id.
textView2
,R.id.
textView3
,R.id.
textView4
};







SimpleCursorAdapter scadapter =
new

SimpleCursorAdapter(getApplicationContext(),R.layout.
xsxxlist
,cursor,
from,to);







ListView listView = getListView();







listView.setAdapter(scadap
ter);






}





}).setNegativeButton(
"
取消
"
,
new

DialogInterface.OnClickListener() {






public

void

onClick(DialogInterface dialog,
int

which)
{ }





});





AlertDialog aleraDialog = adBuilder.create();





aleraDialog.show();




}






});



dbHelper.c
lose();




}


}


知识梳理:

一、基础知识

DatabaseHelper
继承自

SQLit敏灥湈敬灥p

重写了
潮Cr敡e攨)

潮U灧牡摥r)
方法

实现

i湳敲e

摥d

q略特

捬潳o
等方法

Li獴View
显示记录,字段对应成

Te硴View

Li獴View


o湉n敭Click()
实现删除记录

数据库文件路径:
/摡d愯摡t愯<

㸯摡d慢慳支⨪⨮摢

二、插入函数

public void insert(Conte
ntValues values){


SQLiteDatabase db = getWritableDatabase();


db.insert(TABLE_NAME, null, values);


db.close();

}

三、删除函数

public void del(int id){


if(db == null) db = getWritableDatabase();


db.delete(TABLE_NAME, "_id=?", new String[]{String.valueOf(
id)});


}

四、
创建表

public void onCreate(SQLiteDatabase db) {


this.db = db;


db.execSQL(CREATE_TABLE);

}

五、查询函数

DatabaseHelper dbHelper = new DatabaseHelper(this);

Cursor cursor = dbHelper.query();

String[] from = {"_id","name","url","notes"};

int[] to = {R
.id.textView1,R.id.textView2,R.id.textView3,


R.id.textView4};

SimpleCursorAdapter scadapter = new SimpleCursorAdapter


(this,R.layout.favoritelist,cursor,from,to);

ListView listView = getListV
iew();

listView.setAdapter(scadapter);

AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);

课后练习:

站点收藏


其中能够对自己喜爱的站点进行增、删、浏览操作。

知识提高:

一、
Preference



-


方式存储,以

xml
文件的形式保存

二、
File

采用

ja癡⹩漮⨠
库所提供

I/O
接口读写文件

三、
Content P
rovider

用于实现不同应用程序之间的数据共享