鱼C论坛

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

[学习笔记] android编程6.4.1

[复制链接]
发表于 2017-10-16 06:24:40 | 显示全部楼层 |阅读模式

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

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

x
首先新建个项目,然后在mainactivity.java旁边新建个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.     private Context mContext;
  17.     public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  18.         super(context, name, factory, version);
  19.         mContext=context;
  20.     }

  21.     @Override
  22.     public void onCreate(SQLiteDatabase sqLiteDatabase) {
  23.         sqLiteDatabase.execSQL(CREATE_BOOK);
  24.         Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
  25.     }

  26.     @Override
  27.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

  28.     }
  29. }
复制代码

然后修改activity_main.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.xinwei.databasetest.MainActivity">

  8.     <Button
  9.         android:id="@+id/create_database"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:text="create database"/>

  13. </android.support.constraint.ConstraintLayout>
复制代码

再修改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,1);
  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. }
复制代码

数据库创建就大功告成了,非常简单。

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 06:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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