首页 > 其他分享 >2022-11-19学习内容-Server端代码编写-Client端代码编写

2022-11-19学习内容-Server端代码编写-Client端代码编写

时间:2022-11-19 23:55:40浏览次数:49  
标签:11 UserDBHelper String 代码 db import 编写 android public

1.Server端代码编写

1.1UserDBHelper.java

package com.example.chapter07_server.database;

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

public class UserDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "user.db";
    public static final String TABLE_NAME = "USER_INFO";
    private static final int DB_VERSION = 1;
    private static UserDBHelper mHelper = null;

    private UserDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    // 利用单例模式获取数据库帮助器的唯一实例
    public static UserDBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new UserDBHelper(context);
        }
        return mHelper;
    }

    // 创建数据库,执行建表语句
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                "_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " NAME VARCHAR NOT NULL," +
                " AGE INTEGER NOT NULL," +
                " HEIGHT LONG NOT NULL," +
                " WEIGHT FLOAT NOT NULL," +
                " MARRIED INTEGER NOT NULL);";
        db.execSQL(sql);
    }

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

}

1.2UserInfoProvider.java

package com.example.chapter07_server.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.example.chapter07_server.database.UserDBHelper;

public class UserInfoProvider extends ContentProvider {

    private UserDBHelper dbHelper;

    @Override
    public boolean onCreate() {
        Log.d("ning", "UserInfoProvider onCreate");
        dbHelper = UserDBHelper.getInstance(getContext());
        return true;
    }

    // content://com.example.chapter07_server.provider.UserInfoProvider/user
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d("ning", "UserInfoProvider insert");
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.insert(UserDBHelper.TABLE_NAME, null, values);
        return uri;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Log.d("ning", "UserInfoProvider query");
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(UserDBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, null);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

2.Client端代码编写

2.1activity_content_write.xml

<?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="vertical"
    android:padding="5dp">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="姓名:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="请输入姓名"
            android:inputType="text"
            android:maxLength="12"
            android:text="Jack"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="年龄:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="请输入年龄"
            android:inputType="number"
            android:maxLength="2"
            android:text="18"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_height"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="身高:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="请输入身高"
            android:inputType="number"
            android:maxLength="3"
            android:text="180"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_weight"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="体重:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_weight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="请输入体重"
            android:inputType="numberDecimal"
            android:maxLength="5"
            android:text="180"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/ck_married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:gravity="center"
        android:text="已婚"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

 

标签:11,UserDBHelper,String,代码,db,import,编写,android,public
From: https://www.cnblogs.com/pingfanliliang/p/16905420.html

相关文章

  • 11.19小记
    上午参加洛谷模拟赛,想了3hT1仍不会正解,想着打个暴力拿80,没想到数组开小只有60,开大之后就100了下午参加端点星,T1一眼,T2写了个nklog,没开O2只有30,T3少看条件认为不可做就溜去......
  • 代码随想录算法训练营Day04|24. 两两交换链表中的节点、19. 删除链表的倒数第 N 个结
    代码随想录算法训练营Day04|24.两两交换链表中的节点、19.删除链表的倒数第N个结点、02.07.链表相交、142.环形链表II24.两两交换链表中的节点题目链接:24.两两交......
  • cuda11镜像
    目录pullgputf使用镜像测试是否使用的gpupulldockerpullnvidia/cuda:11.2.0-cudnn8-devel-ubi7dockertagnvidia/cuda:11.2.0-cudnn8-devel-ubi7registry.cn-......
  • 2022.11.19
    ###noip模拟又炸了。。。。。。##出错点t1:又假了,问题是自己知道假了还没想着写暴力##过程分析半小时通读完先开得t2,因为有个点没转化过来,打了个多一个二分log的......
  • 散乱的思绪-2022.11.19
    我是一个什么都想要,梦想着一步登天的妄想者。我没有一颗聪明绝顶的脑袋,也没有出众样貌,更没有父母的日复一日劳作的毅力。有时候我在想,我是一个什么样的人呢?在我看来我大概......
  • jbpm 常用代码及 jbpm IdentityService 扩展
    一、流程定义1.部署流程定义ProcessEngineprocessEngine=newConfiguration().buildProcessEngine();RepositoryServicerepositoryService=......
  • Python程序员:代码写的好,丝滑的壁纸少不了
    不知道大家的电脑桌面一般用的什么类型的壁纸?早上来上班,打开电脑,被漂亮的桌面壁纸所吸引,年底将近,这又是哪个地方的节日?才晓得,原来这是泰国第二大城市清迈的“天......
  • #yyds干货盘点#【愚公系列】2022年11月 微信小程序-多人音视频对话
    前言微信小程序开发多人音视频对话首先得去小程序管理后台,「开发」-「接口设置」中自助开通该组件权限。相关属性:一级类目/主体类型二级类目小程序内容场景教......
  • 1183. 电力
    题目链接1183.电力给定一个由\(n\)个点\(m\)条边构成的无向图,请你求出该图删除一个点之后,连通块最多有多少。输入格式输入包含多组数据。每组数据第一行包含两个......
  • T292113 [传智杯 #5 练习赛] 平等的交易 ----- 贪心算法、upper_bound()/lower_bound(
    题目描述你有 nn 件道具可以买,其中第 ii 件的价格为 a_iai​。你有 ww 元钱。你仅能用钱购买其中的一件商道具。当然,你可以拿你手中的道具换取其他的道具,只是这......