Android Database SQLite (a)

Source: Internet
Author: User

Create a database

First define the subclass of the Sqliteopenhelper

The code is as follows:

Package Com.example.myandroid;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;public class Mydatabasehelper extends sqliteopenhelper{private static final String DATABASENAME = "Mldn.db";p rivate static final int databaseversion = 1;private static final String TABLENAME = "Myta B ";p ublic Mydatabasehelper (context context) {super (context,databasename,null,databaseversion);} Public Mydatabasehelper (mainactivity context) {super (context,databasename,null,databaseversion);} public void OnCreate (Sqlitedatabase db) {String sql = "CREATE TABLE" +tablename+ "(" + "idintegerprimary KEY," + "Nameverchar (50) Not NULL, "+" birthdaydatenot null) ";d b.execsql (SQL);} public void Onupgrade (sqlitedatabase db,int oldversion,int newversion) {String sql = "DROP TABLE IF EXITS" +tablename;db.ex Ecsql (SQL); this.oncreate (db);}}

Then define the activity class

The code is as follows:

Package Com.example.myandroid;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.printstream;import Java.util.scanner;import Android.os.Bundle;import Android.app.activity;import Android.content.sharedpreferences;import Android.database.sqlite.SQLiteOpenHelper; Import Android.view.contextmenu;import Android.view.contextmenu.contextmenuinfo;import Android.view.layoutinflater;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.widget.expandablelistadapter;import Android.widget.expandablelistview;import Android.widget.expandablelistview.onchildclicklistener;import Android.widget.expandablelistview.ongroupclicklistener;import Android.widget.expandablelistview.ongroupcollapselistener;import Android.widget.expandablelistview.ongroupexpandlistener;import Android.widget.tabhost;import Android.widget.tabhost.tabspec;import Android.widget.textview;import Android.widget.toast;public class MainACtivity extends Activity {@Override public void onCreate (Bundle savedinstancestate) {super.oncreate (Savedin        Stancestate);        Setcontentview (R.layout.activity_main);        Sqliteopenhelper helper = new Mydatabasehelper (this);    Helper.getwritabledatabase (); }   }

After running the database is created, you can see the corresponding database in the Data\data\ package name \databases in DDMS

In the case of starting the virtual machine, you can enter the database from the command line.

Specific as follows:

1. Enter the ADB shell in the command line mode and enter the shell command line mode,

Note: The first two are due to the virtual machine not starting up

2. Enter the path of the mldn.db via the CD command: Data\data\ package name \databases

3. Through the LS command, find the content under the path, you can find the corresponding database mldn.db.

4. Enter the sqlite3 mldn.db command to enter the SQLite database

5. Enter the. Schema command to query the data tables in the database.

In this case, the SQL statement can be used to manipulate the table.

Android Database SQLite (a)

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.