1.
MainActivity.java

package com.example.dbproject;

import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
DbContect helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

helper=new DbContect( MainActivity.this);
Button btn=findViewById(R.id.create);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View V) {
SQLiteDatabase db=helper.getWritableDatabase();
}
});
}
}

2.DbContect.java
package com.example.dbproject;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DbContect extends SQLiteOpenHelper {
private static final int VERSION=1;
private static final String DBNAME="Users.db"; // 创建数据库名叫 Users
private Context mContext;

public DbContect(Context context){
super(context,DBNAME,null,VERSION);
mContext = context;
}
//创建数据库
public void onCreate(SQLiteDatabase db){
//创建密码表 pwd_tb
db.execSQL("create table pwd_tb (pwd varchar(20) primary key)");
//创建收入表 user_tb
db.execSQL("create table user_tb(_id integer primary key autoincrement, money decimal," +
" time varchar(10),type varchar(10),handler varchar(100),mark varchar(200))");
}
//数据库版本更新
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists pwd_tb");
db.execSQL("drop table if exists user_tb");
onCreate(db);
}

}

3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00BCD4"
android:textSize="22dp"
android:text="点击创建Users数据库"
>
</Button>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


最新文章

  1. Create a bridge using a tagged vlan (8021.q) interface
  2. 张小龙在2017微信公开课PRO版讲了什么(附演讲实录和2016微信数据报告)
  3. 虚拟机Linux----Ubuntu1204----安装jdk1.8
  4. Eclipse 代码格式化
  5. hdu 4000 树状数组
  6. 使用VS2010调用matlab的mat格式文件
  7. 《University Calculus》-chape8-无穷序列和无穷级数-基本极限恒等式
  8. phpstorm 配置
  9. 常用监控SQL
  10. 算法题丨Next Permutation
  11. springboot+aop切点记录请求和响应信息
  12. Java分布式锁,搞懂分布式锁实现看这篇文章就对了
  13. Django后端向前端直接传html语言防止转义的方法(2种)
  14. bash 文件名操作 常用方法
  15. 装饰器 以及 django 中的应用
  16. 读取本地图片 BitmapImage
  17. Flex的一些小实例
  18. vue input,textarea失去焦点调用函数方法
  19. 20155209林虹宇逆向及Bof基础实验报告
  20. android中的目录结构介绍

热门文章

  1. Triple 协议支持 Java 异常回传的设计与实现
  2. CH579-Lwip-2.12移植
  3. Spark下中文分词常用项目
  4. css、js 缓存清除
  5. Vue双向绑定原理梳理
  6. 使用动态输出打印内核的DEBUG信息
  7. 基于 VScode 搭建 Matlab 运行环境
  8. NLP知识图谱项目合集(信息抽取、文本分类、图神经网络、性能优化等)
  9. Java 进阶P-4.4+P-4.5
  10. 【读书笔记】JS函数式编程指南