Android 与SQlite 数据库操作(新手步骤)

Android 与SQlite 数据库操作(新手步骤)搭建简易的界面新建shape xml文件,定义一个圆角和边框创建shape文件 tv_corner.xml创建shape文件 tv_stroke.xml搭建简易页面 activity_main.xml界面截图控件id截图新建MyHelper.java构造方法,调用SQLiteOpenHelper的构造方法,创建数据库“contacter”重写方法搭建MainActivity.java框架定义控件,创建点击事件框架创建MyHelper 对象实现增,改,删,
摘要由CSDN通过智能技术生成

一、搭建简易的界面

1 新建shape xml文件,定义一个圆角和边框

在这里插入图片描述

1.1 创建shape文件 tv_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp"/>
    <stroke android:color="@color/colorPrimaryDark"
            android:width="3dp"/>
</shape>

在这里插入图片描述

1.2 创建shape文件 tv_stroke.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="3dp"/>
    <stroke android:color="#28A62D"
            android:width="3dp"/>
</shape>

2 搭建简易页面 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="简易通讯录"
        android:textSize="40sp"
        android:textColor="#1051DD"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="84dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="30sp" />

        <EditText
            android:id="@+id/ed_name"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:background="@drawable/tv_stroke"
            android:hint="请输入用户名"
            android:textSize="30sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机号:"
            android:textSize="30sp"/>
        <EditText
            android:id="@+id/ed_phonenum"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:background="@drawable/tv_stroke"
            android:hint="请输入手机号"
            android:textSize="30sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="0dp">

        <Button
            android:id="@+id/bt_insert"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:textSize="20sp"
            android:background="@drawable/tv_corner"
            android:text="插入" />

2.1 界面截图

在这里插入图片描述

2.2 控件id截图

在这里插入图片描述

二、新建MyHelper.java

在这里插入图片描述

1 构造方法,调用SQLiteOpenHelper的构造方法,创建数据库“contacter”

    public MyHelper(Context context){
   
        super(context,"contacter",null,2);
    }

2 重写方法

在这里插入图片描述

package com.maaa.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyHelper extends SQLiteOpenHelper {
   

    public MyHelper(Context context){
   
        //创建数据库
        super(context,"contacter.db",null,2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
   
        //创建表名
        db.execSQL("CREATE TABLE information (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   

    }
}

三.搭建MainActivity.java框架

1.定义控件,创建点击事件框架

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   

    private EditText name,phonenum;
    private Button insert,query,updata,delete;
    private TextView show;

    private void init(){
   
        name = findViewById(R.id.ed_name);
        phonenum = findViewById(R.id.ed_phonenum);
        show = findViewById(R.id.tv_show);

        insert= findViewById(R.id.bt_insert);
        query = findViewById(R.id.bt_query);
        updata = findViewById(R.id.bt_updata);
        delete = findViewById(R.id.bt_delete);

        insert.setOnClickListener(this);
        query.setOn
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值