Android uses SQLite to store data usage examples _android

Source: Internet
Author: User
Tags sqlite sqlite database stub

The example in this article describes how Android uses SQLite to store data. Share to everyone for your reference, specific as follows:

Generally in the development program, regular use of data storage functions, such as the use of Delphi development program, the storage program's simple settings of the INI file, in C # development sometimes also need to store simple settings, individuals are generally stored in XML, also in the INI stored, but not as easy as XML use, So these are simple data storage features that support this kind of file storage on Android, but now it's mostly about using SQLite to store data, SQLite is a cross-platform database, a single file, like the most common Access databases we have. But he has better platform support than access, and supports paging, now primarily for embedded development.

In the development process can actually declare a database object for storage management, the code is as follows:

Sqlitedatabase Sqlitedb; Database object
sqlitedb = This.openorcreatedatabase (datebasename, mode_private, null);
Execution code:
sqlstring = "CREATE TABLE" + tablename + "(_id integer primary key,num text)";
Sqlitedb.execsql (SqlString);

This way we need to deal with more content, personal feeling is not too convenient, just as we develop C # program using SqlHelper, in the Android platform also provides a similar class:

Sqliteopenhelper such a class

We can do this by implementing this class, which is simpler and more efficient:

The code is as follows:

Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;
  public class DBHelper extends Sqliteopenhelper {private static final int db_version = 1; Public DBHelper (context, String name, Cursorfactory factory, int version) {Super (context, name, factory, vers
    ION);  TODO auto-generated Constructor stub} public dbhelper (context, String dbname) {Super (context, dbname,
  NULL, db_version); @Override public void OnCreate (Sqlitedatabase db) {//TODO auto-generated method stub} public void Oncreat
  E (sqlitedatabase db, String createdbsql) {//TODO auto-generated Method Stub db.execsql (createdbsql); @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated Meth OD stub} @Override public void OnOpen (SQLiteDatabase db) {//TODO auto-generated Method stub super.onopen (db);
  public void Insertintodb (sqlitedatabase db,string sql) {db.execsql (SQL);
  Public Cursor opendb (sqlitedatabase db,string sql) {return db.rawquery (SQL, NULL);

 }
}

Here is only a simple writing, not too many ways to write this is just a guide, you can extend this class, so it is more convenient to use.

The following is a simple call:

/declaration Sqlitedatabase db = null;
DBHelper dbhelper = null;
Instantiate dbhelper = new DBHelper (This, "dbname");  method void CreateTable () {String sql = ' Create Table if not EXISTS ' + table_name + (' + ID + ' text not NULL, ' +
    NAME + "text not null);";
  Dbhelper.oncreate (DB, SQL);
    } void Insertdb (string id, string name) {db = Dbhelper.getwritabledatabase ();
    String sql = "INSERT INTO" + table_name + ("+ ID +", "+ name +") VALUES (' + ID + ', ' "+ name + ')"; DBHelper.
  INSERTINTODB (DB, SQL);
    } void Getdb () {db = Dbhelper.getreadabledatabase ();
    String sql = "SELECT * from" + table_name;
    Cursor Cursor = DBHELPER.OPENDB (db, SQL);
    String text = ""; while (Cursor.movetonext ()) {for (int i = 0; i < Cursor.getcolumncount (); i++) {text = Cursor.getstrin
      g (i);
    Text = "\ n";
} toast.maketext (this, cursor.getcount () + "\ n" + Text,toast.length_long). Show (); }

Here is just to provide a simple reference! Welcome to criticize and correct me!

For more information on Android-related content readers can view the site: "Android operation SQLite Database Skills Summary", "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", " Android File Operation Tips Summary, "Android programming development of the SD card operation method Summary", "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and " A summary of the usage of Android controls

I hope this article will help you with the Android program.

Related Article

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.