小学生100字日记,新概念英语2,特种养殖业
android专门提供了一个 sqliteopenhelper帮助类对数据库进行创建和升级
sqliteopenhelper需要创建一个自己的帮助类去继承它并且重写它的两个抽象方法,即 oncreate() 和 onupgrade()
sqliteopenhelper 中有两个重要的实例方法:getreadabledatabase() 和 getwritabledatabase(),第一个方法可以在磁盘空间已满的时候,只读数据,而第二种方法在空间已满的时候,则会出现异常
创建一个名为 bookstore.db 的数据库
在这个数据库中新建一张 book表,表中有 id(主键)、作者、价格、页数和书名等列
我们在我们的项目当中新建 mydatabasehelper类,并继承自 sqliteopenhelper,后面则可以在我们的主活动当中启用这段新建的代码,这个类的代码如下:
package com.example.lenovo.studyittwo; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.widget.toast; public class mydatabasehelper extends sqliteopenhelper { private static final string create_book = "create table book(" +"id integer primary key autoincrement," +"author text," +"price real," +"pages integer," +"name text)"; private context mcontext; /** * 构造方法 * @param context * @param name 数据库名 * @param factory 允许我们在查询数据的时候返回一个自定义的 cursor,一般都是传入 null * @param version 当前数据库的版本号,可用于对数据库进行升级操作 */ public mydatabasehelper(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, name, factory, version); mcontext = context; } /** * 创建数据库 * @param db */ @override public void oncreate(sqlitedatabase db) { // 执行建表语句 db.execsql(create_book); toast.maketext(mcontext,"创建数据库功",toast.length_long).show(); } /** * 升级数据库 * @param db * @param oldversion * @param newversion */ @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } }
下面是主活动的代码。也十分容易理解,我们在一个按钮的事件当中加入主活动与这个类相联系的函数就可以了:
package com.example.lenovo.studyittwo; import android.content.intentfilter; import android.content.sharedpreferences; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; public class mainactivity extends appcompatactivity { private mydatabasehelper dbhelper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // 构建mydatabasehelper对象,指定数据库名为"bookstore.db、版本号为1 dbhelper = new mydatabasehelper(this, "bookstore.db", null, 1); button btn_create_database = (button) findviewbyid(r.id.creat); btn_create_database.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的) dbhelper.getwritabledatabase(); } }); }}
下面是我们主界面的代码,只需要创建一个按钮就可以了:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout 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" tools:context=".mainactivity"> <button android:id="@+id/creat" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="creat database"/> </android.support.constraint.constraintlayout>
最后,我们只需要按住这个按钮,界面上就会弹出“数据库创建成功”的toast资阳区,再次点击的时候就不会出现了,但我们可以使用另外的的方法来查看我们创建数据库成功没,打开cmd就可以很容易地查看了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论