zoukankan      html  css  js  c++  java
  • 增删改查

    package com.example.lwj;
    
    import java.awt.Cursor;
    import java.awt.List;
    
    import android.R;
    import android.os.Bundle;
    import android.app.Activity;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_item);
        }
         public void add(View v) {
                StuOpenHelper helper = new StuOpenHelper(this);
                SQLiteDatabase db = helper.getWritableDatabase();
                String name = ((EditText) findViewById(R.id.et_name)).getText()
                        .toString();
                int age = Integer.parseInt(((EditText) findViewById(R.id.et_age))
                        .getText().toString());
                db.execSQL("insert into stuinfo (name,age) values(?,?)", new Object[] {
                        name, age });
                Toast.makeText(this, "ok", 0).show();
            }
         
            public void delete(View view) {
                StuOpenHelper helper = new StuOpenHelper(this);
                SQLiteDatabase db = helper.getWritableDatabase();
                db.execSQL("delete from stuinfo where _id=?", new Object[] { 2 });
                Toast.makeText(this, "ɾ���ɹ�", 0).show();
         
            }
         
            public void update(View view) {
                StuOpenHelper helper = new StuOpenHelper(this);
                SQLiteDatabase db = helper.getWritableDatabase();
                db.execSQL("update stuinfo set name=? where _id=?", new Object[] {
                        "micky", 3 });
                Toast.makeText(this, "�޸ijɹ�", 0).show();
         
            }
            List<Student> list=new ArrayList<Student>();
         
            public void search(View view) {
                System.out.println(list.size()+"��search��");
                StuOpenHelper helper = new StuOpenHelper(this);
                SQLiteDatabase db = helper.getWritableDatabase();
                Cursor cursor = db.rawQuery("select * from stuinfo", null);
                if (((AdapterView<ListAdapter>) cursor).getCount() != 0) {
                    while (cursor.moveToNext()) {
                        stu s1=new stu();
                        s1.setId(cursor.getInt(0));
                        s1.setName(cursor.getString(1));
                        s1.setAge(cursor.getInt(2));
                        list.add(s1);
                         
                    }
                }
                ListView lv=(ListView) findViewById(R.id.lv1);
                lv.setAdapter(new myadapter());
            }
            private class myadapter extends BaseAdapter{
         
                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return list.size();
                }
                @Override
                public View getView(int position, View convertView, ViewGroup arg2) {
             
                    // TODO Auto-generated method stub
                    View view=View.inflate(MainActivity.this, R.layout.list_item, null);
                    TextView tvid=(TextView)view.findViewById(R.id.tv_id);
                    TextView tvname=(TextView)view.findViewById(R.id.tv_name);
                    TextView tvage=(TextView) view.findViewById(R.id.background);
             
                    Bundle list;
                    System.out.println(list.get(position).getId());        
                    tvid.setText(list.get(position).getId()+"");
                    tvname.setText(List.get(position).getName());
                    tvage.setText(list.get(position).getAge()+"");     
                    return view;
                }
         
         
         
                public Object getItem(int arg0) {
                    // TODO Auto-generated method stub
                    return null;
                }
         
         
         
                public long getItemId(int arg0) {
                    // TODO Auto-generated method stub
                    return 0;
                }
                 
            }
         
        }
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    
    public class StuOpenHelper extends SQLiteOpenHelper{
         public StuOpenHelper(Context context) {
                super(context, "stu.db", null, 1);
                // TODO Auto-generated constructor stu
            }
         @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)");
         
            }
         
            @Override
            public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                // TODO Auto-generated method stub
         
            }
         
        }
               
    public class stu {
         private int id;
            private String name;
            private int age;
            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 int getAge() {
                return age;
            }
            public void setAge(int age) {
                this.age = age;
            }
         
        }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"
        android:orientation="vertical"
        android:padding="16dp" >
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="130dp" >
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓  名 :"
                android:textSize="18sp" />
     
            <EditText
                android:id="@+id/et_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入姓名"
                android:textSize="16sp" />
        </LinearLayout>
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp" >
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="年  龄:"
                android:textSize="18sp" />
     
            <EditText
                android:id="@+id/et_age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="输入年龄"
                android:textSize="16sp" />
        </LinearLayout>
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
     
            <Button
                android:id="@+id/btn_add"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight="1"
                android:background="#B9B9FF"
                android:onClick="add"
                android:text="添加"
                android:textSize="18sp" />
     
            <Button
                android:id="@+id/btn_query"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight="1"
                android:background="#DCB5FF"
                android:onClick="search"
                android:text="查询"
                android:textSize="18sp" />
     
            <Button
                android:id="@+id/btn_update"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight="1"
                android:background="#E6CAFF"
                android:onClick="update"
                android:text="修改"
                android:textSize="18sp" />
     
            <Button
                android:id="@+id/btn_delete"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#ACD6FF"
                android:onClick="delete"
                android:text="删除"
                android:textSize="18sp" />
        </LinearLayout>
     
        
     
            <ListView
                android:id="@+id/lv1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginTop="25dp"
                android:textSize="20sp" />
        
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
     
        <TextView
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
     
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />
     
        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
     
    </LinearLayout>

  • 相关阅读:
    02、书店寻宝(一)
    01、博客爬虫
    00、下载文件
    068、Calico的网络结构是什么?(2019-04-11 周四)
    067、如何部署Calico网络 (2019-04-10 周三)
    066、Weave如何与外网通信?(2019-04-09 周二)
    065、容器在Weave中如何通信和隔离?(2019-04-08 周一)
    064、Weave网络结构分析(2019-04-04 周四)
    063、如何使用weave网络(2019-04-03 周三)
    CMD运行指令
  • 原文地址:https://www.cnblogs.com/917174759luowenjing/p/14040863.html
Copyright © 2011-2022 走看看