第八周

标签:tco   sele   sqlite   void   content   mat   gets   class   code   

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <LinearLayout android:padding="16dp" android:orientation="vertical" android:background="@drawable/bg" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 4 
 5 
 6     <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginTop="130dp">
 7 
 8         <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="18sp" android:text="姓 名 :"/>
 9 
10         <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="16sp" android:hint="请输入姓名" android:id="@+id/et_name"/>
11 
12     </LinearLayout>
13 
14 
15     -<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginBottom="10dp">
16 
17     <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="18sp" android:text="年 龄:"/>
18 
19     <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="16sp" android:hint="输入年龄" android:id="@+id/et_age"/>
20 
21 </LinearLayout>
22 
23 
24     -<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent">
25 
26     <Button android:background="#B9B9FF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="添加" android:id="@+id/btn_add" android:onClick="add" android:layout_weight="1" android:layout_marginRight="2dp"/>
27 
28     <Button android:background="#DCB5FF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="查询" android:id="@+id/btn_query" android:onClick="search" android:layout_weight="1" android:layout_marginRight="2dp"/>
29 
30     <Button android:background="#E6CAFF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="修改" android:id="@+id/btn_update" android:onClick="update" android:layout_weight="1" android:layout_marginRight="2dp"/>
31 
32     <Button android:background="#ACD6FF" android:layout_height="wrap_content" android:layout_width="0dp" android:textSize="18sp" android:text="删除" android:id="@+id/btn_delete" android:onClick="delete" android:layout_weight="1"/>
33 
34 </LinearLayout>
35 
36 
37     -<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent">
38 
39     <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginTop="25dp" android:textSize="20sp" android:id="@+id/tv_show"/>
40 
41 </ScrollView>
42 
43 </LinearLayout>

 1 package com.example.login_add;
 2 
 3 import android.annotation.SuppressLint;
 4 import android.os.Bundle;
 5 import android.app.Activity;
 6 import android.content.ContentValues;
 7 import android.database.Cursor;
 8 import android.database.sqlite.SQLiteDatabase;
 9 import android.view.Menu;
10 import android.view.View;
11 import android.widget.EditText;
12 import android.widget.TextView;
13 import android.widget.Toast;
14 
15 public class MainActivity extends Activity {
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21     }
22 
23     @SuppressLint("WrongConstant")
24     public void add(View v) {
25         StuOpenHelper helper = new StuOpenHelper(this);
26         SQLiteDatabase db = helper.getWritableDatabase();
27         String name = ((EditText) findViewById(R.id.et_name)).getText()
28                 .toString();
29         int age = Integer.parseInt(((EditText) findViewById(R.id.et_age))
30                 .getText().toString());
31 
32         // 原生sql操作,
33         // db.execSQL("insert into stuinfo (name,age) values(‘lisi‘,22)");
34         // db.execSQL("insert into stuinfo (name,age) values(?,?)",new
35         // Object[]{"kitty",30});
36         db.execSQL("insert into stuinfo (name,age) values(?,?)", new Object[] {
37                 name, age });
38         Toast.makeText(this, "ok", 0).show();
39 
40     }
41 
42     public void delete(View view) {
43         StuOpenHelper helper = new StuOpenHelper(this);
44         SQLiteDatabase db = helper.getWritableDatabase();
45         db.execSQL("delete from stuinfo where _id=?", new Object[] { 2 });
46         Toast.makeText(this, "删除成功", 0).show();
47 
48     }
49 
50     @SuppressLint("WrongConstant")
51     public void update(View view) {
52         StuOpenHelper helper = new StuOpenHelper(this);
53         SQLiteDatabase db = helper.getWritableDatabase();
54         db.execSQL("update stuinfo set name=? where _id=?", new Object[] {
55                 "micky", 3 });
56         Toast.makeText(this, "修改成功", 0).show();
57 
58     }
59 
60     public void search(View view) {
61         StuOpenHelper helper = new StuOpenHelper(this);
62         SQLiteDatabase db = helper.getWritableDatabase();
63         String s = "";
64         Cursor cursor = db.rawQuery("select * from stuinfo", null);
65         if (cursor.getCount() != 0) {
66 
67             while (cursor.moveToNext()) {
68                 s += cursor.getInt(0) + "   " + cursor.getString(1) + "   "
69                         + cursor.getInt(2) + "\n";
70             }
71         }
72 
73         // Toast.makeText(this, s, 0).show();
74         ((TextView) (findViewById(R.id.tv_show))).setText(s);
75 
76     }
77 
78 }

 1 package com.example.login_add;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 6 import android.database.sqlite.SQLiteOpenHelper;
 7 
 8 public class StuOpenHelper extends SQLiteOpenHelper {
 9 
10     public StuOpenHelper(Context context) {
11         super(context, "stu.db", null, 1);
12         // TODO Auto-generated constructor stub
13     }
14 
15     @Override
16     public void onCreate(SQLiteDatabase db) {
17         // TODO Auto-generated method stub
18         db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)");
19 
20     }
21 
22     @Override
23     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
24         // TODO Auto-generated method stub
25 
26     }
27 
28 }

 

第八周

标签:tco   sele   sqlite   void   content   mat   gets   class   code   

原文地址:https://www.cnblogs.com/baigei/p/14163903.html

版权声明:完美者 发表于 2020-12-25 11:58:52。
转载请注明:第八周 | 完美导航

暂无评论

暂无评论...