An example analysis of database SQL programming for Android programming _android

Source: Internet
Author: User
Tags sqlite static class

The example of this article is about the implementation of database SQL programming for Android programming. Share to everyone for your reference. The specific analysis is as follows:

Install lightweight database SQLite in Android, and now test the database for basic operations.

Database basic operation: CREATE TABLE, INSERT, delete can use Execsql (), read can use Rawquery (), both functions can be standard SQL statement operation.

Source:

Package com.test.sql; 
Import android.app.Activity; 
Import Android.content.Context; 
Import Android.database.Cursor; 
Import Android.database.sqlite.SQLiteDatabase; 
Import Android.database.sqlite.SQLiteOpenHelper; 
Import Android.os.Bundle; 
Import Android.util.Log; 
 public class Test_sql extends activity {databasehelper mopenhelper; 
 private static final String database_name = "TEST.DB"; 
 private static final int database_version = 1; private static class Databasehelper extends Sqliteopenhelper {databasehelper (context) {Super (context, DAT 
  Abase_name, NULL, database_version); @Override public void OnCreate (Sqlitedatabase db) {//Create a new user table//Total 5 items: Id Pwd Name onlinetime level D
  B.execsql ("CREATE TABLE user_table" (' Id ' INT, ' Pwd ' VARCHAR, ' Name ' VARCHAR, ' onlinetime ' int, ' level ' int) ");  @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}/** called when the The activity is the created. */ 
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  Mopenhelper = new Databasehelper (this);
  Sqlitedatabase db = Mopenhelper.getwritabledatabase (); 
  Clears the data db.execsql ("DELETE from user_table"); Insert data 10000 123456 "JDH" 0 0//Insert data 10001 123456 "Jim" 0 0 db.execsql ("INSERT INTO user_table VALUES (?,?,?,?,?)", NE 
  W object[]{10000, "123456", "Jdh", 0,0}); 
  Db.execsql ("INSERT into user_table VALUES (?,?,?,?,?)", New object[]{10001, "123456", "Jim", 0,0}); 
  Read data Cursor Cursor = Db.rawquery ("SELECT * from user_table WHERE Name =?", New string[]{"JDH"}); while (Cursor.movetonext ()) {String str = cursor.getint (0) + cursor.getstring (1) + cursor.getstring (2) + cursor.ge 
   TInt (3) + cursor.getint (4); 
  LOG.I ("str:", str); }  
 } 
}

I hope this article will help you with your 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.