鱼C论坛

 找回密码
 立即注册
查看: 2105|回复: 0

[学习笔记] android 6.4.2

[复制链接]
发表于 2017-10-17 07:39:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
修改mainactivity.java
  1. package com.example.xinwei.databasetest;

  2. import android.support.v7.app.AlertDialog;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;

  7. public class MainActivity extends AppCompatActivity {
  8.     private MyDatabaseHelper dbHelper;
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
  14.         Button createDatabase = (Button)findViewById(R.id.create_database);
  15.         createDatabase.setOnClickListener(new View.OnClickListener() {
  16.             @Override
  17.             public void onClick(View view) {
  18.                 dbHelper.getWritableDatabase();
  19.             }
  20.         });
  21.     }
  22. }
复制代码

修改mydatabasehelper.java
  1. package com.example.xinwei.databasetest;

  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.widget.Toast;

  6. /**
  7. * Created by xinwei on 2017/10/16.
  8. */

  9. public class MyDatabaseHelper extends SQLiteOpenHelper {
  10.     public static final String CREATE_BOOK = "create table book("
  11.             +"id integer primary key autoincrement,"
  12.             +"author text,"
  13.             +"price real,"
  14.             +"pages integer,"
  15.             +"name text)";
  16.     public static final String CREATE_CATEGORY = "create table Category("
  17.             +"id integer primary key autoincrement,"
  18.             +"category_name text,"
  19.             +"category_code integer)";
  20.     private Context mContext;
  21.     public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  22.         super(context, name, factory, version);
  23.         mContext=context;
  24.     }

  25.     @Override
  26.     public void onCreate(SQLiteDatabase sqLiteDatabase) {
  27.         sqLiteDatabase.execSQL(CREATE_BOOK);
  28.         sqLiteDatabase.execSQL(CREATE_CATEGORY);
  29.         Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
  30.     }

  31.     @Override
  32.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  33.         sqLiteDatabase.execSQL("drop table if exists book");
  34.         sqLiteDatabase.execSQL("drop table if exists Category");
  35.         onCreate(sqLiteDatabase);
  36.     }
  37. }
复制代码

这样就成功对数据库进行了更新

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 19:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表