1 <?xml version="1.0" encoding="UTF-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="@drawable/bg"
6 android:orientation="vertical"
7 android:padding="16dp" >
8
9
10 <LinearLayout
11 android:layout_width="match_parent"
12 android:layout_height="wrap_content"
13 android:layout_marginTop="130dp" >
14
15 <TextView
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:text="姓 名 :"
19 android:textSize="18sp" />
20
21 <EditText
22 android:id="@+id/et_name"
23 android:layout_width="match_parent"
24 android:layout_height="wrap_content"
25 android:hint="请输入姓名"
26 android:textSize="16sp" />
27 </LinearLayout>
28
29
30 <LinearLayout
31 android:layout_width="match_parent"
32 android:layout_height="wrap_content"
33 android:layout_marginBottom="10dp" >
34
35 <TextView
36 android:layout_width="wrap_content"
37 android:layout_height="wrap_content"
38 android:text="年 龄:"
39 android:textSize="18sp" />
40
41 <EditText
42 android:id="@+id/et_age"
43 android:layout_width="match_parent"
44 android:layout_height="wrap_content"
45 android:hint="输入年龄"
46 android:textSize="16sp" />
47 </LinearLayout>
48
49
50 <LinearLayout
51 android:layout_width="match_parent"
52 android:layout_height="wrap_content" >
53
54 <Button
55 android:id="@+id/btn_add"
56 android:layout_width="0dp"
57 android:layout_height="wrap_content"
58 android:layout_marginRight="2dp"
59 android:layout_weight="1"
60 android:background="#B9B9FF"
61 android:onClick="add"
62 android:text="添加"
63 android:textSize="18sp" />
64
65 <Button
66 android:id="@+id/btn_search"
67 android:layout_width="0dp"
68 android:layout_height="wrap_content"
69 android:layout_marginRight="2dp"
70 android:layout_weight="1"
71 android:background="#DCB5FF"
72 android:onClick="search"
73 android:text="查询"
74 android:textSize="18sp" />
75
76 <Button
77 android:id="@+id/btn_update"
78 android:layout_width="0dp"
79 android:layout_height="wrap_content"
80 android:layout_marginRight="2dp"
81 android:layout_weight="1"
82 android:background="#E6CAFF"
83 android:onClick="update"
84 android:text="修改"
85 android:textSize="18sp" />
86
87 <Button
88 android:id="@+id/btn_delete"
89 android:layout_width="0dp"
90 android:layout_height="wrap_content"
91 android:layout_weight="1"
92 android:background="#ACD6FF"
93 android:onClick="delete"
94 android:text="删除"
95 android:textSize="18sp" />
96 </LinearLayout>
97
98 <TextView
99 android:id="@+id/tv_show"
100 android:layout_width="match_parent"
101 android:layout_height="wrap_content"
102 android:layout_marginTop="25dp"
103 android:textSize="20sp" />
104
105 </LinearLayout>
1 package com.example.myapplication;
2 import android.os.Bundle;
3 import android.app.Activity;
4 import android.database.Cursor;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.view.View;
7 import android.widget.EditText;
8 import android.widget.TextView;
9 import android.widget.Toast;
10
11 public class MainActivity extends Activity {
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17 }
18
19 public void add(View v) {
20 StuOpenHelper helper = new StuOpenHelper(this);
21 SQLiteDatabase db = helper.getWritableDatabase();
22 String name = ((EditText) findViewById(R.id.et_name)).getText()
23 .toString();
24 int age = Integer.parseInt(((EditText) findViewById(R.id.et_age))
25 .getText().toString());
26
27 db.execSQL("insert into stuinfo (name,age) values(?,?)", new Object[]{
28 name, age});
29 Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
30
31 }
32
33 public void delete(View view) {
34 StuOpenHelper helper = new StuOpenHelper(this);
35 SQLiteDatabase db = helper.getWritableDatabase();
36 db.execSQL("delete from stuinfo where _id=?", new Object[]{2});
37 Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
38
39 }
40
41 public void update(View view) {
42 StuOpenHelper helper = new StuOpenHelper(this);
43 SQLiteDatabase db = helper.getWritableDatabase();
44 db.execSQL("update stuinfo set name=? where _id=?", new Object[]{
45 "micky", 3});
46 Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
47
48 }
49
50 public void search(View view) {
51 StuOpenHelper helper = new StuOpenHelper(this);
52 SQLiteDatabase db = helper.getWritableDatabase();
53 String s = "";
54 Cursor cursor = db.rawQuery("select * from stuinfo", null);
55 if (cursor.getCount() != 0) {
56
57 while (cursor.moveToNext()) {
58 s += cursor.getInt(0) + " " + cursor.getString(1) + " "
59 + cursor.getInt(2) + "\n";
60 }
61 }
62
63 // Toast.makeText(this, s, 0).show();
64 ((TextView) (findViewById(R.id.tv_show))).setText(s);
65
66 }
67
68 }
1 package com.example.myapplication;
2
3 import android.content.Context;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.database.sqlite.SQLiteOpenHelper;
6
7 public class StuOpenHelper extends SQLiteOpenHelper {
8 public StuOpenHelper(Context context) {
9 super(context, "stu.db", null, 1);
10 // TODO Auto-generated constructor stub
11 }
12
13 @Override
14 public void onCreate(SQLiteDatabase db) {
15 // TODO Auto-generated method stub
16 db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)");
17
18 }
19
20 @Override
21 public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
22 // TODO Auto-generated method stub
23
24 }
25
26 }
