android--Database Operation Helper class: Sqliteopenhelper

Source: Internet
Author: User

The 1.mydatabasehelper.java code is as follows:

Package Org.lxh.demo;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public  Class Mydatabasehelper extends Sqliteopenhelper {private static final String DATABASENAME = "test.db";p rivate static final int databaseversion = 1;private static final String TABLENAME = "Mytab";p ublic mydatabasehelper (context context) {Super (C Ontext, DATABASENAME, NULL, databaseversion);} @Overridepublic void OnCreate (Sqlitedatabase db) {/* * String sql= "CREATE TABLE" +tablename+ "(" + * "ID INTEGER Primar Y KEY, "+" name VARCHAR (not null), "+ *" birthday DATE NOT null) "; */string sql = "CREATE TABLE" + tablename+ "(" + "Idintegerprimary key,"///Set As Integer in SQLite, primary//KEY then ID autogrow + "nam e VARCHAR () not NULL, "+" birthday DATE NOT NULL "+") ";d b.execsql (SQL); Execute SQL} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {String sql= "DROP TABLE IF EXISTS" + tablename;db.execsql (SQL); this.oncreate (db);}} 
The 2.hello.java code is as follows:

Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.database.sqlite.sqliteopenhelper;import Android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Hello extends Activity {public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use Sqliteopenhelper helper=new mydatabasehelper (this); Helper.getwritabledatabase ();}}

Run as follows:



android--Database Operation Helper class: Sqliteopenhelper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.