• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android SQLite数据库——数据库创建和升级

武飞扬头像
芒果海
帮助3

layout文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_create_table"
        android:id="@ id/btn_create_table"/>
</LinearLayout>

MainActivity文件

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper myDatabaseHelper;

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

//        myDatabaseHelper = new MyDatabaseHelper(this,"BookStore,db",null,1);
        myDatabaseHelper = new MyDatabaseHelper(this,"BookStore,db",null,2);
        findViewById(R.id.btn_create_table).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDatabaseHelper.getWritableDatabase();
            }
        });

    }
}
学新通

MyDatabaseHelper.java文件

package com.example.databasetest;

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

import androidx.annotation.Nullable;

public class MyDatabaseHelper extends SQLiteOpenHelper {

    //建表语句
    public static final String CREATE_BOOK = "create table book("
     "id integer primary key autoincrement,"
     "author text,"
     "price real,"
     "pages integer,"
     "name text)";
    private Context mContext;

    //新建表
    public static final String CREATE_CATEGORY = "create table Category("
             "id integer primary key autoincrement,"
             "category_name text,"
             "category_code integer)";


    public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    //升级表单,修改表单内容
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //执行两条DROP语句,如果发现数据库中已经存在Book表或者Category表,就进行删除后重新拆功能键
        db.execSQL("drop table if exists Book");
        db.execSQL("drop table if exists Category");
        db.execSQL(CREATE_BOOK);
        Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
       onCreate(db);

    }
}

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhgbhk
系列文章
更多 icon
同类精品
更多 icon
继续加载