Android开发数据库Sqlite1

1.数据库的创建

DatabaseHelper.java

package com.xrj.databasedemo;

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

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    /*
    context 上下文
    name 数据库名称
    factory 游标工厂
    version 版本号
     */
    public DatabaseHelper(@Nullable Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }
}

Constants.java

package com.xrj.databasedemo;

public class Constants {
    public static final String DATABASE_NAME = "first.db";
    public static final int VERSION_CODE = 1;
}

MainActivity.java

package com.xrj.databasedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建数据库
        DatabaseHelper helper = new DatabaseHelper(this);
        helper.getWritableDatabase();
    }
}

在monitor导出first.db,导入可视化

Android开发数据库Sqlite1

相关推荐