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