• activity_main


    activity_main

    <?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:padding="16dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_marginTop="130dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <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:text="添加"
                android:textSize="18sp"
                android:onClick="add" />
            <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:text="查询"
                android:textSize="18sp"
                android:onClick="serch"/>
            <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:text="修改"
                android:textSize="18sp"
                android:onClick="update"/>
            <Button
                android:id="@+id/btn_delete"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#ACD6FF"
                android:text="删除"
                android:textSize="18sp"
                android:onClick="delete" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:textSize="20sp" />
    </LinearLayout>
    StuOpenHelper
    package com.example.zsgc;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    import androidx.annotation.Nullable;
    
    public class StuOpenHelper extends SQLiteOpenHelper {
        public StuOpenHelper(@Nullable Context context) {
            super(context, "stu.db", null, 1);
        }
    
        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table student (id integer primary key autoincrement ,name varchar(20),price integer)");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    
        }
    }
    MainActivity
    package com.example.zsgc;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.annotation.SuppressLint;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @SuppressLint("WrongConstant")
        public void add(View view){
            String name = ((TextView)findViewById(R.id.et_name)).getText().toString();
            int age = Integer.parseInt(((TextView)findViewById(R.id.et_age)).getText().toString());
            StuOpenHelper stuOpenHelper=new StuOpenHelper(this);
            SQLiteDatabase db = stuOpenHelper.getReadableDatabase();
            db.execSQL("insert into student (name,price) values(?,?)",new Object[]{name,age});
            Toast.makeText(this,"ok",0).show();
        }
    
        public void serch(View view){
            StuOpenHelper stuOpenHelper=new StuOpenHelper(this);
            SQLiteDatabase db = stuOpenHelper.getReadableDatabase();
            String s="";
            Cursor cursor = db.rawQuery("SELECT * from student", null);
            while (cursor.getCount()!=0){
                while (cursor.moveToNext()){
                    s+=cursor.getInt(0)+" "+cursor.getInt(1)+" "+cursor.getInt(2)+"
    ";
                }
            }
            ((TextView)(findViewById(R.id.tv_show))).setText(s);
        }
    }
  • 相关阅读:
    windows安装pkg-config
    完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题
    iOS 解析RFC3339时间格式
    依赖工程开发,编译报错Command Libtool failed with a nonzero exit code
    移除项目中的UIWebView
    输出手机中的字体与常用的苹方字体名称
    Null passed to a callee that requires a non-null argument
    SDK内本地化处理 localizedStringForKey:value:table:
    iOS [AFHTTPSessionManager GET:parameters:progress:success:failure:]: unrecognized selector sent to
    xcode搜索路径缩写
  • 原文地址:https://www.cnblogs.com/horfe666/p/14125461.html
Copyright © 2020-2023  润新知