Storage of Android SQLite data

Source: Internet
Author: User

Generally in developmentProgramThe data storage function is often used. For example, when developing a program using Delphi, The INI file that stores simple settings of the program sometimes needs to be stored with simple settings in C # development, individuals are generally stored in XML and ini, but they are not as convenient as XML. These are simple data storage functions and such file storage is also supported in Android, however, here we mainly talk about using SQLite to store data. SQLite is a cross-platform database in a single file format, as we usually see the most access database, however, he has better platform support than access and supports paging. Now he is mainly used for embedded development.

During the development process, you can directly declare database objects for storage management,CodeAs follows:

Code

  Sqlitedatabase sqlitedb;  //  Database objects  
Sqlitedb = This . Openorcreatedatabase (datebasename, mode_private, Null );

// Run the Code:
Sqlstring = " Create Table " + Tablename
+ " (_ Id integer primary key, num integer, data text) " ;
Sqlitedb.exe csql (sqlstring );

 

This method requires more content to be processed. I personally feel that it is not very convenient. Just like the C # program we developed using sqlhelper, a similar class is also provided on the Android platform:

Sqliteopenhelper

We can implement this class to complete our operations, which is simpler and more efficient:

The Code is as follows:

Code

 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, version );
// 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 Oncreate (sqlitedatabase dB, string createdbsql ){
// Todo auto-generated method stub
Db.exe csql (createdbsql );
}

@ Override
Public Void Onupgrade (sqlitedatabase dB, Int Oldversion, Int Newversion ){
// Todo auto-generated method stub

}

@ Override
Public Void Onopen (sqlitedatabase dB ){
// Todo auto-generated method stub
Super . Onopen (db );
}

Public Void Insertintodb (sqlitedatabase dB, string SQL)
{
Db.exe csql (SQL );
}
Public Cursor opendb (sqlitedatabase dB, string SQL ){
Return DB. rawquery (SQL, Null );
}
}

 

Here, we only do a simple writing. If we don't write too many methods, this is just a guide. We can extend this class to make it easier to use.

 

The following is a simple call:

Code

  //  Statement  
Sqlitedatabase DB = Null ;

Dbhelper = Null ;

// Instantiation
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 " + 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. getstring (I );
}
Text + = " \ N " ;
}
Toast
. Maketext ( This , Cursor. getcount () + " \ N " + Text,
Toast. length_long). Show ();
}

 

This is just a simple reference! Thank you for your criticism!

 

 

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.